2017-09-22 13 views
0

によってグループ化すると、合計は、コードの1日は次のようになり、日によって基準やグループに基づいて、最後の30日間です:サミングは、私がやろうとしている何の日

select 
sum(case when f.hire_date__c between '2017-08-01 00:00:00' and '2017-09-01 
00:00:00' 
and t.createddate between '2017-08-01 00:00:00' and '2017-09-01 00:00:00' 
and t.name = 'Request' then 1 else 0 end) as Requests 
      from case_task_c as t 
      join case_file_c as f 
      on f.id = t.case_file__c 

I今日の日付などに基づいて30日のルックバックに応じて日付を調整することができます。私が把握できないのは、毎日、すなわち昨日の結果、前日など、調整された日付範囲。 ' - 間隔-30日はCURRENT_DATEただ..アップ現在の日付を探している

select 
date(cast(f.hire_date__c as date)), 
row_number() over (order by f.hire_date__c desc) as rownumber, 
rr.Cancels as Cancels, 
qq.hires as hires, 
sum(rr.Cancels) over (rows between 1 following and 30 following) as 
CumulCancel, 
sum(qq.Hires) over (rows between 1 following and 30 following) as Hires 
from case_file_c as f 
    left join(
      select 
      cast(f.hire_date__c as date) as date1, 
      sum(case when 
      t.name = 'Cancellation Request' then 1 else 0 end) as Cancels 
      from case_task_c as t 
      join case_file_c as f 
      on f.id = t.case_file__c 
      group by date1) 
      as rr 
      on rr.date1 = cast(f.hire_date__c as date) 
    left join(
      select 
      cast(f.hire_date__c as date) as date2, 
      sum(case when f.hire_date__c is not null then 1 else 0 end) as 
      hires 
      from sf_case_file_c as f 
      group by date2) as qq 
      on qq.date2 = cast(f.hire_date__c as date) 
where f.hire_date__c is not null 
and f.hire_date__c >= '2017-01-01 00:00:00' 
and f.hire_date__c between date_add('day',-30,current_date) and current_date 
group by f.hire_date__c, rr.Cancels, qq.hires 
order by f.hire_date__c desc 

でも使用:

は、これまでのところ私は、これは持っています。

Postgresの使用8.0.2

+0

により、グループ内の列選択中の日付に日時を変換しますがされていませんか? – phroureo

+0

'group by date(キャスト(日付としてf.hire_date__c))、rr.Cancels、qq.hires' – Rakib

答えて

0

以下のようにグループを使用してください。あなたは例のスキーマを作成して、あなたの元の質問でそれをリンクするhttps://sqlfiddle.comを使用することはでき

GROUP BY date(cast(f.hire_date__c as date)),rr.Cancels, qq.hires