多个子查询结构的问题

时间:2015-02-25 15:22:34

标签: sql

我试图在下面使用多个子查询,以便我可以使用(并最终求和)我的case语句别名。该查询运行正常,但它返回的provgrpcodes不限于' MIM'和' XIMD'例如。基本上我想将每个子查询视为应该的表或数据集。

SELECT t.px
 , t.provgrpcode
 , t.revctrdesc
 , t.pgrp_prov_combo
 , sum(t.CorrectedUnits) as measure

 from 
 (SELECT q.px
 , q.provgrpcode
 , q.revctrdesc
 , q.pgrp_prov_combo
 , q.postingdate
 , q.correcteddate
 , q.corrected
 , case when q.corrected = 'Y' and q.correcteddate Between '2015-01-01' And    '2015-02-01' and q.postingdate Between '2015-01-01' And '2015-02-01' and        q.txtype = 'c' then 0
   when q.corrected = 'y' and q.correcteddate Between '2015-01-01' And    '2015-02-01' and q.postingdate Not Between '2015-01-01' And '2015-02-01' and    q.txtype = 'c' then - q.rvuunitadjustment
   when q.txtype = 'c' then q.rvuunitadjustment
   else 0 end as CorrectedUnits
 from 
 (SELECT db1.px
 , db1.provgrpcode
 , db2.revctrdesc
 , prov.pgrp_prov_combo
 , db1.postingdate
 , db1.correcteddate
 , db1.corrected
 , db1.txtype
 , case when db1.procmod2='tc' then 0
   when db1.procmod1='tc' then 0
   when substring(db1.proccode,6,2)='tc' then 0
   else db1.totalunits
   end as RVUUnitAdjustment
 FROM Damariscotta..Damariscotta_Monthly_Data as db1 
 LEFT JOIN pp_reporting..RevenueCenter_union_v as db2 ON db1.px = db2.px 
 AND db1.revctrcode = db2.revctrcode
 LEFT JOIN pp_reporting..Providers prov ON db1.provcode = prov.pgrp_prov_cd 
 AND db1.px = prov.pgrp_practice
 )q
 )t
 WHERE px ='dmm' 
 AND postingdate Between '2015-01-01' And '2015-02-01' OR px='dmm' AND                correcteddate Between '2015-01-01' And '2015-02-01'
 --adding these for other queries
 AND revctrdesc like '%exam%'
 and provgrpcode in ('MIM','XIMD')
 group by px, provgrpcode, revctrdesc, revctrdesc, pgrp_prov_combo

谢谢 - 这有效

  WHERE
  (db1.px ='dmm' 
  AND postingdate Between '2015-01-01' And '2015-02-01') OR (db1.px='dmm'      AND correcteddate Between '2015-01-01' And '2015-02-01')
  )q
  )t
  WHERE 
  --px ='dmm' 
  --AND postingdate Between '2015-01-01' And '2015-02-01' OR px='dmm' AND    correcteddate Between '2015-01-01' And '2015-02-01'
  --adding these for other queries
  revctrdesc like '%exam%'
  and provgrpcode in ('MIM','XIMD')
  group by px, provgrpcode, revctrdesc, revctrdesc, pgrp_prov_combo

1 个答案:

答案 0 :(得分:0)

两件事。

首先,将where子句放在内部最多子查询中。这将提高您的查询效率。

第二个AND优先于或,所以这个:

WHERE px ='dmm' 
AND postingdate Between '2015-01-01' And '2015-02-01' OR px='dmm' AND                   correcteddate Between '2015-01-01' And '2015-02-01'
--adding these for other queries
AND revctrdesc like '%exam%'
and provgrpcode in ('MIM','XIMD')

等同于:

WHERE (px ='dmm' 
AND postingdate Between '2015-01-01' And '2015-02-01') OR (px='dmm' AND                   correcteddate Between '2015-01-01' And '2015-02-01'
--adding these for other queries
AND revctrdesc like '%exam%'
and provgrpcode in ('MIM','XIMD'))

这意味着如果

px ='dmm' 
AND postingdate Between '2015-01-01' And '2015-02-01'

为true,即使

,也会返回记录

px ='dmm'并在'2015-01-01'和'2015-02-01'之间纠正      - 为其他查询添加这些     AND revctrdesc喜欢'%exam%'     和'provgrpcode'('MIM','XIMD')

是假的。