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的就尽量不用,我是初级菜鸟。
店名 --- 销售日期
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的就尽量不用,我是初级菜鸟。
解决方案 »
- oracle无效且未通过重新验证
- 这样的sql查询如何优化
- toad与tablespace
- 求oracle查询语句 急啊 拜托了
- 在XP(上海政府OEM版)中不能安装Oracle 8i或者9i吗?在线等待,急!!!
- 请问oracle有没有让id自动增长(insert时不用手动插入id)的办法?
- 等米下锅!!!
- 大家能不能给我介绍基本比较经典的Oracle书籍.
- …………效率问题:保存一位数字用char(1) 还是Number(1),那种更适合?
- suse linux 11 sp1 64位,创建oracle11g.建立监听报错
- oracle 查询结果中的一个字段结果是id的组合(1,2)是字符串,如何让另一个表查询的时候可以用in查这个Id的集合
- 类似与WMSYS.WM_CONCAT函数的方法
with t as
select year||'-'||month||'-'||lpad(rownum,2,'00') saleday from all_all_tables where rownum<day
select b.店名,c.saleday 销售日期,(select sum(销售量) from 销售表 c where to_char(销售日期,'yyyy-mm-dd')=t.saleday and c.店名=b.店名) 销售数量 from t,销售店表 b
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_num
from
(
select a.c1,b.t_date
from
(
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.c2
group by c.c1,c.t_date
order by c.c1,c.t_date c1 t_date c_num
--------------------------------------
1 A000 2012/10/1 0
2 A000 2012/10/2 0
3 A000 2012/10/3 1
4 A110 2012/10/1 2
5 A110 2012/10/2 0
6 A110 2012/10/3 1
7 A120 2012/10/1 1
8 A120 2012/10/2 0
9 A120 2012/10/3 0
2
#4楼 得分:0回复于:2012-10-26 10:13:11按照常规思路写了一个 构造点名日期表 关联销售表统计数量 参数: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
)
这部分的数据不是要写很多吗?而且这样写好像也是不能根据输入日期变动呀??求回复,谢谢。
然后sql里面的2012-10-03相当于参数 你也可以输入2012-10-31 那么动态构建的日期就是31天 lz可以试下
select c.dlrcode,c.t_date,count(d.salesDATE) c_num
from
(
select a.dlrcode,b.t_date
from
(
select distinct dlrcode
from watool.VHC_LEDJER_TB
where salesDATE 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 watool.VHC_LEDJER_TB d on c.dlrcode = d.dlrcode and c.t_date = d.salesDATE
group by c.dlrcode,c.t_date
order by c.dlrcode,c.t_date
但是得到的结果是,销售量t_date全为0呀?麻烦在帮我看看。
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.c21 A000 2012/10/1 0
2 A000 2012/10/2 0
3 A000 2012/10/3 1
4 A000 2012/10/4 0
5 A000 2012/10/5 0
6 A000 2012/10/6 0
7 A100 2012/10/1 3
8 A100 2012/10/2 0
9 A100 2012/10/3 4
10 A100 2012/10/4 0
11 A100 2012/10/5 5
12 A100 2012/10/6 0
13 A110 2012/10/1 7
14 A110 2012/10/2 8
15 A110 2012/10/3 0
16 A110 2012/10/4 9
17 A110 2012/10/5 0
18 A110 2012/10/6 0
19 A120 2012/10/1 5
20 A120 2012/10/2 7
21 A120 2012/10/3 3
22 A120 2012/10/4 0
23 A120 2012/10/5 0
24 A120 2012/10/6 0