t-sql group by pivot 查询

t-sql group by pivot query

我有一个结果集

name        time_begin      time_end
SUNDAY      00:01:00        13:59:00
SUNDAY      14:01:00        23:59:00
MONDAY      22:01:00        21:59:00
TUESDAY     00:01:00        23:59:00
WEDNESDAY   00:01:00        23:59:00
THURSDAY    00:01:00        23:59:00
FRIDAY      00:01:00        23:59:00
SATURDAY    00:01:00        23:59:00

现在我希望此数据显示为

SUNDAY_begin    SUNDAY_end      MONDAY_begin    MONDAY_end  TUESDAY_begin   TUESDAY_end
00:01:00        13:59:00        22:01:00        21:59:00    00:01:00        23:59:00
14:01:00        23:59:00        null            null        null            null

其他日子依此类推。

如何在 t-sql 查询中实现这一点?

更新: -> @Deepak 的查询:是的。因为星期天最初有两行,所以新结果集中的第二行。由于星期一或星期二没有多行,因此它为空。没有逻辑分组。想象一下,它只是任何一天的可能时间段列表。 谢谢

您需要为 ROW_NUMBER() 获取一列以获取每个工作日重复值的行。

;WITH CTE AS
(
    SELECT ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY (SELECT 0)) RNO,
    NAME+'_begin' [DATECOL],time_begin
    FROM yourtable
    UNION ALL
    SELECT ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY (SELECT 0)) RNO,
    NAME+'_end',time_end
    FROM yourtable
)
SELECT 
MIN(CASE WHEN [DATECOL]='SUNDAY_begin' THEN time_begin END)SUNDAY_begin,
MIN(CASE WHEN [DATECOL]='SUNDAY_end' THEN time_begin END)SUNDAY_begin,
MIN(CASE WHEN [DATECOL]='MONDAY_begin' THEN time_begin END)MONDAY_begin,
MIN(CASE WHEN [DATECOL]='MONDAY_end' THEN time_begin END)MONDAY_end,
MIN(CASE WHEN [DATECOL]='TUESDAY_begin' THEN time_begin END)TUESDAY_begin,
MIN(CASE WHEN [DATECOL]='TUESDAY_end' THEN time_begin END)TUESDAY_end,
MIN(CASE WHEN [DATECOL]='WEDNESDAY_begin' THEN time_begin END)WEDNESDAY_begin,
MIN(CASE WHEN [DATECOL]='WEDNESDAY_end' THEN time_begin END)WEDNESDAY_end,
MIN(CASE WHEN [DATECOL]='THURSDAY_begin' THEN time_begin END)THURSDAY_begin,
MIN(CASE WHEN [DATECOL]='THURSDAY_end' THEN time_begin END)THURSDAY_end,
MIN(CASE WHEN [DATECOL]='FRIDAY_begin' THEN time_begin END)FRIDAY_begin,
MIN(CASE WHEN [DATECOL]='FRIDAY_end' THEN time_begin END)FRIDAY_end,
MIN(CASE WHEN [DATECOL]='SATURDAY_begin' THEN time_begin END)SATURDAY_begin,
MIN(CASE WHEN [DATECOL]='SATURDAY_end' THEN time_begin END)SATURDAY_end
FROM CTE
GROUP BY RNO

结果

由于SQLFiddle有内部错误,我正在添加结果图像。

这应该会为您提供所需的结果:

Select t1.Sunday Sunday_Begin, t2.Sunday Sunday_End, t1.Monday Monday_Begin, t2.Monday Monday_End, t1.Tuesday Tuesday_Begin, t2.Tuesday Tuesday_End, t1.Wednesday Wednesday_Begin, t2.Wednesday Wednesday_End, t1.Thursday Thursday_Begin, t2.Thursday Thursday_End, t1.Friday Friday_Begin, t2.Friday Friday_End, t1.Saturday Saturday_Begin, t2.Saturday Saturday_End

From

(Select RNo, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
from
(Select ROW_NUMBER() Over(Partition by name order by time_begin) RNo, name, time_begin
from #tbl) tb1
pivot
(min(time_begin) for name in (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)) as pv1) as t1

JOIN

(Select RNo, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
from
(Select ROW_NUMBER() Over(Partition by Name order by time_begin) RNo, name, time_end
from #tbl) tb2
pivot
(min(time_end) for name in (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)) as pv1) as t2 on t1.RNo = t2.RNo