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
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Convert sumproduct from Excel to SQL
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

angang
Starting Member

5 Posts

Posted - 02/24/2014 :  16:44:58  Show Profile  Reply with Quote
Hi All, I have this column that calculates the previous yr total drug charges to compare to current yr drug charges all done in Excel. I use the following calculation:
SUMPRODUCT(([Referral Date]<ThisMonthStart)*1,([Referral Date]>=PrvYrStart)*1,[Product Charges])
How do I convert this calculation in SQL, is there a way? any help would be greatly appreciated!

Mar
Starting Member

40 Posts

Posted - 02/25/2014 :  08:24:22  Show Profile  Reply with Quote
I don't know what SUMPRODUCT does and you don't specify what it does either. SQL does have a SUM and you can use inequality operators. Perhaps if you explained in english what you need to do we could help.
Go to Top of Page

angang
Starting Member

5 Posts

Posted - 02/25/2014 :  10:41:07  Show Profile  Reply with Quote
sorry... ok so what that calculation does is to take the sum of product charges for the given period of time, in this case month to month of the previous yr by checking the month column and grouping all the charges for that month in the previous yr and listing it in a separate column. I then do the same thing for the current charges for each current month so now I can compare for example total charges of January 2014 to January 2013 and compute the difference in either numbers or %.... I hope this makes sense
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 02/25/2014 :  11:51:06  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT	SUM(CASE WHEN [Referral Date] >= PrvYrStart AND [Referral Date] < ThisMonthStart THEN [Product Charges] ELSE 0 END)
FROM	dbo.Table1;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

angang
Starting Member

5 Posts

Posted - 02/25/2014 :  15:28:40  Show Profile  Reply with Quote
hi thanks for the reply but I ran it and it gave me 0's all the way down
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 02/25/2014 :  15:49:57  Show Profile  Visit SwePeso's Homepage  Reply with Quote
My suggestion returns only one row.
Let us see your code.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

angang
Starting Member

5 Posts

Posted - 02/25/2014 :  15:55:16  Show Profile  Reply with Quote
it does only return one row with 0's... by examining the data it sould have totals in it other than 0's....

SELECT
SUM(CASE WHEN [Referral Date] >= PrvYrStart AND [Referral Date] < FirstOfMonth THEN [Product Charges] ELSE 0 END)
FROM [prs_MSCRM].[dbo].[Achievement_Analysis_Totals]

is there something im missing? also how would I get that to populate a field in the view so I can access it without calculating it each time
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 02/26/2014 :  11:20:36  Show Profile  Visit SwePeso's Homepage  Reply with Quote
It depends on what datatype you are using for your date columns.
Let med guess...? You are using varchar and storing data in US date format?
Instead of using proper datatypes?

SELECT	
SUM(CASE WHEN cast([Referral Date] as date) >= cast(PrvYrStart as date) AND cast([Referral Date] as date) < cast(FirstOfMonth as date) THEN [Product Charges] ELSE 0 END)
FROM	[prs_MSCRM].[dbo].[Achievement_Analysis_Totals]



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

angang
Starting Member

5 Posts

Posted - 02/26/2014 :  11:49:11  Show Profile  Reply with Quote
I tried your solution and still brings 0 as the result....
Go to Top of Page

Mar
Starting Member

40 Posts

Posted - 02/27/2014 :  10:12:05  Show Profile  Reply with Quote
Can you give us a few rows that should be included in the calculation as an example
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.06 seconds. Powered By: Snitz Forums 2000