在SQL Server中,将列转换为行的方法有多种。以下是一些常用的方法:
SELECT *
FROM (
SELECT Category, Sales
FROM SalesTable
) AS s
PIVOT (
SUM(Sales)
FOR Category IN ([Category1], [Category2], [Category3])
) AS p
SELECT
SUM(CASE WHEN Category = 'Category1' THEN Sales ELSE 0 END) AS Category1,
SUM(CASE WHEN Category = 'Category2' THEN Sales ELSE 0 END) AS Category2,
SUM(CASE WHEN Category = 'Category3' THEN Sales ELSE 0 END) AS Category3
FROM SalesTable
SELECT Category, Sales
FROM SalesTable
UNPIVOT (
Sales FOR Category IN (Sales1, Sales2, Sales3)
) AS u
这些是一些常见的将列转换为行的方法。具体使用哪种方法取决于你的需求和数据结构。