Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Count Help

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 query

1. 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 0

2. 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 end
FROM Audit_FDMS_Billing_Fees_Hist
inner JOIN Dim_Outlet
ON Audit_FDMS_Billing_Fees_Hist.FDMSAccountNo = Dim_Outlet.FDMSAccountNo
where fee_sequence = '32r'
order by FDMSAccountNo


would 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 end
FROM Audit_FDMS_Billing_Fees_Hist
inner JOIN Dim_Outlet
ON Audit_FDMS_Billing_Fees_Hist.FDMSAccountNo = Dim_Outlet.FDMSAccountNo
where fee_sequence = '32r'
order by FDMSAccountNo


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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_Date

so for eg if open date = 21/06/2013 and fee_wholesale_Date = 26/06/2013 will produce 1

But i only want to give it a one when fee_wholesale_Date equals this month


Go to Top of Page

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_Date

so for eg if open date = 21/06/2013 and fee_wholesale_Date = 26/06/2013 will produce 1

But i only want to give it a one when fee_wholesale_Date equals this month





see my last suggestion

COUNT(1) OVER (PARTITION BY DATEDIFF(mm,0,fee_wholesale_Date)) AS DateMonthSameCount

will give you total count for wholesale date in same month

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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_col

Thanks..

M.MURALI kRISHNA
Go to Top of Page

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",
Go to Top of Page

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 statement
as what you need is an aggregate which you cant just use directly as you're not applying any GROUP BY
so either you've to apply group by and apply aggregation

or you need to use OVER () syntax as i suggested

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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
Go to Top of Page
   

- Advertisement -