View previous topic :: View next topic
|
Author |
Message |
balaji81_k
Active User
Joined: 29 Jun 2005 Posts: 155
|
|
|
|
Hi ,
I have written a SQL query to create a date file which is used by job to fetch sales data for every past 1 hour . Problem is when current time is pass 12 hours y (i,e) by 12 AM it is fetching CURRENT DATE which is wrong , for that we are hardcoding date for the query input .
Code: |
SELECT CAST(VARCHAR_FORMAT(GREGORIAN_DATE,'MM/DD/YYYY') AS CHAR(10)),
CAST('|' AS CHAR(1)),
CAST(VARCHAR_FORMAT(GREGORIAN_DATE,'YYYY.MM.DD') AS CHAR(10)),
SUBSTR(DIGITS(COL2),9,2),
CAST('|' AS CHAR(1)),
CASE WHEN GREGORIAN_DATE = DATE(CURRENT_TIMESTAMP - 1 HOUR) AND
COL2 = HOUR(CURRENT_TIME - 1 HOUR)
THEN CAST ('CURRHOUR' AS CHAR(8))
WHEN GREGORIAN_DATE = DATE(CURRENT_TIMESTAMP - 2 HOUR) AND
COL2 = HOUR(CURRENT_TIME - 2 HOUR)
THEN CAST ('PREVHOUR' AS CHAR(8))
WHEN GREGORIAN_DATE = CURRENT_DATE AND
COL2 >= HOUR(CURRENT_TIME)
THEN CAST('OPENHOUR' AS CHAR(8))
ELSE CAST('CLSDHOUR' AS CHAR(8)) END,
CAST('|' AS CHAR(1)),
SUBSTR(DIGITS(HOUR(CURRENT_TIME - 2 HOUR)),9,2)
CAST('|' AS CHAR(1)),
SUBSTR(DIGITS(HOUR(CURRENT_TIME - 1 HOUR)),9,2)
CAST('|' AS CHAR(1))
FROM TEST.CALENDER_TABLE ,
(SELECT 00 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 01 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 02 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 03 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 04 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 05 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 06 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 07 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 08 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 09 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 10 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 11 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 12 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 13 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 14 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 15 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 16 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 17 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 18 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 19 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 20 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 21 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 22 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 23 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL) AS A
WHERE GREGORIAN_DATE = CURRENT_DATE <- hardcoded everytime with current date on each day .
ORDER BY GREGORIAN_DATE,COL2
WITH UR;
|
Output file looks like
10/14/2016|2016.10.14|00|CLSDHOUR|09|10|
....
....,
WHEN IT RUNS at 12:30 in Night it represents as
10/15/2016|2016.10.15|23|CURRHOUR|22|23|
but expected is
10/14/2016|2016.10.14|23|CURRHOUR|22|23|
I should check for time by adding another case while populating when time crosses 12 hrs. Can any one help me in checking this case and also i need to feed the current date as input . |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
You are getting precisely what you told the computer you wanted:
Code: |
SELECT CAST(VARCHAR_FORMAT(GREGORIAN_DATE,'MM/DD/YYYY') AS CHAR(10)),
CAST('|' AS CHAR(1)),
CAST(VARCHAR_FORMAT(GREGORIAN_DATE,'YYYY.MM.DD') AS CHAR(10)), |
so you cannot say anything else is expected -- you didn't provide any code to use a previous date, so you won't get one. |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3074 Location: NYC,USA
|
|
|
|
Try,
Code: |
select case when current time - 1 Hour <= '23:59:59' then VARCHAR_FORMAT(current date - 1 day,'MM/DD/YYYY')
else VARCHAR_FORMAT(current date,'MM/DD/YYYY') ) end |
|
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
Rohit Umarjikar wrote: |
case when current time - 1 Hour <= '23:59:59' |
I might be missing something. but won't this be always true? |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3074 Location: NYC,USA
|
|
|
|
That's right!!, How about this?
Code: |
select
case when (current time > '23:59:59' AND
current time <= '00:59:59') then
VARCHAR_FORMAT(current date - 1 day,'MM/DD/YYYY')
else VARCHAR_FORMAT(current date,'MM/DD/YYYY') end
from sysibm.sysdummy1 |
|
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10879 Location: italy
|
|
|
|
why the
wouldn' t
Code: |
current time <= '00:59:59' |
be enough |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3074 Location: NYC,USA
|
|
|
|
Took out -1(assuming"fetch sales data for every past 1 hour" done before) but I guess range is still needed, after 24:00:00 even though we are entering into next day and that's technically true but here TS wants to go back 1 hour which then gets us back to previous day date but TS unable to get that date instead getting the next date. So basically TS wants previous date only when it is between midnight till 1am. |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
Rohit Umarjikar wrote: |
How about this?
Code: |
select
case when (current time > '23:59:59' |
|
Will this ever be true? |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3074 Location: NYC,USA
|
|
|
|
I realize now that I can't think of good, it is Friday too. I should go home better
After all your comments , this should work and have a Good Weekend!!
Code: |
select
case when (current time <= '00:59:59' )
then
VARCHAR_FORMAT(current date - 1 day,'MM/DD/YYYY')
else VARCHAR_FORMAT(current date,'MM/DD/YYYY') end
from sysibm.sysdummy1 |
|
|
Back to top |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1310 Location: Vilnius, Lithuania
|
|
|
|
Has anyone actually thought about what happens if the job is held up and doesn't run until 2 hours later?
'nuff said... |
|
Back to top |
|
|
balaji81_k
Active User
Joined: 29 Jun 2005 Posts: 155
|
|
|
|
Hi All,
Thanks every one for the inputs. |
|
Back to top |
|
|
balaji81_k
Active User
Joined: 29 Jun 2005 Posts: 155
|
|
|
|
Hi Enrico,
i have tried to use only one condition as advised but i couldn't get
columns '00- 23' as per subselect query(i,e) SELECT 00-23 from SYSIBM.SYSDUMMY1. Could you please help me what is missing here.
Code: |
SELECT
CASE WHEN (CURRENT_TIME <= '00:59:59' ) THEN
VARCHAR_FORMAT(CURRENT_DATE - 1 day,'MM/DD/YYYY')
||CAST('|' AS CHAR(1))
||VARCHAR_FORMAT(CURRENT_DATE - 1 day,'MM.DD.YYYY')
ELSE
VARCHAR_FORMAT(CURRENT_DATE,'MM/DD/YYYY')
||CAST('|' AS CHAR(1))
||VARCHAR_FORMAT(CURRENT_DATE,'MM.DD.YYYY')
END AS DATE
CASE WHEN GREGORIAN_DATE = DATE(CURRENT_TIMESTAMP - 1 HOUR) AND
COL2 = HOUR(CURRENT_TIME - 1 HOUR)
THEN CAST ('CURRHOUR' AS CHAR(8))
WHEN GREGORIAN_DATE = DATE(CURRENT_TIMESTAMP - 2 HOUR) AND
COL2 = HOUR(CURRENT_TIME - 2 HOUR)
THEN CAST ('PREVHOUR' AS CHAR(8))
WHEN GREGORIAN_DATE = CURRENT_DATE AND
COL2 >= HOUR(CURRENT_TIME)
THEN CAST('OPENHOUR' AS CHAR(8))
ELSE CAST('CLSDHOUR' AS CHAR(8)) END,
CAST('|' AS CHAR(1)),
SUBSTR(DIGITS(HOUR(CURRENT_TIME - 2 HOUR)),9,2)
CAST('|' AS CHAR(1)),
SUBSTR(DIGITS(HOUR(CURRENT_TIME - 1 HOUR)),9,2)
CAST('|' AS CHAR(1))
FROM TEST.CALENDER_TABLE ,
(SELECT 00 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 01 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 02 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 03 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 04 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 05 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 06 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 07 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 08 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 09 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 10 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 11 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 12 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 13 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 14 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 15 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 16 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 17 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 18 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 19 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 20 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 21 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 22 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 23 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL) AS A
WHERE GREGORIAN_DATE = CURRENT_DATE
ORDER BY GREGORIAN_DATE,COL2
WITH UR;
|
|
|
Back to top |
|
|
balaji81_k
Active User
Joined: 29 Jun 2005 Posts: 155
|
|
|
|
Hi Every one ,
I am able to see the output in my file . However I am seeing the low values in my output file at column 23rd position(i,e) after date format(YYYY.MM.DD) .Not sure what is the issue . Can any one help me on this?
Code: |
SELECT
(
SELECT WHEN CURRENT_TIME <= '00:59:59' THEN
CAST(VARCHAR_FORMAT(CURRENT_DATE - 1 DAY,'MM/DD/YYYY') AS CHAR(10))
||CAST('|' AS CHAR(1))
||CAST(VARCHAR_FORMAT(CURRENT_DATE - 1 DAY,'YYYY.MM.DD') AS CHAR(10))
||CAST('|' AS CHAR(1))
ELSE
CAST(VARCHAR_FORMAT(CURRENT_DATE,'MM/DD/YYYY') AS CHAR(10))
||CAST('|' AS CHAR(1))
CAST(VARCHAR_FORMAT(CURRENT_DATE,'YYYY.MM.DD') AS CHAR(10))
||CAST('|' AS CHAR(1))
END FROM SYSIBM.SYSDUMMY1) AS A,
CAST('|' AS CHAR(1)),
SUBSTR(DIGITS(COL2),9,2),
CAST('|' AS CHAR(1)),
CASE WHEN GREGORIAN_DATE = DATE(CURRENT_TIMESTAMP - 1 HOUR) AND
COL2 = HOUR(CURRENT_TIME - 1 HOUR)
THEN CAST ('CURRHOUR' AS CHAR(8))
WHEN GREGORIAN_DATE = DATE(CURRENT_TIMESTAMP - 2 HOUR) AND
COL2 = HOUR(CURRENT_TIME - 2 HOUR)
THEN CAST ('PREVHOUR' AS CHAR(8))
WHEN GREGORIAN_DATE = CURRENT_DATE AND
COL2 >= HOUR(CURRENT_TIME)
THEN CAST('OPENHOUR' AS CHAR(8))
ELSE CAST('CLSDHOUR' AS CHAR(8)) END,
CAST('|' AS CHAR(1)),
SUBSTR(DIGITS(HOUR(CURRENT_TIME - 2 HOUR)),9,2)
CAST('|' AS CHAR(1)),
SUBSTR(DIGITS(HOUR(CURRENT_TIME - 1 HOUR)),9,2)
CAST('|' AS CHAR(1))
FROM TEST.CALENDER_TABLE ,
(SELECT 00 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 01 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 02 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 03 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 04 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 05 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 06 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 07 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 08 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 09 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 10 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 11 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 12 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 13 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 14 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 15 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 16 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 17 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 18 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 19 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 20 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 21 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 22 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 23 AS COL2 FROM SYSIBM.SYSDUMMY1 UNION ALL) AS A
WHERE GREGORIAN_DATE = CURRENT_DATE
ORDER BY GREGORIAN_DATE,COL2
WITH UR;
|
Output file:-
Code: |
10/18/2016|2016.10.18|.|00|CLSDHOUR|12|13|
|
|
|
Back to top |
|
|
RahulG31
Active User
Joined: 20 Dec 2014 Posts: 446 Location: USA
|
|
|
|
Quote: |
SELECT WHEN CURRENT_TIME <= '00:59:59' THEN
CAST(VARCHAR_FORMAT(CURRENT_DATE - 1 DAY,'MM/DD/YYYY') AS CHAR(10))
||CAST('|' AS CHAR(1))
||CAST(VARCHAR_FORMAT(CURRENT_DATE - 1 DAY,'YYYY.MM.DD') AS CHAR(10))
||CAST('|' AS CHAR(1))
ELSE
CAST(VARCHAR_FORMAT(CURRENT_DATE,'MM/DD/YYYY') AS CHAR(10))
||CAST('|' AS CHAR(1))
CAST(VARCHAR_FORMAT(CURRENT_DATE,'YYYY.MM.DD') AS CHAR(10))
||CAST('|' AS CHAR(1))
END FROM SYSIBM.SYSDUMMY1) AS A,
CAST('|' AS CHAR(1)),
SUBSTR(DIGITS(COL2),9,2),
CAST('|' AS CHAR(1)),
|
You have a '|' before end of select and another '|' after end of select. That makes 2 continuous '|'.
. |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3074 Location: NYC,USA
|
|
|
|
what is the relationship between A and TEST.CALENDER_TABLE ? |
|
Back to top |
|
|
balaji81_k
Active User
Joined: 29 Jun 2005 Posts: 155
|
|
|
|
Hi Rohit,
Now there is no relation as it was required when GREGORIAN DATE is fetched from it . I have removed '|' after select but still getting the same data in output file. |
|
Back to top |
|
|
balaji81_k
Active User
Joined: 29 Jun 2005 Posts: 155
|
|
|
|
Hi Rohit,
Output file data looks like
Code: |
10/18/2016|2016.10.18|.00|CLSDHOUR|12|13|
|
|
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3074 Location: NYC,USA
|
|
|
|
there is no relationship then why you have it?
Code: |
SUBSTR(DIGITS(COL2),9,2), |
why you need this ? you can use a col2 by itself, right? |
|
Back to top |
|
|
balaji81_k
Active User
Joined: 29 Jun 2005 Posts: 155
|
|
|
|
Yes Rohit you are correct we can use by itself .But still getting the low values in my output file .Please advise. |
|
Back to top |
|
|
RahulG31
Active User
Joined: 20 Dec 2014 Posts: 446 Location: USA
|
|
|
|
Code: |
SELECT
(
SELECT WHEN CURRENT_TIME <= '00:59:59' THEN
|
You are missing the word 'CASE' before 'WHEN'. I am not sure how your query would run without a mention of 'CASE'.
And if you are just missing that in this post, then you may well be missing some other fields as well. You need to break your query field by field and verify.
. |
|
Back to top |
|
|
balaji81_k
Active User
Joined: 29 Jun 2005 Posts: 155
|
|
|
|
Hi Rahul,
Sorry it is missing in the post , actual query has the case statement .
I break each field and even tried to cast the col2 as CHAR(2) but couldn't able to avoid the low value in the output file |
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2454 Location: Hampshire, UK
|
|
|
|
Why not cut and paste the real query - then people will be working with reality instead of something that is, basically, a figment of the imagination.
And, how do you know it is a low-value? All you have presented is a plain-text impression - not the 'hex on' view. Without that we do not know if it is a real period, a low value or some other unprintable value. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10879 Location: italy
|
|
|
|
time to lock the topic, it is going nowhere |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3074 Location: NYC,USA
|
|
|
|
I think, we should lock this topic and let the TS open a new one with right and complete query instead of piece meal. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10879 Location: italy
|
|
|
|
done |
|
Back to top |
|
|
|