metode Anda saat ini tidak terlalu efisien. Orang lain biasanya akan menggunakan CASE WHEN
untuk melakukannya.
SELECT t.uniqueID,
IN_Info1 = MAX(case when t.type = 'IN' then t.information1 end),
IN_Info2 = MAX(case when t.type = 'IN' then t.information2 end),
IN_Notes = MAX(case when t.type = 'IN' then t.Notes end),
OUT_Info1 = MAX(case when t.type = 'OUT' then t.information1 end),
OUT_Info2 = MAX(case when t.type = 'OUT' then t.information2 end),
OUT_Notes = MAX(case when t.type = 'OUT' then t.Notes end)
FROM TABLEB t
GROUP BY t.uniqueID
dan kemudian untuk dimasukkan ke dalam kueri besar Anda, Anda dapat menggunakan CTE atau TABEL TURUN
-- CTE
; with Tblb as
(
SELECT t.uniqueID,
IN_Info1 = MAX(case when t.type = 'IN' then t.information1 end),
IN_Info2 = MAX(case when t.type = 'IN' then t.information2 end),
IN_Notes = MAX(case when t.type = 'IN' then t.Notes end),
OUT_Info1 = MAX(case when t.type = 'OUT' then t.information1 end),
OUT_Info2 = MAX(case when t.type = 'OUT' then t.information2 end),
OUT_Notes = MAX(case when t.type = 'OUT' then t.Notes end)
FROM TABLEB t
GROUP BY t.uniqueID
)
select *
from TableA a
inner join Tblb b ON a.uniqueID = b.uniqueID
Anda tidak dapat melakukan ini X1.t1.uniqueID.
, seharusnya hanya X1.uniqueID