|
View previous topic :: View next topic
|
| Author |
Message |
Learncoholic
New User
Joined: 20 Sep 2007 Posts: 97 Location: India
|
|
|
|
Hi,
I have 2 tables TABLE1 & TABLE2. TABLE1 has colums COL_TB1_1, COL_TB1_2,COL_TB1_3,COL_TB1_4.
TABLE2 has columns COL_TB2_1, COL_TB2_2, COL_TB2_3, COL_TB2_4, COL_TB2_5.
Out of these columns COL_TB1_1 is the primary key of TABLE1 & COL_TB2_1,COL_TB2_2,COL_TB2_3 together forms a unique key for TABLE2,with COL_TB2_2 containing date & COL_TB2_3 containing time.
Now I want to select COL_TB1_1, COL_TB1_3, COL_TB1_4, COL_TB2_4 & COL_TB2_5, such that only the highest value of COL_TB2_2 & COL_TB2_3 is selected for each COL_TB2_1.
I need to do this using SPUFI. I have written the below query which is not working:
| Code: |
SELECT COL_TB1_1, COL_TB1_3, COL_TB1_4, COL_TB2_4, COL_TB2_5
FROM (SELECT COL_TB2_4, COL_TB2_5
FROM TABLE2
WHERE COL_TB2_1 LIKE 'ABC%'
ORDER BY COL_TB2_2 DESC,COL_TB2_3 DESC
FETCH FIRST 1 ROW ONLY) A
WHERE A.COL_TB1_1 = COL_TB2_1
AND B.COL_TB1_2 IN ('01','02') |
The above query is throwing an error code of -199 & error message:
| Code: |
| ILLEGAL USE OF KEYWORD ORDER. TOKEN ) UNION EXCEPT WAS EXPECTED |
Kindly let me know what modifications shall I make for my query to work. A completely new query is also welcome. |
|
| Back to top |
|
 |
dbzTHEdinosauer
Global Moderator

Joined: 20 Oct 2006 Posts: 6965 Location: porcelain throne
|
|
|
|
well, guess you have learned that you can not do an order by in a sub-select.
why don't you just simply join the tables using an ON clause,
do an order by
and then FETCH 1st only? |
|
| Back to top |
|
 |
dbzTHEdinosauer
Global Moderator

Joined: 20 Oct 2006 Posts: 6965 Location: porcelain throne
|
|
|
|
or simply remove the order by and fetch first from the subselect
and put it on the main select?
and I would check an explain output.
would not be suprised if db2 made it a join, anyway |
|
| Back to top |
|
 |
Learncoholic
New User
Joined: 20 Sep 2007 Posts: 97 Location: India
|
|
|
|
Hi dbzTHEdinosauer,
I took the example of using order by in sub-select from this forum & tried it out. The link is: ibmmainframes.com/viewtopic.php?t=7683
There is an info I missed. TABLE1 has a composite key. One of them is COL_TB1_1.
I want to select all the composite key of TBALE1 having specific value in COL_TB1_1.
If I do a join followed by order by clause & then fetch first 1 row only will relay me only 1 row which isn't my intention.
Had I written a COBOL with DB2, the logic would have been as follows:
Step1. Get required columns along with COL_TB2_1 from TABLE2 ordered by date & time values
Step2. Report all values of specific columns from TABLE1 where COL_TB1_1 = COL_TB2_1 |
|
| Back to top |
|
 |
dbzTHEdinosauer
Global Moderator

Joined: 20 Oct 2006 Posts: 6965 Location: porcelain throne
|
|
|
|
maybe in your subselect,
select .......
where col? like 'abc5'
and timestamp(datecol, timecol) = max(timestamp(datecol,timecol))
?????? |
|
| Back to top |
|
 |
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
this time I'll shout it :
you can order by fetch first in a subselect in DB2 9 NFM |
|
| Back to top |
|
 |
dbzTHEdinosauer
Global Moderator

Joined: 20 Oct 2006 Posts: 6965 Location: porcelain throne
|
|
|
|
| and when you don't have DB2 9 NFM? |
|
| Back to top |
|
 |
|
|