簡體   English   中英

SQL Pivot 生成動態列

[英]SQL Pivot to Generate Dynamic Columns

我有以下數據:

在此處輸入圖像描述

我有以下代碼:


select * from
(
SELECT 
d.CreatedDate,
m.siteid,
m.ProjectNum



FROM DWCorp.SSMaster m 
INNER JOIN DWCorp.SSDetail d ON d.MasterId = m.Id WHERE  ActionId = 7
)as Sourcetable
pivot
(
max(createddate)
for siteid in ([1],[2],[3],[4],[5])
) As pivottable 

我希望數據看起來像這樣:

在此處輸入圖像描述

最多只能有 5 個日期。 我現在擁有它的方式是按我不想要的站點來旋轉它。 我希望它按日期旋轉。

任何人都可以幫忙嗎? 我知道我可能需要使用動態 SQL,但不知道該怎么做。 我已經搜索了論壇,但沒有得到我想要的東西。

文字 Output:

CreatedDate               siteid ProjectNum
2021-04-06 13:14:01.8933333 20  OTHO00006
2021-04-28 16:40:01.9066667 20  OTHO00006
2021-05-03 22:47:01.7466667 20  OTHO00006
2021-04-28 16:42:02.3700000 20  OTHO00016
2021-05-06 13:27:01.9633333 20  OTHO00016
2021-05-27 15:10:01.7066667 20  OTHO00018
2021-06-29 13:01:01.9266667 20  OTHO00024
2021-05-12 13:38:01.8300000 20  OTHO00024
2021-06-29 13:02:04.7800000 20  OTHO00028
2021-03-25 13:00:03.6100000 21  OBEL00001
2021-08-10 19:44:01.9233333 21  OBEL00003
2021-11-03 20:45:39.2733333 21  OBEL00003
2021-04-26 18:57:34.5533333 21  OBEL00004

您可以使用row_number window function 和條件案例聚合來執行此操作:

with c as (
    select Convert(date,CreatedDate) CreatedDate, ProjectNum,
      Row_Number() over (partition by ProjectNum order by createddate) col
    from t
)
select 
    ProjectNum,
    max(case when col=1 then CreatedDate end) Date1,
    max(case when col=2 then CreatedDate end) Date2,
    max(case when col=3 then CreatedDate end) Date3,
    max(case when col=4 then CreatedDate end) Date4,
    max(case when col=5 then CreatedDate end) Date5
from c
group by ProjectNum

像這樣的東西可以解決問題:

select projectNum, 
  [1] as Date1,
  [2] as Date2,
  [3] as Date3,
  [4] as Date4,
  [5] as Date5
from (
  select d.projectNum, d.createdDate, d.dateId
  from (
    select dd.rn as dateId, dd.createdDate, dd.projectNum
    from (
      select *, row_number() over (partition by projectNum order by createdDate asc) rn
      from your_data
      ) dd
    where rn <= 5
    -- order by 3, 1
    ) d
  ) as src
  pivot (
    max(createdDate)
    for dateId in ([1],[2],[3],[4],[5])
  ) as pvt

為了使其更容易並避免使用動態 sql,您可以使用row_number為從最舊到最新排序的每個日期分配一個dateId ,然后將此 id 用於 pivot 數據。

試試這個,但我希望這只是一個示例用例,因為動態用例可能有 365 天/日期

   With dtes as (
  Select distinct trim(createdDate), row_number() over (order by 
   1) rn 
  From  DWCorp.SSMaster m 
  INNER JOIN DWCorp.SSDetail d ON d.MasterId = m.Id WHERE  
  ActionId = 7 )

    SELECT 
    m.ProjectNum, 
  
     Max(case when trim(createddate) = Select 
    trim(createdDate) from dtes where rn=1 then m.siteid end), 
    Max(case when trim(createddate) = Select 
    trim(createdDate) from dtes where rn=2 then m.siteid end)
     Max(case when trim(createddate)= Select 
    trim(createdDate) from dtes where rn=3 then m.siteid end),
    Max(case when trim(createddate)= Select 
    trim(createdDate) from dtes where rn=4 then m.siteid end),
    
       Max(case when trim(createddate)= Select 
    trim(createdDate) from dtes where rn=5 then m.siteid end)
    
  FROM DWCorp.SSMaster m 
 INNER JOIN DWCorp.SSDetail d ON d.MasterId = m.Id WHERE  
 ActionId = 7 
  group by project_num
 
  
 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM