How to do a conditional COUNT with T-SQL

Home Forums Tech Web Development How to do a conditional COUNT with T-SQL

This topic contains 0 replies, has 1 voice, and was last updated by  Mr. Mangus 12 years, 3 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2319

    Mr. Mangus
    Participant

    In your TASKS database you have records in various categories.

    You need to produce a summarized report with 3 columns:

    Task Category | Tasks Open | Tasks Completed

    Regular count will not work. You have to use a conditional count.

    SELECT
    tkCategory AS TaskCategory,
    COUNT(CASE WHEN tkDoneYN = 0 THEN 1 ELSE NULL END) AS TasksOpen,
    COUNT(CASE WHEN tkDoneYN = 1 THEN 1 ELSE NULL END) AS TasksCompleted
    FROM TaskMaster
    GROUP BY tkCategory

    Note that field taskDoneYN is of type bool.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.