473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Getting Error sql0338n

Hi,
I am running a following query in DB2 and getting SQL0338n error.
This is primarily because, there are subqueries at few places (marked in bold)in the main query, after 'ON' clause.
Could anyone please help me, as how these subqueries can be replaced without change in resultset?

Thanks,
Tejas.



--------------------------------------------------------------------------------
Select
.
.
.
FROM join_is JID
LEFT OUTER JOIN AD_PH ph
ON JID.h_num=ph.h_num
AND JID.ph_ts=ph.ts
LEFT OUTER JOIN AD_COM cmd
ON ph.cmd_cd = cmd.cmd_cd
AND cmd.ts =
(
SELECT max(ts)
FROM AD_COM cmd_sub
WHERE cmd_sub.v_frm <= @start
AND cmd_sub.cmd_cd = ph.cmd_cd
)
LEFT OUTER JOIN AD_PS ps
ON JID.p_num=ps.p_num
AND JID.ps_ts=ps.ts
LEFT OUTER JOIN PSM
ON JID.p_num=PSM.p_num
LEFT OUTER JOIN AD_FORM af
ON JID.h_num = af.h_num
AND af.ts =
(
SELECT max(ts)
FROM AD_FORM af_sub
WHERE af_sub.v_frm <= @start
AND af_sub.h_num = JID.h_num
)
LEFT OUTER JOIN AD_FC afc
ON JID.h_num = afc.h_num
AND afc.ts =
(
SELECT max(ts)
FROM AD_FC afc_sub
WHERE afc_sub.v_frm <= @start
AND afc_sub.h_num = JID.h_num
),

AD_PCR pcr
LEFT OUTER JOIN
(
SELECT sub_p.ps_num AS m_ps_num, max(f_pr_dt) AS match_f_pr_dt
FROM AD_PCR sub_p, PSM ps_ids
WHERE sub_p.ps_num = ps_ids.ps_num
AND sub_p.ts =
(SELECT MAX(ts) FROM AD_PCR sub_p2 WHERE sub_p2.p_num = sub_p.p_num AND sub_p2.v_frm < @end)
AND sub_p.ad_ty_ind != 'D'
AND sub_p.td_st_ind != 'C'
GROUP BY sub_p.ps_num
HAVING MAX(sub_p.f_pr_dt) <= @start
) match ON match.m_ps_num = pcr.ps_num
LEFT OUTER JOIN #efp efp ON efp.p_num = pcr.p_num
LEFT OUTER JOIN AD_BD bd
ON bd.p_num IN
(
SELECT head_bd.p_num
FROM AD_BD head_bd
WHERE head_bd.bd_num = pcr.bd_num
AND head_bd.ts =
(
SELECT max(ts)
FROM AD_BD head_bd_sub
WHERE head_bd_sub.v_frm <= @start
AND head_bd_sub.bd_num = head_bd.bd_num
)
)
AND bd.ts =
(
SELECT max(ts)
FROM AD_BD bd_sub
WHERE bd_sub.v_frm <= @start
AND bd.bd_num = bd_sub.bd_num
)
AND bd.pump_dt IS NOT NULL
AND bd.ad_ty_ind != 'D'
AND NOT EXISTS
(
SELECT 1
FROM AD_BD bd_sub2
WHERE bd_sub2.p_num = bd.p_num
AND bd_sub2.ts != bd.ts
AND bd_sub2.ad_ty_ind != 'D'
AND
(
bd_sub2.pump_dt IS NULL
OR (bd.pump_dt <= bd.v_frm AND bd_sub2.ts > bd.ts)
OR (bd.pump_dt > bd.v_frm AND (bd_sub2.pump_dt > bd.pump_dt OR bd_sub2.v_frm > bd.pump_dt))
)

AND bd_sub2.ts =
(
SELECT max(ts)
FROM AD_BD bd_sub3
WHERE bd_sub3.v_frm <= @start
AND bd_sub3.bd_num = bd_sub2.bd_num
)
)
LEFT OUTER JOIN AD_CNT cntr
ON pcr.cnt_num = cntr.cnt_num
AND cntr.ts =
(
SELECT max(ts)
FROM AD_CNT cntr_sub
WHERE cntr_sub.v_frm <= @start
AND cntr_sub.cnt_num = pcr.cnt_num
)
Nov 16 '09 #1
3 2952
You might want to try the following approach:

Expand|Select|Wrap|Line Numbers
  1. -- using first 3 joins as an example
  2. create table join_is(h_num int not null, ph_ts timestamp not null);
  3. create table ad_ph(h_num int not null, ts timestamp not null, cmd_cd varchar(3) not null);
  4. create table ad_com(cmd_cd varchar(3) not null, ts timestamp not null, v_frm int not null);
  5.  
  6. insert into join_is values(0, timestamp('2009-01-01 00:00:00'));
  7.  
  8. insert into join_is values(1, timestamp('2009-01-01 00:00:00'));
  9. insert into join_is values(1, timestamp('2009-01-02 00:00:00'));
  10. insert into ad_ph values(1, timestamp('2009-01-02 00:00:00'), 'CC1');
  11.  
  12. insert into join_is values(2, timestamp('2009-01-01 00:00:00'));
  13. insert into join_is values(2, timestamp('2009-01-03 00:00:00'));
  14. insert into ad_ph values(2, timestamp('2009-01-03 00:00:00'), 'CC2');
  15. insert into ad_com values('CC2', timestamp('2009-01-02 00:00:00'), 50);
  16. insert into ad_com values('CC2', timestamp('2009-01-03 00:00:00'), 50);
  17. insert into ad_com values('CC2', timestamp('2009-01-04 00:00:00'), 150);
  18. insert into ad_com values('CCX', timestamp('2009-01-04 00:00:00'), 50);
  19.  
  20. insert into join_is values(3, timestamp('2009-01-01 00:00:00'));
  21. insert into join_is values(3, timestamp('2009-01-04 00:00:00'));
  22. insert into ad_ph values(3, timestamp('2009-01-04 00:00:00'), 'CC3');
  23. insert into ad_com values('CC3', timestamp('2009-01-06 00:00:00'), 150);
  24.  
  25. -- expected results:
  26. -- 0, NULL, NULL
  27. -- 1, NULL, NULL
  28. -- 1, 'CC1', NULL
  29. -- 2, NULL, NULL
  30. -- 2, 'CC2', 2009-01-03 00:00:00
  31. -- 2, NULL, NULL
  32. -- 3, 'CC3', NULL
  33.  
  34. -- fails with SQL0338N
  35. SELECT JID.h_num, PH.cmd_cd, CMD.ts
  36.   FROM join_is JID
  37.   LEFT OUTER JOIN
  38.        ad_ph    PH
  39.     ON JID.h_num = PH.h_num
  40.    AND JID.ph_ts = PH.ts
  41.   LEFT OUTER JOIN
  42.        ad_com  CMD
  43.     ON PH.cmd_cd = CMD.cmd_cd
  44.    AND CMD.ts = (SELECT max(ts)
  45.                    FROM ad_com CMD_SUB
  46.                   WHERE CMD_SUB.v_frm <= 100
  47.                     AND CMD_SUB.cmd_cd = PH.cmd_cd);
  48.  
  49. -- rewritten to left outer join to nested table expression which performs aggregation
  50. SELECT JID.h_num, PH.cmd_cd, CMD.ts
  51.   FROM join_is JID
  52.   LEFT OUTER JOIN
  53.        ad_ph    PH
  54.     ON JID.h_num = PH.h_num
  55.    AND JID.ph_ts = PH.ts
  56.   LEFT OUTER JOIN
  57.        (SELECT cmd_cd,
  58.                max(ts) as ts
  59.           FROM ad_com
  60.          WHERE v_frm <= 100
  61.          GROUP BY cmd_cd) CMD
  62.     ON PH.cmd_cd = CMD.cmd_cd
  63.  ORDER BY 1;
  64.  
  65. -- similar but uses OLAP function to perform aggregation - potentially more efficient
  66. SELECT JID.h_num, PH.cmd_cd, CMD.ts
  67.   FROM join_is JID
  68.   LEFT OUTER JOIN
  69.        ad_ph    PH
  70.     ON JID.h_num = PH.h_num
  71.    AND JID.ph_ts = PH.ts
  72.   LEFT OUTER JOIN
  73.        (SELECT distinct cmd_cd, max(ts) over(partition by cmd_cd) as ts
  74.           FROM ad_com
  75.          WHERE v_frm <= 100) CMD
  76.     ON PH.cmd_cd = CMD.cmd_cd
  77.  ORDER BY 1;
  78.  
  79. drop table join_is;
  80. drop table ad_ph;
  81. drop table ad_com;
Nov 18 '09 #2
Hi Cburnett,
Can we have some other simple approach than this?
Because the query I gave is just a small part of entire stored procedure; hence if I go ahead with creation of temporary tables / insertion of rows in temporary table; then it'll be an overhead; consuming more time...
Is there any alternative solution for this?


Regards,
Tejas.
Nov 19 '09 #3
Tejas,
I think you may be reading too much into my example. This is a setup and test to show the effect of the changes rather than a rewrite of your problem.

For example, I'm not proposing any temporary tables; just a modificaiton of the SQL. Look particularly at lines 41-47 (of your original problem). I am proposing that you replace these lines with EITHER lines 56-62 OR lines 72-76, depending on whichever performs better.
Nov 19 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
32
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
2
by: MSK | last post by:
Hi, Continued to my earlier post regaring "Breakpoints are not getting hit" , I have comeup with more input this time.. Kindly give me some idea. I am a newbie to .NET, recently I installed...
0
by: MSK | last post by:
Dear all, Continued to my earlier mail regarding "Breaking points are not getting hit in ASP.NET " , I got some idea from net. Can anyone give me more idea on this? Just to remind the issue:...
4
by: Sascha.Moellering | last post by:
Hi, the following SQL-Statement does not compile, I receive SQL0338N as an error. The erroneous parts of the statement are the left-outer-Joins, but I don't know how to solve the problem, any...
2
by: Manikandan | last post by:
Hi, I have a program written in .Net Framework 1.1 using Visual studio enterprise edition 2003. I tried compiling the same program in visual c# express edition 2005. I'm getting following...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.