Skip to content
Advertisement

SQL Pivot on Conditional Count

I have a table of vulnerabilities using SQL server, when I perform the following query

The output looks like so.

My aim is to generate an output that pivots the weeks into column tables, with the values being a count of Hosts for a given vulnerability in that week.

My initial attempt was to do

But the output was the correct layout, but incorrect data as if it was only counting the first occurrence. Is an amendment to the count term required or is my approach the wrong one?

Advertisement

Answer

Conditional aggregation is simpler:

Note only is pivot bespoke syntax, but it is sensitive to what columns are in the table. Extra columns are interpreted as “group by” criteria, affecting the results from the query.

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