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.
Author |
Topic |
BTrain
Starting Member
2 Posts |
Posted - 2013-10-15 : 23:18:36
|
Hello,I have reviewed the various 'Conditional Sum' questions in the forum and none quite match what I'm trying to do:In the database, there is Date, Store#, Item#, and %Total Sales. In some cases, the same item# for the same date may be given more than one value for '% of Total Sales'. (For some reason this is a valid business scenario that happens rarely, but it happens.) In that situation only, the requirement is to sum the two values together into one line. So if Item# 123 has a line with a value of .05%, and another line with a value of .08%, I need to sum those two values into one line for Item #123 that has a %Total of .13%. ONLY when an item has more than one percentage assigned, those percentages should be summed. Otherwise, if an item# has only one percentage value assigned, we just want to see that value. I cannot figure out how to do this. Basically, I would like to implement logic that would work like this:SELECT Date, Store#, Item#, CASE WHEN Count(%Total Sales) >1 THEN Sum(%Total Sales) ELSE %Total SalesENDFROM (some tables and joins)GROUP BY Date, Store#, Item# However, I'm not sure how to craft it so that I don't get a syntax error (this query produces errors).Any help would be appreciated.Thank you! |
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2013-10-16 : 01:02:12
|
Provide Sample Dataveeranjaneyulu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-16 : 02:05:48
|
As per your explanation you just need thisSELECT [Date], [Store#], [Item#], Sum([%Total Sales]) FROM (some tables and joins)GROUP BY [Date], [Store#], [Item#] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
BTrain
Starting Member
2 Posts |
Posted - 2013-10-28 : 17:06:32
|
Unfortunately, I am still having trouble making this work. Please see the sample data below. Basically, I just want to sum the percentage values only where an item was given more than one percentage:CURRENT OUTPUT:Date Store# Item# %Total Sales10/25/2013 50 123 .0510/25/2013 50 123 .0810/25/2013 50 456 .2010/25/2013 50 789 .03DESIRED OUTPUT:Date Store# Item# %Total Sales10/25/2013 50 123 .1310/25/2013 50 456 .2010/25/2013 50 789 .03Any help is appreciated! Thank you! |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-28 : 17:29:01
|
You can sum even if there is only one row - it will just sum that one row, resulting in the same value. See the example below. You can copy and paste it to an SSMS window and run it - you will see that it gives you exactly the output you are looking for:CREATE TABLE #tmp(Date DATETIME, Store INT, Item INT, PercentTotalSales FLOAT);INSERT INTO #tmp VALUES ('20131025',50,123,0.05),('20131025',50,123,0.08),('20131025',50,456,0.20),('20131025',50,789,0.03)SELECT Date,STore, Item, SUM(PercentTotalSales) AS PercentTotalSalesFROM #tmpGROUP BY Date,STore, ItemDROP TABLE #tmp; |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-28 : 23:50:19
|
IS [%Total Sales] a calculated column in the query?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|