разделить строку на три отдельные строки - PullRequest
0 голосов
/ 05 мая 2020

Мне нужно разбить строку 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

Я хотел бы получить такой вывод, также добавив идентификатор оборудования (где 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
;

1 Ответ

1 голос
/ 05 мая 2020

Вы можете использовать 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)

Демо на 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
...