如何使用 PIVOT 根据日期排列数据

How to arrange data according to date with PIVOT

我真的需要帮助,因为我的大脑已经不能正常工作了。所以我有以下这些数据:

PesID CreDate WeekOfMonth(CreDate) TestingProgress
Pes 1 30/03/2021 3-5 30%
Pes 2 31/03/2021 3-5 20%
Pes 180 02/04/2021 3-5 10%
Pes 3 03/04/2021 3-5 25%
Pes 1 03/04/2021 3-5 50%
Pes 150 04/04/2021 4-1 30%
Pes 1 06/04/2021 4-1 70%
Pes 1 12/04/2021 4-2 100%
Pes 2 15/04/2021 4-2 50%

对于WeekOfMonth(CreDate)来说,数据实际上是CreDate月份和星期的concat,所以4月1日到4月3日,按日历算作3月的第5周。所以我需要将 WeekOfMonth(CreDate) 更改为列并按 PesID 排列数据分区并按 CreDate 排序。如果相同的 PesID 也有相同的 WeekOfMonth (CreDate),那么我想获得最高百分比的 TestingProgress。预期结果应如下所示:

PesID 3-1 3-2 3-3 3-4 3-5 4-1 4-2
Pes 1 NULL NULL NULL NULL 50% 70% 100%
Pes 2 NULL NULL NULL NULL 20% NULL 50%
Pes 180 NULL NULL NULL NULL 10% NULL NULL
Pes 3 NULL NULL NULL NULL 25% NULL NULL
Pes 150 NULL NULL NULL NULL NULL 30% NULL

但是,我目前的情况如下:

PesID 3-1 3-2 3-3 3-4 3-5 4-1 4-2
Pes 1 NULL NULL NULL NULL 30% 70% 100%
Pes 150 NULL NULL NULL NULL NULL 30% NULL
Pes 180 NULL NULL NULL NULL 10% NULL NULL
Pes 2 NULL NULL NULL NULL 20% NULL 50%
Pes 3 NULL NULL NULL NULL 25% NULL NULL

这个结果是根据 PesID 而不是 CreDate 排列的,我不知道我应该如何添加带有 PIVOT 的 ORDER BY 子句。下面是我的 SQL 代码:

with
t1
as(
select [RN] = Row_Number()Over(Partition by [PesID],[WeekByMonth] Order by [CreDate] asc)
    ,*
from [MyTable]),

t2
as(select *
from t1
where [RN]='1')

select [PesID],[1-1],[1-2],[1-3],[1-4],[1-5],[2-1],[2-2],[2-3],[2-4],[2-5],[3-1],[3-2],[3-3],[3-4],[3-5],[4-1],[4-2],[4-3],[4-4],[4-5],[5-1],[5-2],[5-3],[5-4],[5-5],[6-1],[6-2],[6-3],[6-4],[6-5]
,[7-1],[7-2],[7-3],[7-4],[7-5],[8-1],[8-2],[8-3],[8-4],[8-5],[9-1],[9-2],[9-3],[9-4],[9-5],[10-1],[10-2],[10-3],[10-4],[10-5]
,[11-1],[11-2],[11-3],[11-4],[11-5],[12-1],[12-2],[12-3],[12-4],[12-5]
from
( 
    select [PesID],[TestingProgress],[WeekByMonth]
    from t2

) d

pivot
(
    min(TestingProgress)
     for [WeekByMonth] in ([1-1],[1-2],[1-3],[1-4],[1-5],[2-1],[2-2],[2-3],[2-4],[2-5],[3-1],[3-2],[3-3],[3-4],[3-5],[4-1],[4-2],[4-3],[4-4],[4-5],[5-1],[5-2],[5-3],[5-4],[5-5],[6-1],[6-2],[6-3],[6-4],[6-5]
,[7-1],[7-2],[7-3],[7-4],[7-5],[8-1],[8-2],[8-3],[8-4],[8-5],[9-1],[9-2],[9-3],[9-4],[9-5],[10-1],[10-2],[10-3],[10-4],[10-5]
,[11-1],[11-2],[11-3],[11-4],[11-5],[12-1],[12-2],[12-3],[12-4],[12-5])
) piv

有人可以帮我解决这个问题吗?非常感谢。

您必须仅使用必要的列来提供数据透视表。你可能会注意到我创建了一个列MinDate,它使用window函数min() over(),那么它是Order By MinDate

的小事

简化版

例子

Set DateFormat DMY

Declare @YourTable Table ([PesID] varchar(50),[CreDate] date,[WeekOfMonth(CreDate)] varchar(50),[TestingProgress] varchar(50))  Insert Into @YourTable Values 
 ('Pes 1','30/03/2021','3-5','30%')
,('Pes 2','31/03/2021','3-5','20%')
,('Pes 180','02/04/2021','3-5','10%')
,('Pes 3','03/04/2021','3-5','25%')
,('Pes 1','03/04/2021','3-5','50%')
,('Pes 150','04/04/2021','4-1','30%')
,('Pes 1','06/04/2021','4-1','70%')
,('Pes 1','12/04/2021','4-2','100%')
,('Pes 2','15/04/2021','4-2','50%')

Select [PesID],[3-4],[3-5],[4-1],[4-2],[4-3]
  From (
        Select MinDate = min(CreDate) over (partition by [PesID])
              ,[PesID]
              ,[WeekOfMonth(CreDate)]
              ,[TestingProgress]
         From  @YourTable
       ) src
 Pivot (max([TestingProgress]) for [WeekOfMonth(CreDate)] in ( [3-4],[3-5],[4-1],[4-2],[4-3] ) ) pvt
 Order By MinDate

Returns