0

我有四个表 tblEmployees

员工 ID 姓名    
1 扎希兹
2尼贡
3 吉面
4 灰
5 丹妮

tbl 成员

成员没有名字
1 萨利姆
2贾米尔
3 贾子
4 船
5 乔恩
6 妈妈
RefSessions

SessionID 开始时间 结束时间
1 0701 0730
2 0731 0800
3 0801 0830
4 0831 0900
5 0901 0930
6 0931 1000
7 1001 1030
8 1031 1100
9 1101 1130
10 1131 1200
11 1201 1230
12 1231 1300
13 1301 1330
14 1331 1400
15 1401 1430
16 1431 1500
17 1501 1530
18 1531 1600
19 1601 1630
20 1631 1700
21 1701 1730
22 1731 1800
23 1801 1830
24 1831 1900
25 1901 1930
26 1931 2000
27 2001 2030
28 2031 2100

tblBookSession

BookingID SessionID EmployeeID MemberNo SessionDate
1 15 2 3 2012-09-30
2 16 2 3 2012-09-30
3 1 3 4 2012-10-03
4 2 3 4 2012-10-03
5 3 3 4 2012-10-03
6 4 3 4 2012-10-03

我正在寻找一个 t-sql 查询,导致针对特定日期的所述表格

它实际上是在健身房预订培训师的时间段并以日期报告的形式显示

4

1 回答 1

3

您将需要执行PIVOT. 使用 PIVOT 有两种方法可以做到这一点,一种是您对要转换的列进行编码的静态 Pivot,另一种是在执行时确定列的动态 Pivot。

静态枢轴,您将需要对值进行硬编码:

select name,
   IsNull([701-730], '') [701-730],
   IsNull([731-800], '') [731-800],
   IsNull([801-830], '') [801-830],
   IsNull([831-900], '') [831-900]
from
(
  select e.name,
    cast(e.starttime as varchar(10))+'-'
      +cast(e.endtime as varchar(10)) Session,
    m.FirstName
  from
  (
    select *
    from tblEmployees e
    cross apply RefSessions
  ) e
  left join tblBookSession b
    on e.EmployeeID = b.EmployeeID
    and e.sessionid = b.sessionid
  left join tblMembers m
    on b.MemberNo = m.MemberNo
) x
pivot
(
  max(FirstName)
  for session in ([701-730], [731-800], [801-830], [831-900]) -- additional sessions here
)p

请参阅带有演示的 SQL Fiddle

如果您有许多值要转换为列或未知数量的值,那么您将需要使用动态 sql 版本PIVOT

DECLARE @cols AS NVARCHAR(MAX),
    @colsPivot AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' 
                      + QUOTENAME(cast(starttime as varchar(10))
                                  +'-'+cast(endtime as varchar(10))) 
                    from RefSessions
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')



select @colsPivot = STUFF((SELECT ', IsNull(' 
                      + QUOTENAME(cast(starttime as varchar(10))
                          +'-'+cast(endtime as varchar(10))) +', '''') as [' +
                           cast(starttime as varchar(10))
                                  +'-'+cast(endtime as varchar(10)) + ']'
                    from RefSessions
                    group by SessionID, starttime, endtime
                    order by SessionID 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')



set @query = 'SELECT name, ' + @colsPivot + ' from 
             (
                  select e.name,
                    cast(starttime as varchar(10))+''-''
                      +cast(endtime as varchar(10)) Session,
                    m.FirstName
                  from
                  (
                    select *
                    from tblEmployees e
                    cross apply RefSessions
                  ) e
                  left join tblBookSession b
                    on e.EmployeeID = b.EmployeeID
                    and e.sessionid = b.sessionid
                  left join tblMembers m
                    on b.MemberNo = m.MemberNo
            ) x
            pivot 
            (
                max(FirstName)
                for Session in (' + @cols + ')
            ) p '

execute(@query)

请参阅带有演示的 SQL Fiddle

于 2012-10-10T11:06:30.740 回答