2014-11-02 88 views

回答

1

我想這是你想要的東西:

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 
+0

哇,你是我的英雄,謝謝了很多:-) – Muflix 2014-11-03 07:02:59