2016-05-19 65 views
0

我需要轉這個視圖(SQL)移調錶與信息

enter image description here

到這樣的觀點:

enter image description here

任何solutiion?

+0

退房UNPIVOT。 –

+0

請說哪個DBMS? –

+0

請在[幫助]中閱讀[問]。 – Pred

回答

0

您可以使用union all

select location, planning_product, '2016-05-16' as MondayDate, [5/16/2016] as ROP 
from t 
union all 
select location, planning_product, '2016-05-09' as MondayDate, [5/9/2016] as ROP 
from t 
union all 
. . . 

注意,列名需要進行轉義。一些數據庫使用雙引號,其他數據庫使用反引號或方括號。

+0

我按照你的解決方案,但它不showind數字....它只顯示0值 –

+0

選擇[位置],[計劃產品],演員('05/23/16'作爲日期時間)星期一,05/23/16作爲SUKadj_TDOS從[dbo]。[Producttdos] union all 選擇[位置],[計劃產品],Cast(05/30/16'作爲日期時間)作爲SUKadj_TDOS的星期一日,05/30/16 [ dbo]。[Producttdos] union all .... –

+0

@PriscilaEstala。 。 。你必須避開列名。否則'05/23/16'被視爲數值計算。根據數據庫的不同,您可能會使用「05/23/16」或「[05/23/16]」。 –

0

我們假設表名爲標籤並且您有以下3個日期(您可以用自己的日期替換它們):[1/16/2015],[2/16/2015],[3/16/2015]

WITH Unpivoted (PlanningProduct, Location, [Monday Date], ROP) 
AS(
    SELECT PlanningProduct, Location, CONVERT(datetime,MondayDate) AS [Monday Date], ROP 
    FROM 
     (SELECT Location, PlanningProduct, [1/16/2015], [2/16/2015], [3/16/2015] -- add your dates 
     FROM Tab) pvt 
    UNPIVOT 
     (ROP FOR MondayDate IN ([1/16/2015], [2/16/2015], [3/16/2015])) AS unpvt -- add your dates 
) 
SELECT PlanningProduct, Location, [Monday Date], SUM(ROP) AS ROP 
FROM Unpivoted 
GROUP BY PlanningProduct, Location, [Monday Date]