2013-02-05 33 views
3

我需要排序日期透視表的項目數據爲同一項目數量透視表有一個SQL語句

項目中創建一個以上的列如下:

"Project nr" "Task"    "Task deadline" "Task Type Production" 
123    pack     1 april 2013 Pack 
123    Leave production  3 april 2013 Leave Production 
123    Flight date   9 april 2013 Flight Date 

「任務類型生產」是爲了確保字段的內容始終保持一致 我只能在數據透視表中創建一列。是否有3列 顯示THES信息它看起來像這樣的方式:

Project nr ; Pack ; leave production ; flightdate 


SELECT [TaskDeadline] AS Packed 
FROM   MSP_EpmTask_UserView where [Task Type Production] = 'Packed' 
SELECT [TaskDeadline] AS LeaveProduction 
FROM   MSP_EpmTask_UserView where [Task Type Production] = 'Leave Production' 
SELECT [TaskDeadline] AS FlightDate 
FROM   MSP_EpmTask_UserView where [Task Type Production] = 'Flight Date' 

感謝 安妮

回答

1

您可以嘗試創建一個使用相關子查詢的傳統方法的交叉表或透視:

select [Project nr] 
    , (select [Task deadline] from MSP_EpmTask_UserView where [Project nr] = t.[Project nr] and [Task Type Production] = 'Pack') as Pack 
    , (select [Task deadline] from MSP_EpmTask_UserView where [Project nr] = t.[Project nr] and [Task Type Production] = 'Leave Production') as [Leave Production] 
    , (select [Task deadline] from MSP_EpmTask_UserView where [Project nr] = t.[Project nr] and [Task Type Production] = 'Flight Date') as [Flight Date] 
from MSP_EpmTask_UserView t 
group by [Project nr] 
order by [Project nr] 

/* 
Project nr Pack  Leave Production Flight Date 
----------- ---------- ---------------- ----------- 
123   2013-04-01 2013-04-03  2013-04-09 
*/ 

我在SQL Server 2008中具有漂亮date數據類型做到了這一點,但你肯定能做到這一點有刺或日期時間。這也假定每個日期只有一個特定的項目號。

另外,如果可能,儘量不要在數據庫對象名稱中放入空格或其他特殊字符,以強制用括號對它們進行分隔。

4

這可以很容易地使用聚合函數和CASE表達來完成:

select [Project nr], 
    MAX(case when [Task Type Production] = 'Pack' then [Task deadline] end) as Pack, 
    MAX(case when [Task Type Production] = 'Leave Production' then [Task deadline] end) as [Leave Production], 
    MAX(case when [Task Type Production] = 'Flight Date' then [Task deadline] end) as [Flight Date] 
from MSP_EpmTask_UserView 
group by [Project nr] 

SQL Fiddle with Demo

如果要使用SQL Server的PIVOT功能,那麼該查詢將是:

select * 
from 
(
    select [Project nr],[Task deadline], [Task Type Production] 
    from MSP_EpmTask_UserView 
) src 
pivot 
(
    max([Task deadline]) 
    for [Task Type Production] in ([Pack], [Leave Production], 
           [Flight Date]) 
) piv 

參見SQL Fiddle with Demo

最後這可以使用多個來完成連接到您的表:

select t1.[Project nr], 
    t1.[Task deadline] pack, 
    t2.[Task deadline] [Leave Production], 
    t3.[Task deadline] [Flight Date] 
from MSP_EpmTask_UserView t1 
left join MSP_EpmTask_UserView t2 
    on t1.[Project nr] = t2.[Project nr] 
    and t2.[Task Type Production] = 'Leave Production' 
left join MSP_EpmTask_UserView t3 
    on t1.[Project nr] = t3.[Project nr] 
    and t3.[Task Type Production] = 'Flight Date' 
where t1.[Task Type Production] = 'Pack' 

SQL Fiddle with Demo

所有查詢的結果是:

| PROJECT NR |  PACK | LEAVE PRODUCTION | FLIGHT DATE | 
------------------------------------------------------------ 
|  123 | 2013-04-01 |  2013-04-03 | 2013-04-09 | 
+0

+1的選項。我喜歡'case'版本,儘管'pivot'語法在我的盒子上一樣快。 –

+0

@TimLehner我喜歡提供其他版本的查詢。我更喜歡pivot,但有些人在語法上很難。 – Taryn