Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if or case statement 1

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
hi,

have a query that needs to sum as holsum if hourdayid = 1
or sum as holsumhours if hourdayid = 2

am trying to use mysql IF statement had tried using case
but couldnt get it to work - could use two queries using where hourdayid=1 etc but this seems a waste

thanks MG

Code:
SELECT CC.code, CC.holtype, CBB.halfday,
IF CBB.hourdayid = '2' THEN 

(SUM(cbb.mfithours) AS holsumhours)

ELSE 

(SUM(CASE WHEN CBB.halfday = 'checked' THEN ((TO_DAYS(CBB.bookend) - TO_DAYS(CBB.bookst) + 1)/2) 
ELSE (TO_DAYS(CBB.bookend) - TO_DAYS(CBB.bookst) + 1) END) AS holsum)

END IF

FROM holtype AS CC 
INNER JOIN
(SELECT CB.bookst, CB.bookend, CB.userholid, CB.holbookid, CB.holtypeid, CB.deptid, CB.halfday, CB.hourdayid, CB.mfithours, CB.hourdayid 
FROM holbook AS CB 
WHERE CB.userholid = '79' 
AND ((cb.bookst BETWEEN '20080101' AND '20081231') 
OR (cb.bookend BETWEEN '20080101' AND '20081231') 
OR (cb.bookst <= '20080101') AND (cb.bookend >= '20081231'))) 
AS CBB ON CC.holtypeid = CBB.holtypeid 
GROUP BY CC.holtypeid 
ORDER BY CC.holtypeid
 
Code:
SELECT CC.code
     , CC.holtype
     , CBB.halfday
     , [blue]SUM(CASE WHEN CBB.hourdayid = '2' 
                THEN CBB.mfithours
                ELSE NULL 
            END) AS holsumhours
     , SUM(CASE WHEN CBB.halfday = 'checked' 
                THEN (TO_DAYS(CBB.bookend) - TO_DAYS(CBB.bookst) + 1)/2
                ELSE (TO_DAYS(CBB.bookend) - TO_DAYS(CBB.bookst) + 1) 
            END) AS holsum[/blue]
  FROM holtype AS CC 
INNER 
  JOIN ( SELECT CB.bookst
              , CB.bookend
              , CB.userholid
              , CB.holbookid
              , CB.holtypeid
              , CB.deptid
              , CB.halfday
              , CB.hourdayid
              , CB.mfithours
              , CB.hourdayid 
           FROM holbook AS CB 
          WHERE CB.userholid = '79' 
            AND (
                (cb.bookst BETWEEN '20080101' AND '20081231') 
             OR (cb.bookend BETWEEN '20080101' AND '20081231') 
             OR (cb.bookst <= '20080101') AND (cb.bookend >= '20081231')
                )
       ) AS CBB 
    ON CC.holtypeid = CBB.holtypeid 
GROUP 
    BY CC.holtypeid 
ORDER 
    BY CC.holtypeid

r937.com | rudy.ca
 
rudy - thanks alot looks great but at the moment if
cbb.hourdayid = 2 then it sums mfithours but also adds a day to holsum - i need it so that if hourdayid = 2 then doesnt add a day to holsum
 
Code:
     , SUM(CASE CBB.hourdayid = '2' 
                THEN 0
                WHEN CBB.halfday = 'checked' 
                THEN (TO_DAYS(CBB.bookend) - TO_DAYS(CBB.bookst) + 1)/2
                ELSE (TO_DAYS(CBB.bookend) - TO_DAYS(CBB.bookst) + 1) 
            END) AS holsum

r937.com | rudy.ca
 
hi - get a syntax error near THEN 0

Code:
SELECT CC.code
     , CC.holtype
     , CBB.halfday
     , SUM(CASE WHEN CBB.hourdayid = '2' 
                THEN CBB.mfithours
                ELSE NULL 
            END) AS holsumhours
     , SUM(CASE CBB.hourdayid = '2' 
                THEN 0
                WHEN CBB.halfday = 'checked' 
                THEN (TO_DAYS(CBB.bookend) - TO_DAYS(CBB.bookst) + 1)/2
                ELSE (TO_DAYS(CBB.bookend) - TO_DAYS(CBB.bookst) + 1) 
            END) AS holsum
  FROM holtype AS CC
 
well, it looks like i forgot to copy/paste the WHEN keyword

you should be able to spot that type of error, eh

;-)

r937.com | rudy.ca
 
got it - thanks sooo much heres a * - cheers rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top