I Have a query which will calculate the number of orders on 5 days range.and planning to run daily.
SELECT REQUESTED_DATE, COUNT(Orders) REQUESTED_ORDERS
FROM SVCO
WHERE (TRUNC(UCBSVCO_REQUESTED_DATE) BETWEEN TRUNC(SYSDATE)AND TRUNC(SYSDATE+5))
GROUP BY REQUESTED_DATE
ORDER BY 1
I need to set a Flag to Y or N based on the following requirement ;
if requested orders >=350 and <=399 or >=400 set flag to Y; if already the flag is set to Y by previous run then flag should be N
Below is the sample scenarios. This query will be run everyday
First day run
REQUESTED_DATE | REQUESTED_ORDERS | flag |
6/23/2016 | 189 | |
6/24/2016 | 99 | |
6/25/2016 | 142 | |
6/26/2016 | 350 | y |
6/27/2016 | 348 | |
Second day Run
REQUESTED_DATE | REQUESTED_ORDERS | flag |
6/24/2016 | 120 | |
6/25/2016 | 270 | |
6/26/2016 | 367 | N |
6/27/2016 | 359 | y |
6/28/2016 | 100 | |
Flag for 6/26/2016 is N because it already been set to Y for last run it will be N till the requested orders are crossing 400;then the flag would be Y.
6/27/2016 flag in Y ; because it was not in the previous run and it crossed 350
Third DayRun
REQUESTED_DATE | REQUESTED_ORDERS | flag |
6/25/2016 | 360 | y |
6/26/2016 | 380 | n |
6/27/2016 | 359 | n |
6/28/2016 | 310 | |
6/29/2016 | 360 | y |
6/25/2016 flag is Y because it never crossed 350 in previous run and never set a flag ;
6/26/2016 flag is still N becasue it never crossed 400 Yet
same with 6/27/2016
Forth day Run
REQUESTED_DATE | REQUESTED_ORDERS | |
6/26/2016 | 402 | Y |
6/27/2016 | 390 | n |
6/28/2016 | 350 | y |
6/29/2016 | 400 | y |
6/30/2016 | 279 | |
6/26/2016 is set to Y becasue it crossed 400.
6/27/2016 is still N becasue it didt cross 400
And so on .
Can anyone tell me how to implement this. I can use procedure or temp tables etc..
Thanks,
Ann