当前位置: 代码迷 >> Oracle开发 >> 老有关问题了,上次没解决,求ORACLE查询语句的写法
  详细解决方案

老有关问题了,上次没解决,求ORACLE查询语句的写法

热度:125   发布时间:2016-04-24 07:43:01.0
老问题了,上次没解决,求ORACLE查询语句的写法.
老问题了,上次没能解决掉,特又来烦扰各位老师:
SQL code
-----------------------表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的.拜谢了!
  相关解决方案