1

I'm making a daily report that will produce a value for the next 7 days as such:

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

These aliases are more ambiguous than I would like, and especially if the user compares reports from one day to another, these mean nothing. Using PL/SQL Developer, can I make an alias that will produce a variable date? I tried the following code:

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

But it says the FROM keyword not found where expected. Is this possible?

1
  • 3
    I don't believe this is possible. You'd have to use dynamic SQL.
    – ilitirit
    Dec 3, 2014 at 20:13

1 Answer 1

1

It would be easier if you use a row generator to get your seven dates (or indeed any number of dates). This will provide the data in row based format.

From there I suggest PIVOT is a good way to reformat the output into columns rather than rows. I've provided an 'offset' column in the dategen query so that this can be added to the startdate which, in this case, is a sqlplus variable but I'd suggest using a bind variable in the production query. I'd strongly recommend against using SYSDATE as this makes things very hard to test. It's easier when you can change the dates covered by the report to suit available test data.

  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 TOMORROW DAY3 DAY4 DAY5 DAY6 DAY7


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

3 rows selected.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.