首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

依据名字和日期分组统计,求高手观望!

2012-11-15 
根据名字和日期分组统计,求高手观望!!!ORACLE数据库中,根据输入的指定日期,从数据表中获取指定日期所在月

根据名字和日期分组统计,求高手观望!!!
ORACLE数据库中,根据输入的指定日期,从数据表中获取指定日期所在月从月初到指定日期的所有日期的销售量小计。以店分组,并将没有销售量的日期,赋值为0数据表内容如下:
店名 --- 销售日期
A110 --- 2012-09-28
A100 --- 2012-07-09
A110 --- 2012-10-01
A110 --- 2012-10-01
A110 --- 2012-10-03
A110 --- 2012-10-05
A120 --- 2012-10-01 
A000 --- 2012-10-03
--------------------------------------------------
如果输入的日期是2012-10-03就统计从2012-10-01到2012-10-03的各店在每一天分别的销售量:查询结果如下:
点名 --- 销售日期 销售量
A110 --- 2012-10-01 -- 2
A110 --- 2012-10-02 -- 0
A110 --- 2012-10-03 -- 1
A120 --- 2012-10-01 -- 1
A120 --- 2012-10-02 -- 0
A120 --- 2012-10-03 -- 0
A000 --- 2012-10-01 -- 0
A000 --- 2012-10-02 -- 0
A000 --- 2012-10-03 -- 1

跪求帮忙,摆脱!!!
想要的是查询的SQL语句,能不用PL/SQL的就尽量不用,我是初级菜鸟。

[解决办法]
按照常规思路写了一个 构造点名日期表 关联销售表统计数量 参数:2012-10-03

SQL code
with t1 as(     select 'A110' c1,date'2012-09-28' c2 from dual     union all     select 'A100' c1,date'2012-07-09' c2 from dual     union all     select 'A110' c1,date'2012-10-01' c2 from dual     union all     select 'A110' c1,date'2012-10-01' c2 from dual     union all     select 'A110' c1,date'2012-10-03' c2 from dual     union all     select 'A110' c1,date'2012-10-05' c2 from dual     union all     select 'A120' c1,date'2012-10-01' c2 from dual     union all     select 'A000' c1,date'2012-10-03' c2 from dual)select c.c1,c.t_date,count(d.c2) c_numfrom (select a.c1,b.t_datefrom   (    select distinct c1    from t1    where c2 between trunc(date'2012-10-03','month') and date'2012-10-03'  ) a,  (    select trunc(date'2012-10-03','month')+level-1 t_date    from dual    connect by level <= date'2012-10-03'-trunc(date'2012-10-03','month')+1  ) b) c left join t1 d on c.c1 = d.c1 and c.t_date = d.c2group by c.c1,c.t_dateorder by c.c1,c.t_date     c1      t_date     c_num--------------------------------------1    A000    2012/10/1    02    A000    2012/10/2    03    A000    2012/10/3    14    A110    2012/10/1    25    A110    2012/10/2    06    A110    2012/10/3    17    A120    2012/10/1    18    A120    2012/10/2    09    A120    2012/10/3    0
[解决办法]
[code=SQL][/code]
with t1 as
(
select 'A000' c1,date'2012-10-03' c2,1 c3 from dual
union all
select 'A100' c1,date'2012-07-09' c2,2 c3 from dual
union all
select 'A100' c1,date'2012-10-01' c2,3 c3 from dual
union all 
select 'A100' c1,date'2012-10-03' c2,4 c3 from dual
union all 
select 'A100' c1,date'2012-10-05' c2,5 c3 from dual
union all
select 'A110' c1,date'2012-08-09' c2,6 c3 from dual
union all
select 'A110' c1,date'2012-10-01' c2,7 c3 from dual
union all 
select 'A110' c1,date'2012-10-02' c2,8 c3 from dual
union all 
select 'A110' c1,date'2012-10-04' c2,9 c3 from dual
union all
select 'A120' c1,date'2012-10-01' c2,5 c3 from dual
union all
select 'A120' c1,date'2012-10-02' c2,7 c3 from dual
union all 
select 'A120' c1,date'2012-10-03' c2,3 c3 from dual
)
select t4.c1,t4.c2,nvl(t1.c3,0) from t1,
(select * from
(select distinct c1 from t1)t2,
(select date'2012-10-06' + 1 - level c2 from dual connect by level <= to_number(to_char(date'2012-10-06','dd')))t3
)t4 
where t1.c1(+) = t4.c1 and t1.c2(+) = t4.c2

1A0002012/10/10
2A0002012/10/20
3A0002012/10/31
4A0002012/10/40
5A0002012/10/50
6A0002012/10/60
7A1002012/10/13
8A1002012/10/20
9A1002012/10/34
10A1002012/10/40
11A1002012/10/55
12A1002012/10/60


13A1102012/10/17
14A1102012/10/28
15A1102012/10/30
16A1102012/10/49
17A1102012/10/50
18A1102012/10/60
19A1202012/10/15
20A1202012/10/27
21A1202012/10/33
22A1202012/10/40
23A1202012/10/50
24A1202012/10/60

热点排行