10/31/2023 0 Comments Sql server pivot![]() ![]() When we run the above pivot statement SQL Server gives these 4 errors: We can’t specify type1 twice like above, because it means we will have 2 columns with the name =. As I said, you can’t put 2 measures in pivot, like this: Sum ( case when trans_type = ‘type2’ then profit_amount end ) as profit_type2īut how about the profit_amount column? It needs to be pivoted for type1 and type2 too. Sum ( case when trans_type = ‘type1’ then profit_amount end ) as profit_type1 , Sum ( case when trans_type = ‘type2’ then sales_amount end ) as sales_type2 , ![]() Sum ( case when trans_type = ‘type1’ then sales_amount end ) as sales_type1 , Meaning, in the above source table, if we have sales amount only, or profit amount only, we can use pivot. ![]() ![]() We can use pivot for 1 value column, but unfortunately we can’t use it for 2 value columns. I usually use the old “case when” for this but being 2010, I’d prefer to use pivot statement.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |