SQL中的嵌套查询

时间:2014-04-26 08:35:48

标签: sql sql-server

这是我的问题:

SELECT Real_acct.[ACCOUNT]
  ,[Mailto]
  ,[Site_addr_1]
  ,[Site_addr_2]
  ,[Site_addr_3]
  ,[State_Class]
  ,[Map_Facet]
  ,[Key_Map]
  ,[Neighborhood_Code]
  ,[Neighborhood_Group]
  ,[Econ_Area]
  ,[Yr_Impr]
  ,[Acreage]
  ,[Prior_Total_Market_Value]
  ,[Land_Value]
  ,[Improvement_Value]
  ,[Extra_features_Value]
  ,[Total_Appraised_Value]
  ,[Total_Building_Area]
  ,[Econ_Bld_Class]
  ,[LAND_USE_CODE]
  ,[PROPERTY_NAME]
  ,[Building_other].[UNITS]
  ,[NET_RENT_AREA]
  ,[Related_Account_Number]
  ,[Agent_ID]
  ,[Agent_Name]

FROM [Pdata].[dbo].[Real_acct] 
    LEFT JOIN [Pdata].[dbo].[Land] ON 
            (Real_acct.ACCOUNT = LAND.ACCOUNT)
    LEFT JOIN [Pdata].[dbo].[Building_other] ON 
            (Real_acct.ACCOUNT = Building_other.ACCOUNT)
    LEFT JOIN [Pdata].[dbo].[Parcel_tieback] ON 
            (Real_acct.ACCOUNT = Parcel_tieback.ACCOUNT)
    LEFT JOIN [Pdata].[dbo].[Real_acct_agents] ON 
            (Real_acct.ACCOUNT = Real_acct_agents.ACCOUNT)

我想将此查询添加为嵌套:

SELECT [ACCOUNT]
  ,[BUILDING_NUMBER]
  ,[FIXTURE_TYPE]
  ,[FIXTURE_DESCRIPTION]
  ,[UNITS]
FROM [Pdata].[dbo].[Fixtures]
WHERE FIXTURE_TYPE = 'OWR'

使用左连接

我怎么能这样做,请帮助我。

1 个答案:

答案 0 :(得分:7)

如果我自己动手,你需要添加这个做你的查询(又名DrivedTable):

Left outer join(
SELECT [ACCOUNT]
  ,[BUILDING_NUMBER]
  ,[FIXTURE_TYPE]
  ,[FIXTURE_DESCRIPTION]
  ,[UNITS]
FROM [Pdata].[dbo].[Fixtures]
WHERE FIXTURE_TYPE = 'OWR') f on f.Account = Real_acct.ACCOUNT

基于评论

你可以根据需要重复这种模式,例如另一个模式有' WHT'在过滤器

Left outer join(
SELECT [ACCOUNT]
  ,[BUILDING_NUMBER]
  ,[FIXTURE_TYPE]
  ,[FIXTURE_DESCRIPTION]
  ,[UNITS]
FROM [Pdata].[dbo].[Fixtures]
WHERE FIXTURE_TYPE = 'WHT') f2 on f2.Account = Real_acct.ACCOUNT