子查询不适用于数据透视表

Subqueries not working with pivot

我是 pivot 的新手,不知道为什么子查询在其中不起作用。查询的动机是查找当天没有班次。

SELECT [DriverId],
  (SELECT [RegistrationNo]+' '+[Name] FROM [dbo].[Employee] where [id]=[DriverId]) as [Driver],
  coalesce([1],0) as [1], coalesce([2],0)as [2],
  coalesce([3],0)as [3], coalesce([4],0)as [4],
  coalesce([5],0)as [5], coalesce([6],0)as [6],
  coalesce([7],0)as [7], coalesce([8],0)as [8],
  coalesce([9],0)as [9], coalesce([10],0)as [10],
  coalesce([11],0)as [11], coalesce([12],0)as [12],
  coalesce([13],0)as [13], coalesce([14],0)as [14],
  coalesce([15],0)as [15], coalesce([16],0)as [16],
  coalesce([17],0)as [17], coalesce([18],0)as [18],
  coalesce([19],0)as [19], coalesce([20],0)as [20],
  coalesce([21],0)as [21], coalesce([22],0)as [22],
  coalesce([23],0)as [23], coalesce([24],0)as [24],
  coalesce([25],0)as [25], coalesce([26],0)as [26],
  coalesce([27],0)as [27], coalesce([28],0)as [28],
  coalesce([29],0)as [29], coalesce([30],0)as [30],
  coalesce([31],0)as [31],([24] +[25]) as Total
FROM (
  -- SELECT (SELECT [RegistrationNo]+' '+[Name] FROM [dbo].[Employee] where [id]=[DriverId]) as [Driver]
 select [DriverId]
      ,count(day([AttendanceDateTime])) as [No Of Shifts]
      ,day([AttendanceDateTime]) as [day]
  FROM [BusOprtn].[dbo].[DriverAttendance] where [AttendanceDateTime] between '2014-12-01 20:12:48.000' and '2014-12-31 20:12:48.000' group by DriverId ,day([AttendanceDateTime]) 
) as s
PIVOT
(
    sum([No Of Shifts])
    FOR [day] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31])
)AS pivota

它给出这样的结果。驱动程序也不是来自 master table 并且 total 也为空。 pivot有什么我不知道的特别之处吗

我建议对您的代码进行一些更改。首先,您可以在子查询中加入 Employee table。其次,您不需要子查询中的 count - 这都可以由 PIVOT 聚合处理。第三,我建议使用窗口函数来获取每个驱动程序的总数。通过这些更改,您的代码将是:

select 
  DriverId,
  Driver,
  [1], [2], [3], [4], [5], [6], --- other columns here
  Total
from 
(
  select 
    da.driverid,
    driver = cast(e.RegistrationNo as varchar(50)) +' '+ e.Name,
    [day] = day(da.AttendanceDateTime),
    Total = count(*) over(partition by da.driverid)
  from Employee e
  inner join DriverAttendance da
    on e.id = da.driverid
) d
pivot
(
  count([day])
  for [day] in ([1], [2], [3], [4], [5], [6]) --- other columns here
) p;

参见 SQL Fiddle with Demo。使用以下示例数据:

CREATE TABLE DriverAttendance
    ([DriverID] int, [AttendanceDateTime] datetime)
;

INSERT INTO DriverAttendance
    ([DriverID], [AttendanceDateTime])
VALUES
    (40, '2015-01-01 00:00:00'),
    (40, '2015-01-02 00:00:00'),
    (523, '2014-12-5 00:00:00'),
    (523, '2014-12-6 00:00:00'),
    (640, '2014-12-5 00:00:00')
;

CREATE TABLE Employee
    ([ID] int, [RegistrationNo] int, [Name] varchar(5))
;

INSERT INTO Employee
    ([ID], [RegistrationNo], [Name])
VALUES
    (40, 123, 'Frank'),
    (523, 456, 'Jim'),
    (640, 789, 'Bob')
;

您将得到以下结果:

| DRIVERID |    DRIVER | 1 | 2 | 3 | 4 | 5 | 6 | TOTAL |
|----------|-----------|---|---|---|---|---|---|-------|
|       40 | 123 Frank | 1 | 1 | 0 | 0 | 0 | 0 |     2 |
|      523 |   456 Jim | 0 | 0 | 0 | 0 | 1 | 1 |     2 |
|      640 |   789 Bob | 0 | 0 | 0 | 0 | 1 | 0 |     1 |