Author |
Topic |
thanksfor help
Posting Yak Master
106 Posts |
Posted - 2008-07-30 : 14:05:09
|
Hi,when I query with different group by clause, I get differnt total sum. For this query with timemonth, I get -6 million resultSELECT sum(Sales.Amount), custname, timeMonthFROM Sales,WHERE timeMonth In ( '2008 06','2008 05','2008 04','2008 03','2008 02','2008 01','2007 12','2007 11','2007 10','2007 09','2007 08','2007 07' )GROUP BY custname, timeMonthHAVING sum(Sales.pfBaseAmount) <= 0When did the same query with just custname grp by, the result is -1 million.SELECT sum(Sales.Amount), custname FROM Sales,WHERE timeMonth In ( '2008 06','2008 05','2008 04','2008 03','2008 02','2008 01','2007 12','2007 11','2007 10','2007 09','2007 08','2007 07' )GROUP BY custname HAVING sum(Sales.pfBaseAmount) <= 0What can the reason. Any tip will be of great help.thanks in advance |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-07-30 : 14:08:16
|
Because the WHERE clause is run before the GROUP BY. If you want it after, then put it in the HAVING. Or am I misunderstanding the question?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
thanksfor help
Posting Yak Master
106 Posts |
Posted - 2008-07-30 : 14:13:18
|
Where part is working fine.It something to do with the data, that I am missing to understand |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-07-30 : 14:16:11
|
You missed my point.Show us some sample data to help illustrate your issue for us. Remember we can't read minds nor can we see your environment.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-30 : 14:21:15
|
quote: Originally posted by thanksfor help Where part is working fine.It something to do with the data, that I am missing to understand
its actually because you're grouping each time on different fields. in first case it gives unique records per custname,timeMonth combination while in second case it further compacts the result to one per custname which is why you get 1 million against 6 millionconsider this simple examplecustname timemonthc1 1c1 1c1 2c1 2c1 3now taking unique records per custname,timeMonth combination you getcustname timemonthc1 1c1 2c1 3which is first caseand taking unique records per custname you getcustnamec1 same is case happening above.Having less fields on GROUP BY means taking unique record per that column value which leads to less rows returned. |
 |
|
thanksfor help
Posting Yak Master
106 Posts |
Posted - 2008-07-30 : 14:28:14
|
Sorry, thanks for quick reply.For query with timemonth 2007 07 2007 08 2007 09........................... 2008 06Custname ABCD -2345 -1789 -90 -140000VXS 0 -3456 -87 -3456 ..................................................................................... Total -2345 - 5245 177 -143456 Overalltotal is( -6 million)When timemonth is not in the group by clause,custname salestotalXYZ -9000JFK - 2390.................Total (- 1 million)thanks for Ur time |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
thanksfor help
Posting Yak Master
106 Posts |
Posted - 2008-07-30 : 14:32:58
|
Visakh16, but why is the total sum is giving different result. when group by C1, it sum up all the sales total irrespective of month which should be same as custname timemonthc1 1c1 2c1 3Thanks in advance |
 |
|
|