不使用pivot的子查询

时间:2015-01-07 12:29:04

标签: sql sql-server 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

它会给出enter image description here之类的结果。驱动程序也不是来自主表,总计也是空的。关于枢轴有什么特别之处,我不知道。

1 个答案:

答案 0 :(得分:1)

我建议您对代码进行一些更改。首先,您可以加入子查询中的Employee表。其次,您不需要子查询中的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 |