2015-09-09 76 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