If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > A query with group problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-06-09, 17:00
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
A query with group problem

select TRPRD,SUM(QTYGD) AS Pieces,(CASE WHEN MCHDC='' THEN 0 ELSE SUM(ELAP1) END ) AS DOWN ,(CASE WHEN MCHDC<>'' THEN 0 ELSE SUM(ELAP1) END) AS UP, MCHDC
from rmsfiles84.TP_PA
group by TRPRD,MCHDC

TRPRD-------- PIECES--------- DOWN--- UP--- MCHDC
AALX -------- 0------ 1--- 0--- 112
AALX-------- 5------- 0--- 3---

But my goal is to group by TRAPRD to sum the pieces, down and up. But MCHDC is a field I need for case condition. How do you think I can do?
Reply With Quote
  #2 (permalink)  
Old 03-06-09, 19:45
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
How about this?

Code:
select TRPRD
     , SUM(QTYGD) AS Pieces
     , SUM(CASE WHEN MCHDC =  '' THEN 0 ELSE ELAP1 END) AS DOWN
     , SUM(CASE WHEN MCHDC <> '' THEN 0 ELSE ELAP1 END) AS UP
  from rmsfiles84.TP_PA 
 group by
       TRPRD
;
Reply With Quote
  #3 (permalink)  
Old 03-09-09, 08:35
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
Doesn't work

That doesn't work unless I put MCHDC on the group by
Otherwise, I got error message:
column MCHDC or expression in SELECT list not valid.

Should I think about sub query?

Code:
 How about this?


Code:
select TRPRD
     , SUM(QTYGD) AS Pieces
     , SUM(CASE WHEN MCHDC =  '' THEN 0 ELSE ELAP1 END) AS DOWN
     , SUM(CASE WHEN MCHDC <> '' THEN 0 ELSE ELAP1 END) AS UP
  from rmsfiles84.TP_PA 
 group by
       TRPRD, MCHDC
;
Reply With Quote
  #4 (permalink)  
Old 03-09-09, 09:15
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
Actually, it worked

Sorry, I just found it worked. Thanks very much.
Reply With Quote
  #5 (permalink)  
Old 03-09-09, 09:56
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
One Little Problem

ROUND(SUM(CASE WHEN MCHDC='' THEN 0 ELSE ELAP1 END ),2)

The result is still with many decimal, The round doesn't work. Why?
Reply With Quote
  #6 (permalink)  
Old 03-09-09, 10:20
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Newer,
you could define the summation as dec 9,2 or something along those lines. Also, instead of then 0, try using then null. It can really help improve your performance as a 0 is added to your summation, whereas, a null is ignored. Though you may have to account for having a null value returned or use the values or coalesce clause to force a 0 when the value is null.

Dave
Reply With Quote
  #7 (permalink)  
Old 03-09-09, 10:34
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
Thanks

I changed to Null, and I did feel the performance is different. Thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On