1

私はこのクエリを持っています:

select '2012-Nov-01' 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,
count(case 
    when trunc(init_dtime)-trunc(create_dtime) >= 1 
    then player_id 
  end) as Day_1_Ret,
count(case 
    when trunc(init_dtime)-trunc(create_dtime) >= 2
    then player_id 
  end) as Day_2_Ret,
count(case 
    when trunc(init_dtime)-trunc(create_dtime) >= 3
    then player_id 
  end) as Day_3_Ret
from player
where trunc(create_dtime) > To_Date('2012-Nov-01','yyyy-mon-dd')-7
and trunc(create_dtime) <= To_Date('2012-Nov-01','yyyy-mon-dd')
and world_flag != '1'
Which outputs:

Week_Of       Cohort_size    Day_0_Ret    Day_1 Ret    Day_2_Ret    Day_3_Ret
2012-Nov-01    2426           2426        1412         1349          1316

このクエリを複数の日付セットに対して実行できるようにしたいので、出力は次のようになります。

Week_Of       Cohort_size    Day_0_Ret    Day_1 Ret    Day_2_Ret    Day_3_Ret
2012-Nov-01    2426           2426        1412         1349          1316
2012-Nov-08    5106           2458        1945         1379          1248
2012-Nov-15    3580           1476        1412         1349          1146

の特定の週に毎回クエリを再実行する代わりに。これは可能ですか?

4

1 に答える 1

4

次のクエリは、ロジックをgroup bywhereではなくに変更します。

select to_char(max(create_dtime), 'yyyy-mm-dd') 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,
count(case 
    when trunc(init_dtime)-trunc(create_dtime) >= 1 
    then player_id 
  end) as Day_1_Ret,
count(case 
    when trunc(init_dtime)-trunc(create_dtime) >= 2
    then player_id 
  end) as Day_2_Ret,
count(case 
    when trunc(init_dtime)-trunc(create_dtime) >= 3
    then player_id 
  end) as Day_3_Ret
from player
where trunc(create_dtime) >= To_Date('2012-Nov-01','yyyy-mon-dd') - 6
and world_flag != '1'
group by trunc((trunc(create_dtime) - (To_Date('2012-Nov-01','yyyy-mon-dd') -  6))/7)
order by 1

週を説明するために利用可能な最後の日付を選択します。

于 2013-03-06T23:26:12.887 に答える