Why is my query not giving correct results?

  • Sorry mates but this seems very simple, yet very, very frustrating.

    When I run this query:

    SELECT clientid,

    CASE WHEN D.ccode = '-1' Then 'Did Not Show' ELSE D.ccode End ccode,

    ca,

    ot,

    bw,

    cshT,

    dc,

    dte,

    approv

    FROM dbo.emC D WhERE year(dte) = year(getdate())

    I get the correct results in that ccode shows 'Did Not Show' when ccode is '-1'

    However, when I do a UNION so I can get total for each column, I get the results but I don't see 'Did Not Show' when ccode is '-1'.

    I know we have over 100 rows of records with value of '-1'.

    Can someone please help?

    Thank you very much.

    SELECT clientid,

    CASE WHEN D.ccode = '-1' Then 'Did Not Show' ELSE D.ccode End ccode,

    ca,

    ot,

    bw,

    cshT,

    dc,

    dte,

    approv

    FROM dbo.emC D WhERE year(dte) = year(getdate())

    UNION ALL

    SELECT 'Total',

    '',

    SUM(D.ca),

    SUM(D.ot),

    SUM(D.bw),

    SUM(D.cshT),

    '',

    '',

    ''

    FROM emC D WHERE YEAR(dte)='2011'

    Thanks a lot in advance

  • Please post the DDL and some sample data along with the expected output. This will help people help you faster and in a better manner.


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • I figured it out. thanks

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply