Author |
Topic |
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2013-06-26 : 05:05:59
|
Hey guys Need another favour from this forum Questions i am struggling writing into a sql query1. I need to produce another column called “Intial Application”. All i need to work out is a count of how many times the open date is equal to fee_wholesale_Date, and if the value is true produce a 1, if its false produce a 02. Produce another column and work out a count how many fee_wholesale_Date are equal to this month. This is my query SELECT Audit_FDMS_Billing_Fees_Hist.FDMSAccountNo,Dim_Outlet.Open_Date,Audit_FDMS_Billing_Fees_Hist.fee_wholesale_date,DATEDIFF(month,dim_Outlet.Open_Date,Audit_FDMS_Billing_Fees_Hist.fee_wholesale_date) AS DiffDate --Case (fee_wholesale_date) when MAX THEN Test else 1 endFROM Audit_FDMS_Billing_Fees_Hist inner JOIN Dim_Outlet ON Audit_FDMS_Billing_Fees_Hist.FDMSAccountNo = Dim_Outlet.FDMSAccountNowhere fee_sequence = '32r'order by FDMSAccountNowould appreciate any help Regards |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-26 : 05:20:58
|
may be this?SELECT Audit_FDMS_Billing_Fees_Hist.FDMSAccountNo,Dim_Outlet.Open_Date,Audit_FDMS_Billing_Fees_Hist.fee_wholesale_date,DATEDIFF(month,dim_Outlet.Open_Date,Audit_FDMS_Billing_Fees_Hist.fee_wholesale_date) AS DiffDate ,COUNT(CASE WHEN opendate = fee_wholesale_date THEN 1 END) OVER () AS DateSameCount,COUNT(1) OVER (PARTITION BY DATEDIFF(mm,0,fee_wholesale_Date)) AS DateMonthSameCount--Case (fee_wholesale_date) when MAX THEN Test else 1 endFROM Audit_FDMS_Billing_Fees_Hist inner JOIN Dim_Outlet ON Audit_FDMS_Billing_Fees_Hist.FDMSAccountNo = Dim_Outlet.FDMSAccountNowhere fee_sequence = '32r'order by FDMSAccountNo ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2013-06-26 : 05:29:12
|
HI Bandi The Case when statement is correct however i have left out an important element on point (2) i want to Produce another column and work out a count how many fee_wholesale_Date are equal to this month., however the open date cant be equal to fee_wholesale_Dateso for eg if open date = 21/06/2013 and fee_wholesale_Date = 26/06/2013 will produce 1But i only want to give it a one when fee_wholesale_Date equals this month |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-26 : 05:33:27
|
quote: Originally posted by masond HI Bandi The Case when statement is correct however i have left out an important element on point (2) i want to Produce another column and work out a count how many fee_wholesale_Date are equal to this month., however the open date cant be equal to fee_wholesale_Dateso for eg if open date = 21/06/2013 and fee_wholesale_Date = 26/06/2013 will produce 1But i only want to give it a one when fee_wholesale_Date equals this month
see my last suggestionCOUNT(1) OVER (PARTITION BY DATEDIFF(mm,0,fee_wholesale_Date)) AS DateMonthSameCountwill give you total count for wholesale date in same month------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
mmkrishna1919
Yak Posting Veteran
95 Posts |
Posted - 2013-06-26 : 05:34:08
|
may be this!sum(case when Open_Date=fee_wholesale_date then 1 else 0 end) AS initial_application,sum(case when (Open_Date=fee_wholesale_date and fee_wholesale_date>=dateadd(mm,datediff(mm,0,getdate()),0) and fee_wholesale_date<=dateadd(mm,datediff(mm,0,getdate())+1,0) )then 1 else 0 end) as second_colThanks..M.MURALI kRISHNA |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2013-06-26 : 05:40:37
|
It has to be a case statement. This is the case statement bandi kindly provided for me, and i have checked this logic and it works Case WHEN Open_Date = fee_wholesale_date THEN 1 else 0 end As "Added initial application"I need to do a similar case stement but along the lines of Case WHEN max(fee_wholesale_date) > Open_Date THEN 1 else 0 end As "Added after Open date", |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-26 : 05:46:18
|
quote: Originally posted by masond It has to be a case statement. This is the case statement bandi kindly provided for me, and i have checked this logic and it works Case WHEN Open_Date = fee_wholesale_date THEN 1 else 0 end As "Added initial application"I need to do a similar case stement but along the lines of Case WHEN max(fee_wholesale_date) > Open_Date THEN 1 else 0 end As "Added after Open date",
it cant just be a CASE statementas what you need is an aggregate which you cant just use directly as you're not applying any GROUP BYso either you've to apply group by and apply aggregationor you need to use OVER () syntax as i suggested------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2013-06-26 : 05:50:12
|
Visakh16 It looks like i need to apply a group as the over() syntax doesnt provide required result |
|
|
|