Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2005
    Posts
    2

    Unanswered: Group by a calculated column

    Hi All,

    I have a query like

    select
    'Queue Name' = case when (some conditions)
    then (select 'My queue')
    else (select 'Not my queue') end ,
    some other selected cols here
    group by
    .....

    I want to group the data by 'Queue Name'.

    Can any one help on this..

    Thanks,
    Meenu

  2. #2
    Join Date
    Aug 2004
    Posts
    42
    You simply repeat the column "calculation" in the group by clause leaving out the column name. Something like:

    select QueueName = case when MyCol = "010" then "ABC"
    when MyCol = "160" then "LMN"
    else "XYZ"
    end
    , count (*)

    from MyTable

    group by
    case when MyCol = "010" then "ABC"
    when MyCol = "160" then "LMN"
    else "XYZ"
    end


    If you have inbedded SELECTs inside your THEN clauses you may need to put your result set into a temp table and then group by the simple resulting column name.

Posting Permissions

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