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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ Register Now and get your question answered!
 All Forums  SQL Server 2008 Forums  Other SQL Server 2008 Topics  Convert sumproduct from Excel to SQL Reply to Topic  Printer Friendly
Author  Topic

angang
Starting Member

5 Posts

 Posted - 02/24/2014 :  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]=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 - 02/25/2014 :  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.

angang
Starting Member

5 Posts

 Posted - 02/25/2014 :  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

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 02/25/2014 :  11:51:06 ```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

angang
Starting Member

5 Posts

 Posted - 02/25/2014 :  15:28:40 hi thanks for the reply but I ran it and it gave me 0's all the way down

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 02/25/2014 :  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

angang
Starting Member

5 Posts

 Posted - 02/25/2014 :  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

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 02/26/2014 :  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

angang
Starting Member

5 Posts

 Posted - 02/26/2014 :  11:49:11 I tried your solution and still brings 0 as the result....

Mar
Starting Member

47 Posts

 Posted - 02/27/2014 :  10:12:05 Can you give us a few rows that should be included in the calculation as an example
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC