- This topic has 0 replies, 1 voice, and was last updated 12 years, 11 months ago by .
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
Home › Forums › Tech › Web Development › How to do a conditional COUNT with T-SQL
Tagged: Conditional, Count, Group, SQL, Summarize
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.
© 2017 DomainWebCenter.com. All Rights Reserved. | Disclaimer | Contact the Editor