0
我有這個表 http://sqlfiddle.com/#!3/641f9如何將行轉換爲新列下的列和列數據?
,我想選擇它看起來像這樣
它是如何能夠在SQL Server 2008? 對我來說真的很重要,謝謝。
我有這個表 http://sqlfiddle.com/#!3/641f9如何將行轉換爲新列下的列和列數據?
,我想選擇它看起來像這樣
它是如何能夠在SQL Server 2008? 對我來說真的很重要,謝謝。
我想這是你想要的東西:
IF OBJECT_ID(N'tempdb..#Temp') IS NOT NULL
BEGIN
DROP TABLE #Temp
END
SELECT
sd.Company,
sd.Brand,
'DateFrom' AS Variable,
sd.DateFrom AS [Date],
sd.WeekName
INTO
#temp
FROM
dbo.SourceData AS sd
UNION ALL
SELECT
sd.Company,
sd.Brand,
'DateTo' AS Variable,
sd.DateTo AS [Date],
sd.WeekName
FROM
dbo.SourceData AS sd
SELECT
Company,
Variable,
Brand,
NOVW42012,
MAYW42014,
AUGW32014,
APRW12013,
AUGW52013,
MARW22014
FROM
(
SELECT
Company,
Variable,
Brand,
[Date],
WeekName
FROM
#temp
) sd PIVOT
(MIN(sd.[Date]) FOR WeekName IN (NOVW42012, MAYW42014, AUGW32014, APRW12013,
AUGW52013, MARW22014)) AS PVT
ORDER BY
Company,
Brand
哇,你是我的英雄,謝謝了很多:-) – Muflix 2014-11-03 07:02:59