将枢轴帮我获得这个结果

时间:2013-09-10 09:42:08

标签: sql

我有以下表格和数据

DutyRoaster

╔═══════════════╦═════════════════════════╦═════════╦════════════╗
║  DutyRecordId ║        DutyDate         ║ ShiftNo ║ EmployeeId ║
╠═══════════════╬═════════════════════════╬═════════╬════════════╣
║             1 ║ 2013-09-10 00:00:00.000 ║       1 ║          2 ║
║             2 ║ 2013-09-10 00:00:00.000 ║       1 ║         10 ║
║             3 ║ 2013-09-10 00:00:00.000 ║       2 ║          7 ║
║             4 ║ 2013-09-10 00:00:00.000 ║       2 ║          9 ║
║             5 ║ 2013-09-10 00:00:00.000 ║       2 ║          4 ║
║             6 ║ 2013-09-10 00:00:00.000 ║       3 ║         12 ║
║             7 ║ 2013-09-10 00:00:00.000 ║       3 ║          5 ║
║             8 ║ 2013-09-10 00:00:00.000 ║       4 ║          3 ║
║             9 ║ 2013-09-10 00:00:00.000 ║       4 ║          1 ║
║            10 ║ 2013-09-10 00:00:00.000 ║       4 ║         13 ║
╚═══════════════╩═════════════════════════╩═════════╩════════════╝

我有另一张包含emplyeedIdName的表格 我想要一个这样的结果:

╔══════════════════════════════════════════════════════╗
║  Date        Shiftno1  Shiftno 2   Sitno 3 Shiftno4  ║
╠══════════════════════════════════════════════════════╣
║                                                      ║
║     10-02-1203  Jhon      peter       Micheal  Jim   ║
║                 Smith     Molly       Henry    Kim   ║
║                           Adam                 Nick  ║
║                                                Kaity ║
╚══════════════════════════════════════════════════════╝

是否可以使用Pivot获取此功能?

2 个答案:

答案 0 :(得分:0)

对于SQL Server(或PostgreSQL),您可以这样做:

with cte as (
    select
        d.DutyDate,
        d.ShiftNo,
        e.Name, 
        row_number() over(partition by d.ShiftNo order by d.DutyRecordId) as rn
    from DutyRoaster as d
        inner join Employees as e on e.Id = d.EmployeeId
)
select
    case when c.rn = 1 then c.DutyDate end as DutyDate,
    min(case when c.ShiftNo = 1 then c.Name end) as ShiftNo1,
    min(case when c.ShiftNo = 2 then c.Name end) as ShiftNo2,
    min(case when c.ShiftNo = 3 then c.Name end) as ShiftNo3,
    min(case when c.ShiftNo = 4 then c.Name end) as ShiftNo4
from cte as c
group by c.DutyDate, c.rn
order by c.DutyDate, c.rn

<强> sql fiddle demo

答案 1 :(得分:0)

如果是 MySql ,您可以使用动态SQL。 假设(来自您发布的所需输出)是您希望每个日期有一行。

SET @sql = NULL;

SELECT GROUP_CONCAT(DISTINCT CONCAT('GROUP_CONCAT(CASE WHEN shiftno = ', shiftno,
                           ' THEN e.name END) `shift', shiftno, '`'))
  INTO @sql
  FROM DutyRoaster;

SET @sql = CONCAT('SELECT DATE(DutyDate),', @sql,
                  '  FROM DutyRoaster r JOIN employees e
                       ON r.employeeId = e.employeeId
                    GROUP BY DATE(DutyDate)');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

输出:

+------------+------------+------------------+---------------+--------------+
| date       | shift1     | shift2           | shift3        | shift4       |
+------------+------------+------------------+---------------+--------------+
| 2013-09-10 | Jhon,Smith | Peter,Molly,Adam | Micheal,Henry | Jim,Kim,Nick |
+------------+------------+------------------+---------------+--------------+

这是 SQLFiddle 演示