Skip to content
Advertisement

How can I get values of the Rating column together with their definition for every specific month by using Pivot with Month in MSSQL

Example :

Data:

Query:

I get this error:

Incorrect syntax near ‘,’.

In line with ” Definition, Rating “

Result should look like this:

Is this possible? Any help, please!

Thank you!

*The code above was based on PIVOT with MONTH().

Advertisement

Answer

You could just use conditional aggregation:

This syntax is somehow more flexible than the specific PIVOT operator (and also, for what it’s worth, it works across different database products).

Note that I modified the filters on dates so it uses half-open intervals, and no date function is applied on column dateRecorded: this should allow the database to take advantage of an index on the date column (and would also smoothly handle the time component of dateCreated, if any).

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