在这种情况下我应该使用子查询还是求和?

时间:2016-12-17 03:09:11

标签: sql sql-server tsql subquery

我想要获得第11行(可以有多个房子不同区域的平方镜头数量的结果,如门廊,车库和包括生活区域),如果可能的话减去第10行以获得区域的总面积在生活区以外的房子里。

as a-- sum(id1.[calc_area] - pp.living_area) as [other_ area],

我的问题是两个数字来自不同的表,select语句使用不同的frompv。最简单的方法是什么?

select distinct pv.prop_id,
pv.hood_cd as neighborhood,
pv.abs_subdv_cd as subdivision,
cast (pv.[legal_desc] as char(16)) as legal,
[deed_date],
[consideration],
pv.prop_val_yr as year,
sts1.[situs_num] as address,
cast(sts1.[situs_street] as char(11)) as street,
pp.living_area,
id1.[calc_area] as [total_area],
cast (pp.[land_total_acres]as decimal (6,2))as acres,
[sale_type],
case when [sale_date] >='01/01/2014'then convert(varchar(18), [sale_date], 101)else''end as'sale date',
pp.ls_table,
(pv.land_hstd_val + pv.land_non_hstd_val + pv.ag_market + pv.timber_market)as land_val,
cast(pp.[main_land_total_adj]as decimal (5,2)) as land_adj_total,
(pv.imprv_hstd_val + pv.imprv_non_hstd_val)as imprv_val,
case when [sale_date] >='01/01/2014'then [sale_price] else 0 end as'sale price',
pv.market
from property_val pv with (nolock)        
inner join prop_supp_assoc psa with (nolock) on
       pv.prop_id = psa.prop_id
       and pv.prop_val_yr = psa.owner_tax_yr
       and pv.sup_num = psa.sup_num
inner join property p with (nolock)on
       pv.prop_id = p.prop_id
inner join owner o with (nolock) on
       pv.prop_id = o.prop_id
       and pv.prop_val_yr = o.owner_tax_yr
       and pv.sup_num = o.sup_num
inner join account ac with (nolock) on
       o.owner_id = ac.acct_id
inner join property_profile pp with (nolock) on
      pv.prop_id = pp.prop_id
      and pv.prop_val_yr = pp.prop_val_yr
left outer join imprv_detail as id1 with (nolock) on
      pv.prop_id = id1.prop_id
      and pv.prop_val_yr =  id1.prop_val_yr 
      and pv.sup_num = id1.sup_num
left outer join
       (select cop.prop_id,
        convert(varchar(20), co.deed_dt, 101) as deed_date,
        co.consideration as consideration, s.sl_dt as sale_date,
        s.sl_price as sale_price, s.sl_type_cd as sale_type
              from chg_of_owner_prop_assoc cop with (nolock)
              inner join chg_of_owner co with (nolock) on
                     co.chg_of_owner_id = cop.chg_of_owner_id
              inner join sale s with (nolock) on
                     co.chg_of_owner_id = s.chg_of_owner_id
              where cop.seq_num = 0
              )as c
              on c.prop_id = pv.prop_id

隐藏了一些列的基本结果-----

prop_id  address   street       living_area total_area acres
x        322       SURBER ST    939             48         0
x        322       SURBER ST    939            288         0
x        322       SURBER ST    939            939         0
xy       318       SURBER STRE  1202             0         0
xy       318       SURBER STRE  1202           120         0
xy       318       SURBER STRE  1202           340         0
xy       318       SURBER STRE  1202          1052         0

1 个答案:

答案 0 :(得分:0)

如果您只需要额外的总和以及当前返回的所有行,那么请使用SUM OVER函数:

sum(id1.[calc_area] - pp.living_area) over (PARTITION BY pv.prop_id, pv.prop_val_yr) as [other_ area]

您可以修改分区,例如:

sum(id1.[calc_area] - pp.living_area) over (PARTITION BY pv.prop_id, pv.prop_val_yr, pp.ls_table) as [other_ area]

但是如果在连接表时有不需要的行乘法,并且需要此表中的聚合值,则在OUTER APPLY子句中使用子查询(并且不以其他方式加入此表),例如:

SELECT ...
, id1.total_area
...
OUTER APPLY (
  SELECT Sum(calc_area) as total_area
  FROM imprv_detail
  WHERE prop_id = pv.prop_id = id1.
  and prop_val_yr = pv.prop_val_yr
  and sup_num = pv.sup_num
  ) AS id1  

然后计算other_area使用total_area - total_living_area