Posted: Tue Jun 07, 2011 12:44 am Post subject: Please explain what this query will do
Hi Can anybody please explain how the following db2 query works(Please mention step by step execution process)
Table names
Code:
DVDCCAR
ARXIP
IPGRP
DVDCSMY
DVDPRD1
LSPI001
GDB2DW2 is Db2 region name
[code:1:e46a8a69d0]
SELECT
DVDCCAR.AR_ID,ARXIP.IP_ID,COALESCE(DVDCSMY.PRCS_DT,'01.01.0001') AS PRCS_DT,
COALESCE(DVDCSMY.LST_STMT_BAL,0) AS LST_STMT_BAL,
COALESCE(DVDCSMY.HDYTD_NET_AMT_PRCH,0) AS HDYTD_NET_AMT_PRCH,
COALESCE(DVDCSMY.CR_LF_FL,0) AS CR_LF_FL,
COALESCE(DVDCSMY.CR_LN,0) AS CR_LN,
CAST(COALESCE(DVDCSMY.HD_PAY_HST,' ') AS CHAR(12)) AS PAY_HS,
COALESCE(X.MAXDATE,'31.12.9999') AS MAXDATE
FROM
GDB2DW2.IPGRP IPGRP
INNER JOIN GDB2DW2.ARXIP ARXIP ON IPGRP.IP_ID = ARXIP.IP_ID
INNER JOIN GDB2DW2.DVDCCAR DVDCCAR ON DVDCCAR.AR_ID = ARXIP.AR_ID
LEFT JOIN GDB2DW2.DVDCSMY DVDCSMY ON DVDCCAR.AR_ID = DVDCSMY.AR_ID,
(SELECT MAX(PRCS_DT) AS MAXDATE FROM GDB2DW2.DVDCSMY
WHERE PRCS_DT <=
(SELECT PARM_VAL FROM GDB2DW2.LSPI001
WHERE APPL_ID = 'DI001B' AND SEQ_NUM = 5)) X
WHERE DVDCSMY.PRCS_DT <
(SELECT PARM_VAL FROM GDB2DW2.LSPI001
WHERE APPL_ID = 'DI001B' AND SEQ_NUM = 5)
WHERE APPL_ID = 'DI001B' AND SEQ_NUM = 5)
AND DVDCSMY.PRCS_DT >=
(SELECT PARM_VAL FROM GDB2DW2.LSPI001
WHERE APPL_ID = 'DI001B' AND SEQ_NUM = 4)
AND DVDCSMY.PRCS_DT < IPGRP.GRP_END_DTE
AND DVDCSMY.PRCS_DT >= IPGRP.GRP_STRT_DTE
AND DVDCSMY.PRCS_DT < ARXIP.END_CST_DT
AND DVDCSMY.PRCS_DT >= ARXIP.EFF_CST_DT
AND EXISTS
(SELECT PD_ID FROM GDB2DW2.DVDPRD1 DVDPRD1
WHERE DVDCCAR.PD_ID = DVDPRD1.PD_ID
AND DVDPRD1.DI_GRP_DSC =
1: This SQL will do nothing because it contains syntax errors.
2: DB2 has no region names.
3: you can forget the left join. It will be interpreted as a normal join because in the outer where clause dvdcsmy.prcs_dt is evaluated any possible result row where prcs_dt would be NULL is removed by this where clause.
4: too much use of coalesce. In some cases (like X.MAXDATE), the column can never be NULL.
5:furthermore table X is not used anywhere and has no correlation with any other table.In fact it could be executed seperately and give the same result.
Looking at 3:,4:,5: the person who wrote this has no idea what he was doing, but I guess the query returned the result he/she was expecting given certain test-cases.
------------------
This is the kind of query you get when solving temporal data
each tablerow has a start and end date
and when joining those tables you constantly must find the row where a certain date falls between [>= <] those dates.
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
Posted: Tue Jun 07, 2011 7:40 am Post subject:
what guyc was trying to tell you is the word DB2 REGION is incorrect.
qualifier or schema is what you meant to say.
two, next time you post, use the bbcode tags.
[code:1:f766c03ad3]
SELECT
DVDCCAR.AR_ID
,ARXIP.IP_ID
,COALESCE(DVDCSMY.PRCS_DT,'01.01.0001') AS PRCS_DT
,COALESCE(DVDCSMY.LST_STMT_BAL,0) AS LST_STMT_BAL
,COALESCE(DVDCSMY.HDYTD_NET_AMT_PRCH,0) AS HDYTD_NET_AMT_PRCH
,COALESCE(DVDCSMY.CR_LF_FL,0) AS CR_LF_FL
,COALESCE(DVDCSMY.CR_LN,0) AS CR_LN
,CAST(COALESCE(DVDCSMY.HD_PAY_HST,' ') AS CHAR(12)) AS PAY_HS
,COALESCE(X.MAXDATE,'31.12.9999') AS MAXDATE
FROM
GDB2DW2.IPGRP IPGRP INNER JOIN GDB2DW2.ARXIP ARXIP
ON IPGRP.IP_ID = ARXIP.IP_ID
INNER JOIN GDB2DW2.DVDCCAR DVDCCAR
ON DVDCCAR.AR_ID = ARXIP.AR_ID
LEFT JOIN GDB2DW2.DVDCSMY DVDCSMY
ON DVDCCAR.AR_ID = DVDCSMY.AR_ID
*****************from here on it gets cluggy - syntax errors...
,(SELECT MAX(PRCS_DT) AS MAXDATE
FROM GDB2DW2.DVDCSMY
WHERE PRCS_DT <= (SELECT PARM_VAL FROM GDB2DW2.LSPI001
WHERE APPL_ID = 'DI001B' AND SEQ_NUM = 5)
****************what is this orphan close-paraenthesis doing?
) X
WHERE DVDCSMY.PRCS_DT < (SELECT PARM_VAL FROM GDB2DW2.LSPI001
WHERE APPL_ID = 'DI001B' AND SEQ_NUM = 5)
WHERE APPL_ID = 'DI001B' AND SEQ_NUM = 5
************** another one:
)
AND DVDCSMY.PRCS_DT >= (SELECT PARM_VAL FROM GDB2DW2.LSPI001
WHERE APPL_ID = 'DI001B'
AND SEQ_NUM = 4)
AND DVDCSMY.PRCS_DT < IPGRP.GRP_END_DTE
AND DVDCSMY.PRCS_DT >= IPGRP.GRP_STRT_DTE
AND DVDCSMY.PRCS_DT < ARXIP.END_CST_DT
AND DVDCSMY.PRCS_DT >= ARXIP.EFF_CST_DT
AND EXISTS (SELECT PD_ID FROM GDB2DW2.DVDPRD1 DVDPRD1
WHERE DVDCCAR.PD_ID = DVDPRD1.PD_ID
AND DVDPRD1.DI_GRP_DSC = _________________ Dick Brenholtz
American living in Varel, Germany
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
Posted: Tue Jun 07, 2011 9:38 am Post subject:
esaccy,
The first thing you need to do learn how to write sql's that are readable and easy to understand. Here is the formatted sql without any changes and see how easy it is to read and understand
[code:1:acfbf65378]
SELECT DVDCCAR.AR_ID
,ARXIP.IP_ID
,COALESCE(DVDCSMY.PRCS_DT,'01.01.0001') AS PRCS_DT
,COALESCE(DVDCSMY.LST_STMT_BAL,0) AS LST_STMT_BAL
,COALESCE(DVDCSMY.HDYTD_NET_AMT_PRCH,0) AS HDYTD_NET_AMT_PRCH
,COALESCE(DVDCSMY.CR_LF_FL,0) AS CR_LF_FL
,COALESCE(DVDCSMY.CR_LN,0) AS CR_LN
,CAST(COALESCE(DVDCSMY.HD_PAY_HST,' ') AS CHAR(12)) AS PAY_HS
,COALESCE(X.MAXDATE,'31.12.9999') AS MAXDATE
FROM GDB2DW2.IPGRP IPGRP
INNER JOIN GDB2DW2.ARXIP ARXIP ON IPGRP.IP_ID = ARXIP.IP_ID
INNER JOIN GDB2DW2.DVDCCAR DVDCCAR ON DVDCCAR.AR_ID = ARXIP.AR_ID
LEFT JOIN GDB2DW2.DVDCSMY DVDCSMY ON DVDCCAR.AR_ID = DVDCSMY.AR_ID
,(SELECT MAX(PRCS_DT) AS MAXDATE
FROM GDB2DW2.DVDCSMY
WHERE PRCS_DT <= (SELECT PARM_VAL
FROM GDB2DW2.LSPI001
WHERE APPL_ID = 'DI001B'
AND SEQ_NUM = 5)) X
WHERE DVDCSMY.PRCS_DT < (SELECT PARM_VAL
FROM GDB2DW2.LSPI001
WHERE APPL_ID = 'DI001B'
AND SEQ_NUM = 5)
AND DVDCSMY.PRCS_DT >= (SELECT PARM_VAL
FROM GDB2DW2.LSPI001
WHERE APPL_ID = 'DI001B'
AND SEQ_NUM = 4)
AND DVDCSMY.PRCS_DT < IPGRP.GRP_END_DTE
AND DVDCSMY.PRCS_DT >= IPGRP.GRP_STRT_DTE
AND DVDCSMY.PRCS_DT < ARXIP.END_CST_DT
AND DVDCSMY.PRCS_DT >= ARXIP.EFF_CST_DT
AND EXISTS (SELECT PD_ID
FROM GDB2DW2.DVDPRD1 DVDPRD1
WHERE DVDCCAR.PD_ID = DVDPRD1.PD_ID
AND DVDPRD1.DI_GRP_DSC =
Besides the duplicate line there is also invalid quotes around BF.
If you (temporarily) forget about start and end date , you get following query :
Code:
SELECT DVDCCAR.AR_ID
,ARXIP.IP_ID
,DVDCSMY.PRCS_DT
,DVDCSMY.LST_STMT_BAL
,DVDCSMY.HDYTD_NET_AMT_PRCH
,DVDCSMY.CR_LF_FL
,DVDCSMY.CR_LN
,DVDCSMY.HD_PAY_HST
FROM GDB2DW2.IPGRP IPGRP
INNER JOIN GDB2DW2.ARXIP ARXIP ON IPGRP.IP_ID = ARXIP.IP_ID
INNER JOIN GDB2DW2.DVDCCAR DVDCCAR ON DVDCCAR.AR_ID = ARXIP.AR_ID
LEFT JOIN GDB2DW2.DVDCSMY DVDCSMY ON DVDCSMY.AR_ID = DVDCCAR.AR_ID
WHERE EXISTS (SELECT PD_ID
FROM GDB2DW2.DVDPRD1 DVDPRD1
WHERE DVDPRD1.PD_ID = DVDCCAR.PD_ID
AND DVDPRD1.DI_GRP_DSC = 'BF'
AND DVDPRD1.END_DATE = '31.12.9999')
AND GDB2DW2.ARXIP.CST_SEQ = '1'
WITH UR;
which is equivalent to
for all IPGRP
* show all ARXIP with CST_SEQ = '1'
* * show all DVDCCAR that have at least one DVDPRD1= ('BF','31.12.9999')
* * if there are any: show all DVDCSMY[/CODE]
Now, because DVDCSMY can vary in time based on PRCS_DT we start adding some extra clauses :
- PRCS_DT must be between some parameters ('DI001B';4&5) in LSPI001
- ARXIP must be effective on PRCS_DT : EFF_CST_DT and END_CST_DT
- IPGRP must be effective on PRCS_DT : GRP_STRT_DTE and GRP_END_DTE
Originally IPGRP,ARXIP,DVDCCAR were shown even if no DVDCSMY existed = LEFT JOIN.
Because of the placement of the last 3 extra clauses IPGRP,ARXIP,DVDCCAR are no longer shown when no DVDCSMY exists for that combination.
As an extra we want to know the max PRCS_DT of DVDCSMY <= param('DI001B';5) , with no regard to IPGRP,ARXIP,DVDCCAR (Table X).
This could have been added for performance reasons: If none exists, everything else is obsolete because no rows will be returned.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum