SQL别名可以是一个函数吗? (PL / SQL)

时间:2014-12-03 19:57:22

标签: plsql

我制作的每日报告会产生接下来7天的价值:

select
a.itemnumber
,sum(case when a.activitydate = trunc(sysdate) then a.qtyordered else 0 end) as Today
,sum(case when a.activitydate = trunc(sysdate) + 1 then a.qtyordered else 0 end) as Tomorrow
,sum(case when a.activitydate = trunc(sysdate) + 2 then a.qtyordered else 0 end) as Day3
,sum(case when a.activitydate = trunc(sysdate) + 3 then a.qtyordered else 0 end) as Day4
,sum(case when a.activitydate = trunc(sysdate) + 4 then a.qtyordered else 0 end) as Day5
,sum(case when a.activitydate = trunc(sysdate) + 5 then a.qtyordered else 0 end) as Day6
,sum(case when a.activitydate = trunc(sysdate) + 6 then a.qtyordered else 0 end) as Day7
,a.balanceonhand BOH

from
mytable a

where
a.itemnumber between 14000 and 15000

这些别名比我想要的更模糊,特别是如果用户将报告从一天比较到另一天,这些都没有任何意义。使用PL / SQL Developer,我可以创建一个会产生变量日期的别名吗?我尝试了以下代码:

select
a.itemnumber
,sum(case when a.activitydate = trunc(sysdate) then a.qtyordered else 0 end) as sysdate
,sum(case when a.activitydate = trunc(sysdate) + 1 then a.qtyordered else 0 end) as sysdate + 1
,sum(case when a.activitydate = trunc(sysdate) + 2 then a.qtyordered else 0 end) as sysdate + 2
,sum(case when a.activitydate = trunc(sysdate) + 3 then a.qtyordered else 0 end) as sysdate + 3
,sum(case when a.activitydate = trunc(sysdate) + 4 then a.qtyordered else 0 end) as sysdate + 4
,sum(case when a.activitydate = trunc(sysdate) + 5 then a.qtyordered else 0 end) as sysdate + 5
,sum(case when a.activitydate = trunc(sysdate) + 6 then a.qtyordered else 0 end) as sysdate + 6
,a.balanceonhand BOH

from
mytable a

where
a.itemnumber between 14000 and 15000

group by
a.itemnumber
,a.balanceonhand

但是它说没有找到FROM关键字。这可能吗?

1 个答案:

答案 0 :(得分:1)

如果使用行生成器来获取七个日期(或实际上任意数量的日期)会更容易。这将以行为基础格式提供数据。

从那里我建议PIVOT是将输出重新格式化为列而不是行的好方法。我在dategen查询中提供了一个'offset'列,以便可以将其添加到startdate,在这种情况下,它是一个sqlplus变量,但我建议在生产查询中使用绑定变量。我强烈建议不要使用SYSDATE,因为这会让事情很难测试。如果您可以更改报告涵盖的日期以适应可用的测试数据,则会更容易。

  1  WITH
  2  dategen
  3  as
  4  (
  5  SELECT TO_DATE('&startdate','DD-MON-YYYY')+LEVEL mydate, LEVEL myoffset
  6  FROM dual
  7  CONNECT BY LEVEL <=7)
  8  SELECT itemnumber,balanceonhand, activitydate, today, tomorrow, day3, day4, day5, day6, day7
  9  FROM (
 10  SELECT *
 11  FROM mytable t
 12  JOIN dategen d ON (d.mydate = t.activitydate)
 13  PIVOT (
 14    SUM(qtyordered)
 15    FOR myoffset IN (0 as today,1 as tomorrow,2 day3,3 as day4, 4 as day5, 5 as day6, 6 as day7)
 16* ))
SQL> /
Enter value for startdate: 01-JAN-2015
old   5: SELECT TO_DATE('&startdate','DD-MON-YYYY')+(level-1) mydate, LEVEL myoffset
new   5: SELECT TO_DATE('01-JAN-2015','DD-MON-YYYY')+(level-1) mydate, LEVEL myoffset

ITEMNUMBER BALANCEONHAND ACTIVITYDATE TODAY TODORDAYD DAY4 DAY5 DAY6 DAY7


   120       100 02-JAN-15                 10
   140       100 04-JAN-15                           10
   100       100 02-JAN-15                 10

选择了3行。

SQL> select * from mytable;

ITEMNUMBER BALANCEONHAND ACTIVITYDATE       QTYORDERED
---------- ------------- ------------------ ----------
       100       100 02-JAN-15          10
       120       100 02-JAN-15          10
       140       100 04-JAN-15          10

3 rows selected.