在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
這些是一些常見的將列轉換為行的方法。具體使用哪種方法取決于你的需求和數據結構。