老问题了,上次没解决,求ORACLE查询语句的写法.
老问题了,上次没能解决掉,特又来烦扰各位老师:
-----------------------表1-----------------------同一ID有多种甚至上百种物种.ID不是物种编号,而是顾客唯一标识.VISIT_NO是提货次数.T_DATE VISIT_NO ID ITEM_NAME SPEC AMOUNT UNITS2008-6-1 14:43 1 00001 灯泡 100W 2 只2008-7-4 14:43 1 00002 灯泡 100W 3 只2008-7-4 14:43 1 00002 节能灯 50W 5 只2008-7-4 14:43 1 00002 消毒灯 100W 3 只2008-7-5 14:43 2 00003 灯泡 100W 4 只 //注意①2008-7-5 14:43 2 00003 节能灯 100W 7 只//注意②2008-7-5 14:43 2 00003 消毒灯 100W 8 只//注意③2008-7-6 14:43 1 00004 灯泡 100W 5 只2008-7-7 14:43 2 00005 灯泡 100W 6 只2008-7-8 14:43 1 00006 灯泡 100W 3 只-----------------------表2-----------------------表1中有//注意①,表2没有,当出现这类情况时,则转向表3取值.以a3.t_date和a3.id关联.注意:每天日期时间是不同的.但又必须有时间段约束.不然把除本月之外的相同ID也会一并提取统计进来.WW_DATE VISIT_NO TEXT NAME ID2008-6-1 14:43 1 灯泡 李1 000012008-1-4 8:43 1 灯泡 李2 000022008-1-4 8:43 1 节能灯 李2 000022008-1-4 8:43 1 消毒灯 李2 000022008-1-5 8:43 2 节能灯 李3 00003 //注意②2008-1-5 8:43 2 消毒灯 李3 00003 //注意③2008-1-6 8:43 1 灯泡 李4 000042008-1-7 8:43 2 灯泡 李2 000052008-1-8 8:43 1 灯泡 李6 00006-----------------------表3-----------------------注意:每天日期时间是不同的.但又必须做为限制条件.不然把除本月之外的相同ID也会一并提取统计进来,因此不能没有时间约束. ID VISIT_NO EE_DATE NAME00001 1 2008-6-1 14:43 李100002 1 2008-1-4 10:21 李200003 2 2008-1-5 10:21 李300004 1 2008-1-6 10:21 李400005 2 2008-1-7 10:21 李200006 1 2008-1-8 10:21 李600007 1 2008-1-9 10:21 李200008 1 2008-1-10 10:21 李200009 1 2008-1-11 10:21 李900010 1 2008-1-12 10:21 李200011 1 2008-1-13 10:21 李400012 1 2008-1-14 10:21 李400013 1 2008-1-15 10:21 李200014 1 2008-1-16 10:21 李100015 1 2008-1-17 10:21 李3-----------------------最终结果生成表4----------------------- 表4日期格式:YYYY-MM.T_DATE NAME ITEM_NAME SPEC AMOUNT UNITS2008-07 李1 灯泡 100w 33 只2008-07 李2 灯泡 100w 53 只2008-07 李3 灯泡 100w 40 只2008-07 李4 灯泡 100w 17 只2008-07 李5 灯泡 100w 8 只2008-07 李6 灯泡 100w 3 只2008-07 李8 灯泡 100w 3 只当向表2取NAME值无效,或不存在时,转取表3。手边有个表1关联表2的,或请朋友们给改写成关联表3的【句一】select to_char(a1.t_date,'yyyy-mm') t_date, a2.name, a1.item_name, a1.spec, to_char(sum(to_number(substr(amount,1,length(amount-1)))))amount,units from table1 a1,table2 a2 where to_char(a1.t_date, 'yyyy-mm-dd') = to_char(a2.ww_date, 'yyyy-mm-dd') and a1.ITEM_NAME = a2.TEXT and a1.visit_no =a2.visit_no and a1.ID=a2.ID and a1.t_date between to_date('2008-7-1','yyyy-mm-dd') and to_date('2008-7-31','yyyy-mm-dd')+0.99999 and a1.item_name = '灯泡' ; group by to_char(a1.t_date,'yyyy-mm'), a2.name, a1.item_name, a1.spec, units【句二】select to_char(t1.t_date, 'yyyy-mm') t_date, t2.name, t1.item_name, t1.spec, sum(to_number(substr(amount, 1, length(amount - 2)))) amount, t1.units from table1 t1, table2 t2 where to_char(t1.t_date, 'yyyy-mm-dd') = to_char(t2.ww_date, 'yyyy-mm-dd') and a1.ITEM_NAME = a2.TEXT and a1.visit_no =a2.visit_no and a1.ID=a2.ID and to_char(t1.t_date, 'yyyy-mm-dd') >= '2008-07-1' and to_char(t1.t_date, 'yyyy-mm-dd') <= '2008-07-31' and a1.item_name = '灯泡' ; group by to_char(t1.t_date, 'yyyy-mm'), t2.name, t1.item_name, t1.spec, t1.units;
[解决办法]
可能是本人天生愚笨,看了3遍还没明白需求。
如果可以的话,是否能按以下方式贴出需求?
1: 要用到的source tables.
2: 要生成的target table.
然后解释target table的数据是怎么生成的并且要注意的地方.
最好别贴以前的写的SQL,因为这会难以理解并且更混淆.
Just FYI ...
[解决办法]
如果是web页面run SQL语句然后返回出查询结果集的话,比如在JAVA里面写两个不同的方法,当关联表1和表2时返回为NULL记录后进行判断,再调用另外一个方法:关联表1 和表3的. 但是这样要进行数据库连接两次,比较消耗资源。
写一个存储过程的方法实现比较好(为3个传入变量:开始日期、结束日期和item_name),因为上述需求貌似为逻辑化的思想,不如是否对你有帮助。
Just FYI ..
[解决办法]
下边是表1和表3的关联:你union上表1和表2的关联就可以了(仅做参考)
select to_char(c1.t_date,'yyyy-mm') t_date,
d1.name,
c1.item_name,
c1.spec,
to_char(sum(to_number(substr(c1.amount,1,length(amount-1)))))amount,units
from
(select * from table1 a1 where not exists
(select * from table2 a2 where
to_char(a1.t_date, 'yyyy-mm-dd') = to_char(a2.ww_date, 'yyyy-mm-dd')
and a1.ITEM_NAME = a2.TEXT
and a1.visit_no =a2.visit_no
and a1.ID=a2.ID
and a1.t_date between to_date('2008-7-1','yyyy-mm-dd')
and to_date('2008-7-31','yyyy-mm-dd')+0.99999
and a1.item_name = '灯泡' ;
) ) c1,table3 d1
where c1.id=d1.id
and to_char(c1.t_date, 'yyyy-mm-dd') = to_char(d1.ww_date, 'yyyy-mm-dd')
and c1.t_date between to_date('2008-7-1','yyyy-mm-dd')
and to_date('2008-7-31','yyyy-mm-dd')+0.99999
and c1.item_name = '灯泡' ;
[解决办法]
up