2013-03-06 129 views
1

我有這兩個單獨的查詢:的Oracle SQL - 從合併這兩個查詢兩個結果轉換成一個

查詢1

select '2013-03-03' As "Week_Of", 
count(player_id) as cohort_size 
from player 
where trunc(create_dtime) > To_Date('2013-Mar-03','yyyy-mon-dd')-7 
and trunc(create_dtime) <= To_Date('2013-Mar-03','yyyy-mon-dd') 
and world_flag != '1' 
; 

,輸出:

Week_of  Cohort_size 
2013-03-03  18183 

查詢2

select '2013-03-03' As "Week_Of", 
count(player_id) as Day_0_Ret 
from player 
where trunc(init_dtime)-trunc(create_dtime) >= 0 
and trunc(create_dtime) > To_Date('2013-Mar-03','yyyy-mon-dd')-7 
and trunc(create_dtime) <= To_Date('2013-Mar-03','yyyy-mon-dd') 
and world_flag != '1' 
; 

哪些產出:

Week_of  Day_0_Ret 
2013-03-03  15684 

我想帶這兩個查詢在一起,所以我有一個查詢輸出:

Week_of   Cohort_Size  Day_0_Ret 
2013-03-03  18183    15684 

回答

1

使用case語句來完成一個條件計數:

select '2013-03-03' As "Week_Of", 
count(player_id) as cohort_size , 
count(case 
     when trunc(init_dtime)-trunc(create_dtime) >= 0 
     then player_id 
     end) as Day_0_Ret 
from player 
where trunc(create_dtime) > To_Date('2013-Mar-03','yyyy-mon-dd')-7 
and trunc(create_dtime) <= To_Date('2013-Mar-03','yyyy-mon-dd') 
and world_flag != '1' 
;