Oracle按日期范围分组

时间:2016-04-21 10:26:29

标签: oracle oracle11g

我有下表

+-----------+-------+-------+
| Date      | Type  | Value |
+-----------+-------+-------+
| 1/1/2013  | A     | 1     |
| 1/2/2013  | A     | 3     |
| 1/3/2013  | A     | 5     |
| 1/4/2013  | A     | 6     |
| 1/6/2013  | A     | 8     |
| 1/7/2013  | A     | 1     |
| 1/8/2013  | A     | 2     |
+-----------+-------+-------+

我想对某一天的前3个日期的值求和,所以我使用了这个查询。 即:sel_date = 1/3/2013。

select type, sum(value)
from table_name
where date <= seldate
and date > seldate - 3
group by type

现在的问题是,我想输出一个表,其中包含每个日期前3天的给定日期范围。

ie:sel_date range 1/3/2013 - 1/8/2013

+-----------+-------+------------+
| Date      | Type  | Sum(Value) |
+-----------+-------+------------+
| 1/3/2013  | A     | 9          | // 5 + 3 + 1
| 1/4/2013  | A     | 14         | // 6 + 5 + 3
| 1/5/2013  | A     | 11         | // 0 + 6 + 5
| 1/6/2013  | A     | 14         | // 8 + 0 + 6
| 1/7/2013  | A     | 9          | // 1 + 8 + 0
| 1/8/2013  | A     | 11         | // 2 + 1 + 8
+-----------+-------+------------+

有没有办法在单个查询中执行此操作。我尝试阅读分区,但它不会导致我在哪里。

4 个答案:

答案 0 :(得分:4)

在窗口条款中使用range between

select dt, type, value, 
       sum(value) over (order by dt range between 2 preceding and current row) as sv
  from t

测试数据和输出:

create table t (dt date, type varchar2(1), value number(5));
insert into t values (date '2013-01-01', 'A', 1);
insert into t values (date '2013-01-02', 'A', 3);
insert into t values (date '2013-01-03', 'A', 5);
insert into t values (date '2013-01-04', 'A', 6);
insert into t values (date '2013-01-05', 'A', 8);
insert into t values (date '2013-01-06', 'A', 1);
insert into t values (date '2013-01-07', 'A', 2);
insert into t values (date '2013-01-12', 'A', 2);

DT          TYPE  VALUE         SV
----------- ---- ------ ----------
2013-01-01  A         1          1
2013-01-02  A         3          4
2013-01-03  A         5          9
2013-01-04  A         6         14
2013-01-05  A         8         19
2013-01-06  A         1         15
2013-01-07  A         2         11
2013-01-12  A         2          2

答案 1 :(得分:2)

你可以尝试这样的事情:

with test(Date_, Type, Value ) as
(
  select to_date('01/01/2013', 'mm/dd/yyyy'), 'A', 1 from dual union all
  select to_date('01/02/2013', 'mm/dd/yyyy'), 'A', 3 from dual union all
  select to_date('01/03/2013', 'mm/dd/yyyy'), 'A', 5 from dual union all
  select to_date('01/04/2013', 'mm/dd/yyyy'), 'A', 6 from dual union all
  select to_date('01/05/2013', 'mm/dd/yyyy'), 'A', 8 from dual union all
  select to_date('01/06/2013', 'mm/dd/yyyy'), 'A', 1 from dual union all
  select to_date('01/07/2013', 'mm/dd/yyyy'), 'A', 2 from dual
)
select *
from (
        select date_, type,
               value + nvl(lag(value, 1) over (partition by type order by date_), 0)
                     + nvl(lag(value, 2) over (partition by type order by date_), 0) as value
        from test
)        
where date_ between to_date('01/03/2013', 'mm/dd/yyyy') and to_date('01/07/2013', 'mm/dd/yyyy')

根据日期,每行汇总前两个值;外部查询只是用于应用过滤器,因为在内部查询中应用它会导致错误的总和。

LAG用于从当前行之前的行读取1或2个位置的值。

答案 2 :(得分:1)

您可以使用:

IQueryable<XYZ> dalMemberList = dbContext.XYZ.Where(c => c.IsDeleted == false).GroupBy(g => g.CreatedDate).OrderByDescending(o => o.CreatedDate);

答案 3 :(得分:0)

`c`.`name`