Results 1 to 5 of 5

Thread: MS SQL Qry - Using CAse

  1. #1
    Join Date
    Aug 2003
    Posts
    6

    MS SQL Qry - Using CAse

    Hi,
    i have a table(table1) like this

    BatchID Status
    ------- ------
    1001      1
    1001      2
    1001      2
    1002      0
    1002      3
    1002      4

    "Status" has value from 0 to 5..

    i want to write a sql query
    where i get the count of batch's status in a sinle row
    like


    Batch Status-0 Status-1 Status-2 .....
    ----- -------- -------- --------
    1001     0       1         2

    1002     1       0         0

    something like this, where Each batch's all status values is displayed in a single row..
    how 2 do this, plz help me out..

    thanks in adv
    Venu

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    Look BOL for cross tab and pivot table.
    Last edited by MAK; 08-09-2003 at 07:58 AM.

  3. #3
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    The number of status you want to display has to be fixed for you to get the result using T-SQL, that is if you add a row with new status your query has to be changed.

  4. #4
    Join Date
    Aug 2003
    Posts
    6
    Hi,
    the status in the table are fixed, there will not be any addition to status.

    can u plz help me out in building the query...

    manu thanks
    Venu

  5. #5
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    If you look in BOL, you have similar query.

    select batchid,
    sum(case status when 0 then 1 else 0 end) as [status-0],
    sum(case status when 1 then 1 else 0 end) as [status-1],
    sum(case status when 2 then 1 else 0 end) as [status-2],
    sum(case status when 3 then 1 else 0 end) as [status-3],
    sum(case status when 4 then 1 else 0 end) as [status-4]
    from table1
    group by batchid

Posting Permissions

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