Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8
CH-0000001 IN-0000001 IN-0000001 ALL NULL NULL NULL NULL
CH-0000001 PR-0000001 NULL NULL PR-0000001 ALL NULL NULL
CH-0000001 PU-0000001 NULL NULL NULL NULL PU-0000001 ALL
嘿伙计们我试图根据密钥Col1
将所有这些信息压缩成一行。我看过一个使用MAX
和SUM
的技巧,但它似乎只适用于数字。这是期望的结果。
Col1 Col3 Col4 Col5 Col6 Col7 Col8
CH-0000001 IN-0000001 ALL PR-0000001 ALL PU-0000001 ALL
此外,这个问题来自LEFT JOIN
来自一个主密钥表的3个表。如果我达到这种状态的方法是解决这个问题的错误方法,请告诉我。这是查询
SELECT * FROM GCL_Blackboard A
LEFT JOIN GCL_Investors B ON A.Adjective_ID = B.Investor_ID
LEFT JOIN GCL_Loan_Programs C ON A.Adjective_ID = C.Program_ID
LEFT JOIN GCL_Loan_Purposes D ON A.Adjective_ID = D.Purpose_ID
答案 0 :(得分:2)
我认为这是你想要的逻辑:
SELECT a.col1,
max(coalesce(b.col2, c.col2, d.col2)) as col2,
max(coalesce(b.col3, c.col3, d.col3)) as col3,
max(coalesce(b.col4, c.col4, d.col4)) as col4,
max(coalesce(b.col5, c.col5, d.col5)) as col5,
max(coalesce(b.col6, c.col6, d.col6)) as col6,
max(coalesce(b.col7, c.col7, d.col7)) as col7,
max(coalesce(b.col8, c.col8, d.col8)) as col8
FROM GCL_Blackboard A LEFT JOIN
GCL_Investors B
ON A.Adjective_ID = B.Investor_ID LEFT JOIN
GCL_Loan_Programs C
ON A.Adjective_ID = C.Program_ID LEFT JOIN
GCL_Loan_Purposes D
ON A.Adjective_ID = D.Purpose_ID
GROUP BY a.col1;
查询有点猜测,因为我不知道列的来源。
答案 1 :(得分:1)
你可以这样做:
SELECT A.Col1,
MIN(Col3) Col3,
MIN(Col4) Col4 ....
FROM GCL_Blackboard A
LEFT JOIN GCL_Investors B ON A.Adjective_ID = B.Investor_ID
LEFT JOIN GCL_Loan_Programs C ON A.Adjective_ID = C.Program_ID
LEFT JOIN GCL_Loan_Purposes D ON A.Adjective_ID = D.Purpose_ID
GROUP BY A.Col1