• madsovenielsen (8/4/2013)


    That query looks overly complex, is it not possible to use IF ELSE instead of the switch case?

    It really isn't that complex. You could instead choose to not use the cte but then you will need to group by all of the non-aggregate columns.

    You can't use IF ELSE for data. The IF ELSE construct is used to control the flow of statements, that is why we use the case expression here.

    What this query is doing is known as a cross tab. You can read more about the techniques that you could use by following the links in my signature.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/