Two group by conditions in 1 query

  • Hello friends

    I have a tabe of LabTests and I need to add two new columns to this table with two different conditions.

    Here is the sample data

    Lab TestName TotalTests Tests 2

    Alcohol 12 ? ?

    VVV 2 ? ?

    QQQQ 123 ? ?

    Now I need to calculate two columns

    1. Tests <= 2 which means those tests which are less than or equal to a total of 2

    2. Tests > 2

    How do I do it in a single query.

    I know I can calculate in two queries and then join them

    Any thoughts??

    Thanks

  • First you need to provide more data that would show how you get the test2 data as well as the results you are looking for.

    Second, this seems like homework or a test question. Please make an attempt to write a query.

  • Will this work?

    CASE WHEN TotalTests >2 THEN '3PLUS' ELSE '2MINUS' END AS Tests2

    Or do you need two additional columns:

    CASE WHEN TotalTests <= 2 THEN 1 ELSE 0 END AS TEST2,

    CASE WHEN TotalTests > 2 THEN 1 ELSE 0 END AS TEST3

  • Works Well

    Thnks

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

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