case statement
DBAsupport.com Forums - Powered by vBulletin
Results 1 to 3 of 3

Thread: case statement

  1. #1
    Join Date
    Jun 2003
    Posts
    132

    case statement

    Hi

    I have the following case statement, but I am not getting the desired results. I would like the results to return a single row for each fname, lname, email with 'Y' or 'N' values for the WEBCAST_1...4.

    Right now I get multiple rows where fname, lname, email has different value for WEBCAST_1, 2, 3 and 4.


    How can I group it so one row will show wether fname, lname, email attended more than one WEBCAST?



    SELECT distinct fname, lname, email,

    (CASE when web_cast_id = 1 then 'Y' else 'N' end) WEBCAST_1,
    (CASE when web_cast_id = 2 then 'Y' else 'N' end) WEBCAST_2,
    (CASE when web_cast_id = 3 then 'Y' else 'N' end) WEBCAST_3,
    (CASE when web_cast_id = 4 then 'Y' else 'N' end) WEBCAST_4

    FROM marketing_leads.web_cast


    Thank You

  2. #2
    Join Date
    Nov 2000
    Location
    Baltimore, MD USA
    Posts
    1,339
    Try:
    Code:
    SELECT 
       fname, 
       lname, 
       email,
       CASE WHEN SUM(CASE WHEN web_cast_id = 1 THEN 1 END) = 0 THEN 'N' ELSE 'Y' END WEBCAST_1,
       CASE WHEN SUM(CASE WHEN web_cast_id = 2 THEN 1 END) = 0 THEN 'N' ELSE 'Y' END WEBCAST_2,
       CASE WHEN SUM(CASE WHEN web_cast_id = 3 THEN 1 END) = 0 THEN 'N' ELSE 'Y' END WEBCAST_3,
       CASE WHEN SUM(CASE WHEN web_cast_id = 4 THEN 1 END) = 0 THEN 'N' ELSE 'Y' END WEBCAST_4
    FROM 
       marketing_leads.web_cast
    GROUP BY
       fname, 
       lname, 
       email
    - Chris
    Christopher R. Long
    ChrisRLong@HotMail.Com
    But that's just my opinion. I could be wrong

  3. #3
    Join Date
    Jun 2003
    Posts
    132
    Thanks for reply,

    Your script returned Y for all WEBCAST1...4

    Actaully this one worked.

    SELECT DISTINCT fname, lname, email,


    max((CASE when web_cast_id = 1 then 'Y' else 'N' end)) AS WEBCAST_1,

    max((CASE when web_cast_id = 2 then 'Y' else 'N' end)) AS WEBCAST_2,

    max((CASE when web_cast_id = 3 then 'Y' else 'N' end)) AS WEBCAST_3,

    max((CASE when web_cast_id = 4 then 'Y' else 'N' end)) AS WEBCAST_4


    FROM marketing_leads.web_cast

    group by fname, lname, email



    Thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width