SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Count Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masond
Constraint Violating Yak Guru

447 Posts

Posted - 06/26/2013 :  05:05:59  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 06/26/2013 :  05:20:58  Show Profile  Reply with Quote
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 - 06/26/2013 :  05:29:12  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 06/26/2013 :  05:33:27  Show Profile  Reply with Quote
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

India
88 Posts

Posted - 06/26/2013 :  05:34:08  Show Profile  Reply with Quote
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

Edited by - mmkrishna1919 on 06/26/2013 05:39:35
Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 06/26/2013 :  05:40:37  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 06/26/2013 :  05:46:18  Show Profile  Reply with Quote
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 - 06/26/2013 :  05:50:12  Show Profile  Reply with Quote
Visakh16
It looks like i need to apply a group
as the over() syntax doesnt provide required result
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000