Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

SQL-Ansicht:Tabellen verknüpfen, ohne dass die Daten in jeder Zeile dupliziert werden?

In diesem Fall müssten Sie eine UNION (eigentlich UNION ALL) verwenden.

select a.name as account_Name, 
       p.description as property_DESCRIPTION, 
       p.address as property_ADDRESS, 
       null as vehicles_DESCRIPTION,
       null as vehicles_MAKE, 
       null as vehicles_MODEL
    from Accounts a
        inner join Properties p
            on a.accountid = p.accountid
UNION ALL   
select a.name as account_Name, 
       null as property_DESCRIPTION, 
       null as property_ADDRESS, 
       v.description as vehicles_DESCRIPTION,
       v.make as vehicles_MAKE, 
       v.model as vehicles_MODEL
    from Accounts a
        inner join vehicles v
            on a.accountid = v.accountid