Skip to content
Advertisement

How to minimize my big query for showing counts using SQL

I am taking counts from CTE but common is Status(working,pening) and Divisions but my query is becoming big because left join is same but just status and division are different. I have wrote 10 left join count but by passing status and divisions.

Below is my whole sql query

Is there anything which we can minimize query by using group by or anything.

Advertisement

Answer

I’ve taken a bit of a stab at your schema, and I think you could do it with something like:

N.B. I’ve removed NOLOCK as putting this everywhere is a bad habit to kick. Also, I am deeply suspicious of most queries that use DISTINCT across a large number of columns like this. More often than not the duplicates are a symptom of an error with the query, and DISTINCT is just a nasty plaster trying to cover up the real issue. You should work out where duplicates are coming from and look to remove either early or at least in a deterministic fashion.

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