View previous topic :: View next topic
|
Author |
Message |
Ron Klop
New User
Joined: 28 Sep 2012 Posts: 28 Location: holland
|
|
|
|
Hello
I have a table with the next columns:
- date
- weekend-indication (value Y or N)
- holiday-indication (value Y or N)
I want to have all the dates of that table and also every date + 5 working days. A working day is a day where weekend-indication ans holiday-indication are N.
So I have to join the table with itself.
If this is my input
2018-01-01, weekend-indication N, holiday-indication N
2018-01-02, weekend-indication Y, holiday-indication N
2018-01-03, weekend-indication Y, holiday-indication N
2018-01-04, weekend-indication N, holiday-indication N
2018-01-05, weekend-indication N, holiday-indication N
2018-01-06, weekend-indication N, holiday-indication N
2018-01-07, weekend-indication N, holiday-indication Y
2018-01-08, weekend-indication N, holiday-indication Y
2018-01-09, weekend-indication Y, holiday-indication N
2018-01-10, weekend-indication Y, holiday-indication N
2018-01-11, weekend-indication N, holiday-indication N
2018-01-12, weekend-indication N, holiday-indication N
this would be my output for 2018-01-01:
2018-01-01, 2018-01-12
How do I do this?
Regards
Ron |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3051 Location: NYC,USA
|
|
|
|
Why below don't made it to your list?
Code: |
2018-01-04, weekend-indication N, holiday-indication N
2018-01-05, weekend-indication N, holiday-indication N
2018-01-06, weekend-indication N, holiday-indication N
2018-01-04, weekend-indication N, holiday-indication N
2018-01-11, weekend-indication N, holiday-indication N |
Please explain the requirements in much better way, its not clear as what you want exactly and make a use of code tags. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
Quote: |
Please explain the requirements in much better way, its not clear as what you want exactly and make a use of code tags. |
the requirement was/is more than clear ...
add FIVE working days to some date
what is not clear is the requirement to do it with a single query
adding working days to a date is something that has been around for at least
45 years or probably a bit more
and almost every organisation I knew had a callable routine to do it |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3051 Location: NYC,USA
|
|
|
|
Quote: |
what is not clear is the requirement to do it with a single query |
I have asked question already why other rows missed in the output.
Quote: |
this would be my output for 2018-01-01:
2018-01-01, 2018-01-12 |
and how did 2018-01-12 date calculated? |
|
Back to top |
|
|
RahulG31
Active User
Joined: 20 Dec 2014 Posts: 446 Location: USA
|
|
|
|
The things that are Not clear are:
1. Will you have an input date to the query? In the example, will you have '2018-01-01' as input?
2. The example seems to be only a part of the requirement since you want all the dates and then +5 working dates
Quote: |
I want to have all the dates of that table and also every date + 5 working days |
So, If I add one more row to your example as:
Code: |
2018-01-13, weekend-indication N, holiday-indication N |
Then, what'll be your output?
Will it be:
Code: |
2018-01-01, 2018-01-12
2018-01-02, 2018-01-13
2018-01-03, 2018-01-13
|
OR will you still show 2018-01-12 as +5 date to 2018-01-02/03 ?
Code: |
2018-01-01, 2018-01-12
2018-01-02, 2018-01-12
2018-01-03, 2018-01-12
|
3. Will you show all the dates even if there is No +5 date ?
4. Will this query limit to a Month/Year or just ALL THE DATES in the table?
. |
|
Back to top |
|
|
Ron Klop
New User
Joined: 28 Sep 2012 Posts: 28 Location: holland
|
|
|
|
I am showing just a subset of our table.
As I said, in our table a working day is a day where weekend-indication and holiday-indication have a value of N.
So in my example 2018-01-04 won't make it, because:
- 2018-01-05 and 2018-01-06 are working days
- 2018-01-07 and 2018-01-08 are holidays
- 2018-01-09 and 2018-01-10 is weekend
- 2018-01-11 and 2018-01-12 are working days
so totally 4 working days. If I had added 2018-01-13 to the list (with both indicators N) I would have a extra row:
2018-01-04, 2018-01-13
But as I said , it is just a subset
In my opinion it should be possible in one query (with a correlated subquery maybe??)
Regards
Ron |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
Quote: |
and how did 2018-01-12 date calculated? |
by considering days to add those with
the weekend flag Y or the holiday flag Y |
|
Back to top |
|
|
Ron Klop
New User
Joined: 28 Sep 2012 Posts: 28 Location: holland
|
|
|
|
Hai RahulG31
my answers to your questions
1. yes
2. you are right, output will be the first one, so
2018-01-01, 2018-01-12
2018-01-02, 2018-01-13
2018-01-03, 2018-01-13
3. all the dates
4. all the dates in the table.
I want to built a temporary table (CTE: Common Table Expression) with rows that contain two attributes:
date and date + 5 working days
With a specific date I then want to select this row from this temporary table. |
|
Back to top |
|
|
RahulG31
Active User
Joined: 20 Dec 2014 Posts: 446 Location: USA
|
|
|
|
You'll probably need to use ROW_NUMBER() function of DB2 somewhere.
I haven't tested this but you can try something like:
Code: |
​SELECT Y.D2, X.D1
FROM
( SELECT B.MYDATE D2,
ROW_NUMBER() OVER(ORDER BY B.MYDATE ASC) R2
FROM MYTABLE B ) Y,
( SELECT A.MYDATE D1,
ROW_NUMBER() OVER(ORDER BY A.MYDATE ASC) R1
FROM MYTABLE A
WHERE A.WEEKEND_IND = 'N'
AND A.HOLIDAY_IND = 'N') X
WHERE R1= R2 + 5; |
I think this should give you a good sense on how to proceed.
Let me know if this works Or gets you closer to what you plan to achieve.
You can look more into ROW_NUMBER() here:
https://www.ibm.com/support/knowledgecenter/en/SSPT3X_2.1.2/com.ibm.swg.im.infosphere.biginsights.bigsql.doc/doc/bsql_row_number.html
. |
|
Back to top |
|
|
Ron Klop
New User
Joined: 28 Sep 2012 Posts: 28 Location: holland
|
|
|
|
it says
SQLCODE = -206, ERROR: Y.D2 IS NOT VALID IN THE CONTEXT WHERE IT IS
USED
I tried to solve is, but I don't see it
Regards
Ron |
|
Back to top |
|
|
RahulG31
Active User
Joined: 20 Dec 2014 Posts: 446 Location: USA
|
|
|
|
Show the query you are trying to execute. |
|
Back to top |
|
|
Ron Klop
New User
Joined: 28 Sep 2012 Posts: 28 Location: holland
|
|
|
|
---------+---------+---------+---------+---------+---------+---------+-
SELECT Y.D2, X.D1
FROM
( SELECT B.DAT,
ROW_NUMBER() OVER(ORDER BY B.DAT ASC) R2
FROM GWSDDBA5.gwtb403 B ) Y,
( SELECT A.DAT D1,
ROW_NUMBER() OVER(ORDER BY A.DAT ASC) R1
FROM GWSDDBA5.GWTB403 A
WHERE A.NED_WKNd_IND = 'N'
AND A.NED_FSTD_IND = 'N') X
WHERE R1 = R2 + 5;
---------+---------+---------+---------+---------+---------+---------+-
DSNT408I SQLCODE = -206, ERROR: Y.D2 IS NOT VALID IN THE CONTEXT WHERE
USED
Code: |
---------+---------+---------+---------+---------+---------+---------+-
SELECT Y.D2, X.D1
FROM
( SELECT B.DAT,
ROW_NUMBER() OVER(ORDER BY B.DAT ASC) R2
FROM GWSDDBA5.gwtb403 B ) Y,
( SELECT A.DAT D1,
ROW_NUMBER() OVER(ORDER BY A.DAT ASC) R1
FROM GWSDDBA5.GWTB403 A
WHERE A.NED_WKNd_IND = 'N'
AND A.NED_FSTD_IND = 'N') X
WHERE R1 = R2 + 5;
---------+---------+---------+---------+---------+---------+---------+-
DSNT408I SQLCODE = -206, ERROR: Y.D2 IS NOT VALID IN THE CONTEXT WHERE
USED |
|
|
Back to top |
|
|
RahulG31
Active User
Joined: 20 Dec 2014 Posts: 446 Location: USA
|
|
|
|
You don't have a D2 anywhere in your query. You need:
SELECT B.DAT D2
. |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
I did not look at the whole sql, but just for the error - looks like you are missing a D2 in this line here
|
|
Back to top |
|
|
Ron Klop
New User
Joined: 28 Sep 2012 Posts: 28 Location: holland
|
|
|
|
you were right. Runs now, but strange results. Will check it later
D2 D1
---------+---------+--
2018-05-27 2025-02-14
2018-05-28 2025-02-17
2018-05-29 2025-02-18
2018-05-30 2025-02-19
2018-05-31 2025-02-20
2018-06-01 2025-02-21
Code: |
D2 D1
---------+---------+--
2018-05-27 2025-02-14
2018-05-28 2025-02-17
2018-05-29 2025-02-18
2018-05-30 2025-02-19
2018-05-31 2025-02-20
2018-06-01 2025-02-21 |
|
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3051 Location: NYC,USA
|
|
|
|
Try this,
Code: |
select temp.a, temp2.NXT_5_DT
from table temp,
(select max(temp1.a) as NXT_5_DT, 'A' as NXT_5_DT_F
from table temp1
where temp1.a > '2018-01-01'
and temp1.b = 'N'
and temp1.c = 'N'
fetch first 5 rows only) temp2
where temp.a = '2018-01-01'
'A'= temp2.NXT_5_DT_F |
|
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
Please use the code tags when presenting code/data/anything requiring spacing to be maintained. I have coded your last 2 posts in such a way that you can see the difference. |
|
Back to top |
|
|
Ron Klop
New User
Joined: 28 Sep 2012 Posts: 28 Location: holland
|
|
|
|
select X.dat, Y.NXT_5_DT
from gwsddba5.gwtb403 X ,
(select max(a.dat) as NXT_5_DT, 'A' as NXT_5_DT_F
from gwsddba5.gwtb403 a
where a.dat > '2018-01-01'
and A.NED_WKNd_IND = 'N'
AND A.NED_FSTD_IND = 'N'
group by a.dat
order by a.dat asc
fetch first 5 rows only) Y
where x.dat = '2018-01-01'
and 'A'= Y.NXT_5_DT_F
;
---------+---------+---------+---------+---------+-----
DAT NXT_5_DT
---------+---------+---------+---------+---------+-----
2018-01-01 2018-01-02
2018-01-01 2018-01-03
2018-01-01 2018-01-04
2018-01-01 2018-01-05 |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3051 Location: NYC,USA
|
|
|
|
Why did you add group by and order by ? |
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
If you cannot be bothered to present your data properly (using code tags) why should anyone be bothered to help you? Topic locked (and unlocked again). |
|
Back to top |
|
|
|