日期:2014-05-17  浏览次数:20735 次

老问题了,上次没解决,求ORACLE查询语句的写法.
老问题了,上次没能解决掉,特又来烦扰各位老师:
SQL code

-----------------------
表1
-----------------------
同一ID有多种甚至上百种物种.ID不是物种编号,而是顾客唯一标识.VISIT_NO是提货次数.

T_DATE    VISIT_NO    ID    ITEM_NAME    SPEC    AMOUNT    UNITS
2008-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    ID
2008-6-1 14:43    1    灯泡              李1    00001
2008-1-4 8:43    1    灯泡              李2    00002
2008-1-4 8:43    1    节能灯              李2    00002
2008-1-4 8:43    1    消毒灯              李2    00002
2008-1-5 8:43    2    节能灯              李3    00003 //注意②
2008-1-5 8:43    2    消毒灯              李3    00003 //注意③
2008-1-6 8:43    1    灯泡              李4    00004
2008-1-7 8:43    2    灯泡              李2    00005
2008-1-8 8:43    1    灯泡              李6    00006
-----------------------
表3
-----------------------
注意:每天日期时间是不同的.但又必须做为限制条件.不然把除本月之外的相同ID也会一并提取统计进来,因此不能没有时间约束.

  ID    VISIT_NO   EE_DATE         NAME
00001     1       2008-6-1 14:43    李1
00002     1       2008-1-4 10:21    李2
00003     2       2008-1-5 10:21    李3
00004     1       2008-1-6 10:21    李4
00005     2       2008-1-7 10:21    李2
00006     1       2008-1-8 10:21    李6
00007     1       2008-1-9 10:21    李2
00008     1       2008-1-10 10:21    李2
00009     1       2008-1-11 10:21    李9
00010     1       2008-1-12 10:21    李2
00011     1       2008-1-13 10:21    李4
00012     1       2008-1-14 10:21    李4
00013     1       2008-1-15 10:21    李2
00014     1       2008-1-16 10:21    李1
00015     1       2008-1-17 10:21    李3
-----------------------
最终结果生成表4
-----------------------
 表4日期格式:YYYY-MM.
T_DATE    NAME  ITEM_NAME    SPEC    AMOUNT    UNITS
2008-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'),