通过数据透视查询t-sql组

时间:2015-01-29 12:15:15

标签: sql sql-server tsql select pivot

我有一个结果集

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查询中实现这一点?

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

2 个答案:

答案 0 :(得分:1)

您需要获取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

<强> RESULT

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

enter image description here

答案 1 :(得分:0)

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

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