将一行拆分为三行

split a row into three separate rows

我需要将 sql 行分成 3 行,下面是行:

Trim_Master_id  Batch_id    mobius_A_start_runtime  mobius_A_end_runtime    mobius_A_runtime    mobius_B_start_runtime  mobius_B_end_runtime    mobius_B_runtime    mobius_C_start_runtime  mobius_C_end_runtime    mobius_C_runtime    Mobius_A_Number_Used    Mobius_B_Number_Used    Mobius_C_Number_Used
2             BD190626-022             246                     250                  4                    92                        96                   4                      65                     69         4                             0                          0                       0

我想要这样的输出,同时添加一个设备 ID(其中 A = 1,B = 2,C =3):

Batch_id    epuipment_id    mobius_A_start_runtime  mobius_A_end_runtime    mobius_A_runtime    Mobius_A_Number_Used
Batch_id    equipment_id    mobius_B_start_runtime  mobius_B_end_runtime    mobius_B_runtime    Mobius_B_Number_Used
Batch_id    equipment_id    mobius_C_start_runtime  mobius_C_end_runtime    mobius_C_runtime    Mobius_C_Number_Used

最好的方法是什么,我尝试过交叉应用,但我认为我没有正确使用它:

    CASE
        WHEN t.mobius_b_number_used IS NULL AND t.mobius_c_number_used IS NULL THEN 1
        WHEN t.mobius_a_number_used IS NULL AND t.mobius_c_number_used IS NULL THEN 2
        WHEN t.mobius_a_number_used IS NULL AND t.mobius_b_number_used IS NULL THEN 3
    END AS ColGrp, (Select p.id from [phases] p
                        where p.batch in 
                            (select id from [batches] b
                                where b.[name] = Batch_id) and p.[type] = 4) PhaseID,
            t.[createdOn],
            GETDATE(),
            x.[mobius_A_start_runtime], 
            x.[mobius_A_end_runtime], 
            x.[Mobius_A_Number_Used],
            x.[mobius_B_start_runtime], 
            x.[mobius_B_end_runtime], 
            x.[Mobius_B_Number_Used],
            x.[mobius_C_start_runtime], 
            x.[mobius_C_end_runtime], 
            x.[Mobius_C_Number_Used],
            (select l.id FROM labour l
                where l.phase in 
                (Select p.id from [phases] p
                    where  p.[type] = 4 and l.created_at = p.created_at and p.batch in 
                        (select id from [batches] b
                        where b.[name] = Batch_id))) Labour
    FROM [Trim_Master] t
  CROSS APPLY
  (
    VALUES
      (t.[mobius_A_start_runtime], t.[mobius_A_end_runtime], t.[Mobius_A_Number_Used], NULL, NULL, NULL, NULL, NULL, NULL),
      (NULL, NULL, NULL, t.[mobius_B_start_runtime], t.[mobius_B_end_runtime], t.[Mobius_B_Number_Used], NULL, NULL, NULL),
      (NULL, NULL, NULL, NULL, NULL, NULL, t.[mobius_C_start_runtime], t.[mobius_C_end_runtime], t.[Mobius_C_Number_Used])
  ) AS x ([mobius_A_start_runtime], [mobius_A_end_runtime], [Mobius_A_Number_Used], [mobius_B_start_runtime], [mobius_B_end_runtime], [Mobius_B_Number_Used], [mobius_C_start_runtime], [mobius_C_end_runtime], [Mobius_C_Number_Used])
ORDER BY
  t.[mobius_A_start_runtime], t.[mobius_A_end_runtime], t.[Mobius_A_Number_Used] ASC,
  x.[mobius_A_start_runtime], x.[mobius_A_end_runtime], x.[Mobius_A_Number_Used] DESC,
  x.[mobius_B_start_runtime], x.[mobius_B_end_runtime], x.[Mobius_B_Number_Used] DESC,
  x.[mobius_C_start_runtime], x.[mobius_C_end_runtime], x.[Mobius_C_Number_Used] DESC
;

您可以按如下方式使用cross apply

select x.*
from mytable t
cross apply (values
    (Batch_id, 'A', mobius_A_start_runtime, mobius_A_end_runtime, mobius_A_runtime, Mobius_A_Number_Used),
    (Batch_id, 'B', mobius_B_start_runtime, mobius_B_end_runtime, mobius_B_runtime, Mobius_B_Number_Used),
    (Batch_id, 'C', mobius_C_start_runtime, mobius_C_end_runtime, mobius_C_runtime, Mobius_C_Number_Used)
) as x(Batch_id, equipment_id, mobius_start_runtime, mobius_end_runtime, mobius_runtime, Mobius_Number_Used)

Demo on DB Fiddle:

Batch_id     | equipment_id | mobius_start_runtime | mobius_end_runtime | mobius_runtime | Mobius_Number_Used
:----------- | :----------- | -------------------: | -----------------: | -------------: | -----------------:
BD190626-022 | A            |                  246 |                250 |              4 |                  0
BD190626-022 | B            |                   92 |                 96 |              4 |                  0
BD190626-022 | C            |                   65 |                 69 |              4 |                  0