Skip to content
Advertisement

Apply multiple count conditionally in SQL Server query

I have a task table which contains some DateTime type columns like completed, pending, exception. It joins user table and I want to bring
userName, AllTaskCount, AllPendingTaskCount, AllCompletedTaskCount, AllExceptionTaskCount.

If Completed columns has value than it will be considered as completed task, while if completed column is null with value in exception column then it is ExceptionTask and if neither Completed column has value nor exception one than it is pending task.

So I want to apply multiple count conditionally on above given condition. Please help…

Advertisement

Answer

You can use CTEsto get the result.

Added sample schema and data. Please update it as per your requirements. SQLFiddle

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement