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
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Convert sumproduct from Excel to SQL

Author  Topic 

angang
Starting Member

5 Posts

Posted - 2014-02-24 : 16:44:58
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

47 Posts

Posted - 2014-02-25 : 08:24:22
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 - 2014-02-25 : 10:41:07
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

30421 Posts

Posted - 2014-02-25 : 11:51:06
[code]SELECT SUM(CASE WHEN [Referral Date] >= PrvYrStart AND [Referral Date] < ThisMonthStart THEN [Product Charges] ELSE 0 END)
FROM dbo.Table1;[/code]


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

angang
Starting Member

5 Posts

Posted - 2014-02-25 : 15:28:40
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

30421 Posts

Posted - 2014-02-25 : 15:49:57
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 - 2014-02-25 : 15:55:16
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

30421 Posts

Posted - 2014-02-26 : 11:20:36
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 - 2014-02-26 : 11:49:11
I tried your solution and still brings 0 as the result....
Go to Top of Page

Mar
Starting Member

47 Posts

Posted - 2014-02-27 : 10:12:05
Can you give us a few rows that should be included in the calculation as an example
Go to Top of Page
   

- Advertisement -