Oracle中不同時間粒紋的日期約束

來源:互聯網
上載者:User

最近使用arcplan做報表,遇到不同日期粒度下展示資料的需求,利用sql直接從資料庫中查詢更為簡便,且複用性強,記錄下。

年粒度情況:展示當年前推5年的資料。

  1. t.d_day>= add_months(to_date('2012'||'0101', 'yyyymmdd'),-60)   
  2. and  t.d_day<= last_day(to_date('2012'||'1201', 'yyyymmdd'))  

半年粒度情況:展示當前半年前推12個資料點。

  1. d_day>=add_months(to_date(substr('2011H2', 1, 4)||decode(substr('2011H2', 6, 6),1,'0101',2,'0701'), 'yyyymmdd') ,-72)   
  2. and  t3.d_day<=to_date(substr('2011H2', 1, 4)||decode(substr('2011H2', 6, 6),1,'0630',2,'1231'), 'yyyymmdd')  

季粒度情況:展示當前季度前推12個資料點。

  1. t.d_day>=add_months(to_date(substr('2012Q1', 1, 4)||decode(substr('2012Q1', 6, 6),1,'0101',2,'0401',3,'0701',4,'1001'), 'yyyymmdd') ,-36)   
  2. and  t.d_day<=to_date(substr('2012Q1', 1, 4)||decode(substr('2012Q1', 6, 6),1,'0101',2,'0401',3,'0701',4,'1001'), 'yyyymmdd')  

月粒度情況:展示當前年所在12個月的資料,不足12個月前推。

  1. t.d_day<=last_day(to_date('201204   
  2. ', 'yyyymm')) and t.d_day>add_months(last_day(to_date('201204', 'yyyymm')),-12)  

旬粒度情況:展示當年季度前推12個點的資料。

  1. t.d_day>= to_date(to_char(add_months(to_date(substr('201201X3', 1, 6), 'yyyymm'), -ceil(11 / 3) ), 'YYYYMM')||decode( to_char(   
  2. case when to_number(substr('201201X3', 8, 1)) - mod(11, 3) > 0 THEN to_number(substr('201201X3', 8, 1)) - mod(11, 3)   
  3. else to_number(substr('201201X3', 8, 1)) - mod(11, 3) + 3 END),1,'01',2,'11',3,'21'),'yyyymmdd')   
  4. and t.d_day<= decode(substr('201201X3', 8, 8),1,to_date(substr('201201X3', 1, 6)||'10','yyyymmdd'),2,to_date(substr('201201X3', 1, 6)||'20','yyyymmdd'),3,last_day(to_date(substr('201201X3', 1, 6)||'01','yyyymmdd')))  

周粒度情況:展示當年周前12個資料點。

  1. t.d_day<= trunc(to_date(substr('2011W53', 1, 4) || '01' || '01', 'yyyymmdd'), 'IW') + 7 * to_number(substr('2011W53', 6, 2)) - 1     
  2. and t.d_day> trunc(to_date(substr('2011W53', 1, 4) || '01' || '01', 'yyyymmdd'), 'IW') + 7 * to_number(substr('2011W53', 6, 2)) - 1 - 12 * 7   

日粒度情況:展示當前月30天,不足30天前推30天。

  1. t.d_day<=to_date('20120326   
  2. ', 'yyyymmdd') and t.d_day>(to_date('20120326', 'yyyymmdd')-30)  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.