如何比较列中的特定数字并在另一列中驱动它们各自的关系

时间:2017-02-12 18:03:36

标签: sql sql-server

我在表格中有以下列

TicketNumber    MachineNo.    Time
456                 11        10/02/2017 7:12
789                 13        10/02/2017 8:12
456                 22        10/02/2017 11:13
123                 11        10/02/2017 9:59
789                 24        10/02/2017 9:12
123                 22        10/02/17 11:59

票证号码在表格中重复两到三次,并且首先与机器号码相关联。有奇数(限制为11,13),然后它将链接到偶数(限制为22,24)以获得以下结果。

TicketNumber    Machineno.   TimeIn             Machineno.   Timeout
456                 11       10/02/2017 7:12    22           10/02/2017 11:13
789                 13       10/02/2017 8:12    24           10/02/2017 9:12
123                 11       10/02/2017 9:59    22           110/02/17 11:59

2 个答案:

答案 0 :(得分:0)

这样的东西?

class User:
    def __init__(self, username, email, password, 
        firstname, lastname, phone):

        self.username = username
        self.email = email
        self.password = password
        self.firstname = firstname
        self.lastname = lastname
        self.phone = phone

user=User('x','y','z','f','v','c')
print(vars(user))

enter image description here

答案 1 :(得分:0)

这看起来像一个直接的支点。

SELECT ticketNumber,
       MAX(CASE WHEN MachineNo IN (11,13) THEN machineno END) machineno_in,
       MAX(CASE WHEN MachineNo IN (11,13) THEN Time END) time_in,
       MAX(CASE WHEN MachineNo IN (22,24) THEN machineno END) machineno_out,
       MAX(CASE WHEN MachineNo IN (22,24) THEN Time END) time_out
FROM   YourTable
WHERE MachineNo IN (11,13,22,24)
GROUP  BY ticketNumber;