2

我有以下表格和数据

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 获得此功能?

4

2 回答 2

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

于 2013-09-10T10:13:29.287 回答
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;

输出:

+------------+------------+------------------+---- ------------+--------------+
| 日期 | 班次1 | 班次2 | 班次3 | 班次4 |
+------------+------------+------------------+---- ------------+--------------+
| 2013-09-10 | 约翰·史密斯 | 彼得,莫莉,亚当 | 迈克尔,亨利 | 吉姆,金,尼克 |
+------------+------------+------------------+---- ------------+--------------+

这是SQLFiddle演示

于 2013-09-10T10:23:44.120 回答