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

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.