|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Sum + Case + Group By Problem
I am having a problem with a query. I am getting and extra line from my query that I should. The query is:
select Product_Grouping = case when Product_Grouping = 3 or Product_Grouping = 4 then 5 else product_grouping end , sum(TY_revenue), sum(LY_Revenue) from Products group by product_grouping My results should be as follows: Product_Grouping TY_Revenue LY_Revenue 1 34,673,635 34,450,574 2 25,714,032 28,569,706 5 29,052,227 28,939,060 But what I get is: Product_Grouping TY_Revenue LY_Revenue 1 34673635.0000 34450574.0000 2 25714032.0000 28569706.0000 5 20058886.0000 21018703.0000 5 8993341.0000 7920357.0000 When I add the numbers up, they do add up to the correct number but I should not have the extra line of 5. Any ideas as to what is causing this? Thanks, Scott |
|
#2
|
|||
|
|||
|
The group by isn't gonna refer to the 'product_grouping' that was manipulated by the CASE, it's gonna look @ the orig value in the table. I believe (but may be wrong) that you're getting 4 rows instead 3 b/c of that principle...you can keep what you have, and just wrap another query around it, and I think you'll get what you're looking for...be warned though, it will seem very redundant...someone else will probably chime in w/a better solution though.
Code:
select product_grouping, sum(ty_revenue), sum(ly_revenue) from ( select CASE When Product_grouping = 3 or Product_grouping = 4 Then 5 Else Product_grouping END [Product_grouping], sum(TY_revenue) as TY_revenue, sum(LY_Revenue) as LY_revenue from Products group by product_grouping ) as A group by product_grouping Last edited by Username=NULL : May 25th, 2004 at 09:18 PM. |
|
#3
|
||||
|
||||
|
i think null has identified the problem, and the solution is to give the case expression column a different alias --
Code:
select case when Product_Grouping = 3
or Product_Grouping = 4
then 5
else product_grouping
end
as any_other_name
, sum(TY_revenue) as sumTY
, sum(LY_Revenue) as sumLY
from Products
group
by any_other_name
|
|
#4
|
|||
|
|||
|
That is not allowed by SQL server.
Quote:
|
|
#5
|
||||
|
||||
|
thanks, swampboogie
it's hard to remember all the nuances of so many database systems ![]() so the GROUP BY would have to use the CASE expression too, then |
|
#6
|
|||
|
|||
|
Got It To Work
Thanks to all of you that replied to my question. You were of great help with this problem. This is the query that I got to work.
Thanks, Scott Code:
select Product_Grouping, sum(ty_revenue) [TY_Revenue], sum(ly_revenue) [LY_Revenue] from (select CASE When Product_grouping = 3 or Product_grouping = 4 Then 5 Else Product_grouping END [Product_grouping], sum(TY_revenue) as TY_revenue, sum(LY_Revenue) as LY_revenue from Products group by product_grouping ) as A group by product_grouping |
|
#7
|
|||
|
|||
|
Use
DECODE(Product_grouping, 3, 5, 4, 5, Product_grouping) Instead of CASE When Product_grouping = 3 or Product_grouping = 4 Then 5 Else Product_grouping END [Product_grouping], ![]() |
|
#8
|
||||
|
||||
|
you're prolly thinking of oracle
this is mysql in mysql, the DECODE function is used for encryption: Quote:
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Sum + Case + Group By Problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|