2015-09-09 47 views
1

我想添加的所有第一名的成绩(cou_code)到第二结果(cou_code)产生的结果为:总和的2个SELECT语句throught SQL

CHANGES STATE_CODE 

---------- ---------- 
     29 01   
     2 16   
     2 02   
     1 05   
     3 06 

- 改变所有

select count(cou_code), st_code 
from sdrp15_cosd 
where st_code = (select distinct state_code 
        from sdrp15_submission_log 
        where state_code = st_code 
        and cou_code = 'All' 
        and phase = 'A' 
        and qa_date is null) 
and phase = 'A' 
group by st_code; 

产生的结果为:

COUNT(COU_CODE) ST_CODE 
--------------- ------- 
      157 01  
       1 11  
      180 16  
      61 02  
      1088 06  
+1

你忘了这个问题吗? –

回答

0

如果您需要从查询中求出count,请使用cte s来完成。

with x as (
select count(cou_code) as changes, state_code from sdrp15_submission_log sl 
where state_code in (select distinct state_code from sdrp15_submission_log 
         where state_code = sl.state_code 
         and cou_code != 'All') 
and qa_date is null 
and phase = 'A'      
group by state_code) 
, y as (select count(cou_code) as cnt, st_code 
from sdrp15_cosd 
where st_code = (select distinct state_code 
       from sdrp15_submission_log 
       where state_code = st_code 
       and cou_code = 'All' 
       and phase = 'A' 
       and qa_date is null) 
and phase = 'A' 
group by st_code) 
select x.state_code, x.changes+y.cnt 
from x join y on x.state_code = t.st_code