简体   繁体   中英

Reference Temp Column in INNER JOIN

I have a temp column called CFADTemp that I am trying to use in a inner join to help me match up data from a different table.

SELECT 
    CustomerNumber, AddressLine1, AddressLine2, AddressLine3
FROM 
    CFAddress A
INNER JOIN 
    (SELECT 
         CustomerNumber, AddressType, 
         MAX((CASE 
                 WHEN AddressType = '3' THEN '4'
                 WHEN AddressType = '1' THEN '3'
                 WHEN AddressType = '4' THEN '2'
                 WHEN AddressType = '2' THEN '1'
              END) AS CFADTemp
    FROM 
        CFAddress
    GROUP BY 
        CustomerNumber, AddressType) B ON B.CustomerNumber = A.CustomerNumber 
                                       AND...

I need to join with CFADTemp and with the case when situation, not only with CustomerNumber

Does anyone know how to use a temp field to help make this inner join work so I can get the data I need?

Reference the name:

...
) B on B.CustomerNumber = A.CustomerNumber AND B.CFADTemp = A.AddressType

If you need the same 3=>4,1=3,4=>2,2=>1 mapping for A.AddressType , you'll need another CASE expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM