簡體   English   中英

SQL在LEFT JOIN語句中的Case語句后連接多行

[英]SQL Concatenate multiple rows after Case statement inside LEFT JOIN statement

我有單獨的表格

Table1:
| AppID        | Desc | 
|--------------|------|
| 1            | App1 |
| 2            | App2 |

Table2:
| CollateralID | Type  | AppID |
|--------------|-------|-------|
| 1            | Home  | 1     |
| 2            | Condo | 1     |
| 3            | Condo | 2     |

Table3:
| TitleID | Address1 | Address2 | Address3 | CollateralID  |
|---------|----------|----------|----------|---------------|
| 1       | Add1a    | Add2a    | Add3a    |   1           |
| 2       | Add1b    | Add2b    | Null     |   2           |
| 3       | Add1c    | Add2c    | Null     |   3           |


Desired Results:
| AppID  | Desc | Type        | Complete Address |
|--------|------|-------------|------------------|
| 1      | App1 | Home; Condo | Add1a + Add2a + Add3a; Add1b + Add2b |
| 2      | App2 | Condo       | Add1c + Add2c |

到目前為止,這只是我得到的

SELECT  app.AppID                       AS AppID,
        app.Desc                        AS Desc,
        details.type                    AS Type,
        details.compadd                 AS "Complete Address"

FROM    Table1    app
LEFT JOIN (SELECT   tab2.AppID,
                    tab2.Type, 
                    
                    CASE WHEN tab2.Type = 'HOME' 
                         THEN tab3.Address1 || ' ' || tab3.Address2 || ' ' || tab3.Address3

                         WHEN tab2.Type = 'CONDO' 
                         THEN tab3.Address1 || ' ' || tab3.Address2 

                    END AS compadd      
            
            FROM  Table2 tab2 
            JOIN  Table3 tab3 ON tab2.CollateralID = tab3.CollateralID
            ) details ON app.AppdID = tab2.AppID

我錯過了什么,不明白如何申請:

如上所示,如何連接 AppID 的類型和完整地址?

感謝高級的幫助。 如果不清楚,請告訴我。 謝謝!

首先,您不需要subquery來實現您所需要的。 其次,使用upper()函數來確保你有完全匹配

select app.AppID as AppID
    , app.Desc as "Desc"
    , tab2.type as "Type"
    , case 
        when upper(tab2.Type) = 'HOME'  
            then tab3.Address1 || ' ' ||  tab3.Address2 ||  ' ' ||  tab3.Address3
        when upper(tab2.Type) = 'CONDO'
            then tab3.Address1 ||  ' ' ||  tab3.Address2 end as "Complete Address"
from Table1 app
left join Table2 tab2 on tab2.AppID = app.AppID
left join Table3 tab3 on tab3.CollateralID = tab2.CollateralID

您需要將 GROUP BY 子句與 STRING_AGG 函數一起使用。 像下面

SELECT  app.AppID AS AppID, app.Desc AS Desc, details.type AS Type, details.compadd AS "Complete Address"
FROM    Table1    app
LEFT JOIN 
(
SELECT   tab2.AppID, STRING_AGG(tab2.Type,';') Type, 
STRING_AGG(CASE WHEN tab2.Type = 'HOME' 
THEN tab3.Address1 || ' ' || tab3.Address2 || ' ' || tab3.Address3
WHEN tab2.Type = 'CONDO' 
THEN tab3.Address1 || ' ' || tab3.Address2 
END,';') AS compadd      
FROM  Table2 tab2 
JOIN  Table3 tab3 ON tab2.CollateralID = tab3.CollateralID
Group By tab2.AppID
) details ON app.AppdID = details.AppID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM