我想知道是否有选项可以从某些表中进行选择,结果如下:
我将从数据库中获取"Text1", "Text3","Text3"
,我不会在数据库中使用SUBTEXT
而我应该自己编写Date
是来自DB和值来完成表格。
答案 0 :(得分:1)
您可以使用PIVOT。
唯一的缺点是,如果不手动编程枢轴列名称(FOR SellDay IN ( [20150101], [20150201] )
),则无法进行旋转。
;WITH CTE_Products AS
(
SELECT 1 AS Product, 'CatFood' AS SubCategory, 'Food' AS Category, 1.0 AS Price
UNION SELECT 2 AS Product, 'DogFood' AS SubCategory, 'Food' AS Category, 2.0 AS Price
UNION SELECT 3 AS Product, 'HumanFood' AS SubCategory, 'Food' AS Category, 3.0 AS Price
UNION SELECT 5 AS Product, 'Windows Software' AS SubCategory, 'Software' AS Category, 4.0 AS Price
UNION SELECT 6 AS Product, 'Mac Software' AS SubCategory, 'Software' AS Category, 5.0 AS Price
UNION SELECT 4 AS Product, 'Linux Software' AS SubCategory, 'Software' AS Category, 0.0 AS Price
)
,CTE_SALES AS
(
SELECT 1 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 3 AS Quantity
UNION SELECT 2 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 6 AS Quantity
UNION SELECT 3 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 4 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 5 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 6 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 1 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 3 AS Quantity
UNION SELECT 2 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 6 AS Quantity
UNION SELECT 3 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 4 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 5 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 6 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 9 AS Quantity
)
SELECT *
FROM
(
SELECT
Category
,SubCategory
,SellDay
,Quantity * Price AS Value
FROM CTE_Products
LEFT JOIN CTE_SALES
ON SalesProduct = Product
) AS SourceTable
PIVOT
(
SUM(Value)
FOR SellDay IN ( [20150101], [20150201] )
) AS PivotTable
您也可以半动态地执行此操作,如下所示:
IF OBJECT_ID('tempdb..#MySales') IS NOT NULL
DROP TABLE #MySales
IF OBJECT_ID('tempdb..##MyPivottedResults') IS NOT NULL
DROP TABLE ##MyPivottedResults
DECLARE @cols nvarchar(MAX)
;WITH CTE_Products AS
(
SELECT 1 AS Product, 'CatFood' AS SubCategory, 'Food' AS Category, 1.0 AS Price
UNION SELECT 2 AS Product, 'DogFood' AS SubCategory, 'Food' AS Category, 2.0 AS Price
UNION SELECT 3 AS Product, 'HumanFood' AS SubCategory, 'Food' AS Category, 3.0 AS Price
UNION SELECT 5 AS Product, 'Windows Software' AS SubCategory, 'Software' AS Category, 4.0 AS Price
UNION SELECT 6 AS Product, 'Mac Software' AS SubCategory, 'Software' AS Category, 5.0 AS Price
UNION SELECT 4 AS Product, 'Linux Software' AS SubCategory, 'Software' AS Category, 0.0 AS Price
)
,CTE_SALES AS
(
SELECT 1 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 3 AS Quantity
UNION SELECT 2 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 6 AS Quantity
UNION SELECT 3 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 4 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 5 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 6 AS SalesProduct, CAST('20150101' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 1 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 3 AS Quantity
UNION SELECT 2 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 6 AS Quantity
UNION SELECT 3 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 4 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 5 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 9 AS Quantity
UNION SELECT 6 AS SalesProduct, CAST('20150201' AS datetime) AS SellDay, 9 AS Quantity
)
SELECT
Category
,SubCategory
,SellDay
,Quantity * Price AS Value
INTO #MySales
FROM CTE_Products
LEFT JOIN CTE_SALES
ON SalesProduct = Product
SELECT
@cols = COALESCE(@cols + N', ', N'') + N'[' + CONVERT(nchar(8), SellDay, 112) + N']'
FROM #MySales
GROUP BY SellDay
ORDER BY SellDay ASC -- DESC ?
SET @cols = N'
SELECT *
INTO ##MyPivottedResults
FROM #MySales
PIVOT(SUM(Value)
FOR SellDay IN (' + @cols + ')) AS PivotTable
'
-- PRINT @cols
EXECUTE(@cols)
SELECT *
FROM ##MyPivottedResults
IF OBJECT_ID('tempdb..#MySales') IS NOT NULL
DROP TABLE #MySales
IF OBJECT_ID('tempdb..##MyPivottedResults') IS NOT NULL
DROP TABLE ##MyPivottedResults