Select语句返回每行的所有ID值

时间:2013-08-14 08:32:28

标签: sql select join duplicates row

此select语句返回每行timeid的所有值。 连接使用正确的唯一值连接。 我已经尝试了内部连接/左外/右外部和完全也选择明显无济于事。

SELECT
    timed.timeid AS [Time Number], 
    emp.firstname AS [First Name], 
    emp.surname AS [Surame], 
    emp.empid AS [Employee Number], 
    timed.ver AS [Ver],
    timed.tdate AS [Date], 
    shifts.code AS [Code], 
    timed.stime AS [Start], 
    timed.etime AS [End], 
    DATEDIFF(HOUR, timed.stime, timed.etime) AS [Hours]
FROM 
    emp 
    INNER JOIN timed ON emp.divid = timed.divid
    INNER JOIN shifts ON timed.shiftid = shifts.shiftid     

Time Number First Name  Surame  Employee Number Ver     Date    Code      Start        End     Hours
OPS93041        C        BLOGS     G100         142 30/04/2013  Nts     19:00:00    06:00:00    11
OPS93041        L        SMITH     I101         142 30/04/2013  Nts     19:00:00    06:00:00    11
OPS93041        M        WALKER    M115         142 30/04/2013  Nts     19:00:00    06:00:00    11
OPS93041        P        DJANGO    P965         142 30/04/2013  Nts     19:00:00    06:00:00    11

正如上面结果的一个小例子所示,它是将timeid添加到所有结果中,也可能是除emp表中的其他值之外的其他值。

所需的输出就像是。

Time Number First Name  Surame  Employee Number Ver     Date    Code      Start        End     Hours
OPS93041        C        BLOGS     G100         142 30/04/2013  Nts     19:00:00    06:00:00    11
JDY73592        L        SMITH     I101         142 24/03/2013  Nts     11:00:00    17:00:00    6
QJA74057        M        WALKER    M115         188 08/07/2013  Nts     08:00:00    15:00:00    7
PDG03845        P        DJANGO    P965         125 19/01/2013  Nts     22:00:00    03:00:00    5

1 个答案:

答案 0 :(得分:0)

这是通过员工对数据进行分组来解决的,因此时间与员工行相关而不是班次号