Hi,
Forgot to mention few important parts required.
The actual query is
Code:
SELECT T1.ASSOCID, T1.ASSOCTP,(T1.ASPYAMT - T1.ASPROFEE),
T2.LSTNAM,T2.FSTNAM,
T4.LSTNAM,T4.FSTNAM
FROM ASSOC_MASTER_PYT T1 ,
ASSOC_ASS_NAMES T2 ,
(SELECT * FROM ASSOC_ASS_NAMES WHERE NAMCDE = '1') T4
WHERE (T1.ASSOCID = T2.ASSOCID)
AND (T1.ASSOCID = T4.ASSOCID)
AND (T1.CKISSUEST = 'H' AND T1.CKPYDTE = '2008-04-18')
AND T2.NAMCDE = '2'
This query given above presents unique ASSOCID along with selected columns.
For Example currently this results in result set with 5 records.
Now, I need to add one more fee component in (T1.ASPYAMT - T1.ASPROFEE) which makes it to be (T1.ASPYAMT - T1.ASPROFEE - T5.MISCFEE). The updated query would be as below.
Code:
SELECT T1.ASSOCID, T1.ASSOCTP,(T1.ASPYAMT - T1.ASPROFEE - T5.MISCFEE),
T2.LSTNAM,T2.FSTNAM,
T4.LSTNAM,T4.FSTNAM
FROM ASSOC_MASTER_PYT T1 ,
ASSOC_ASS_NAMES T2 ,
(SELECT * FROM ASSOC_ASS_NAMES WHERE NAMCDE = '1') T4
(SELECT * FROM ASSOC_MISC_FEE WHERE CKPYDTE = '2008-04-18') T5
WHERE (T1.ASSOCID = T2.ASSOCID)
AND (T1.ASSOCID = T4.ASSOCID)
*****EXTRA CONDITION GOES HERE*************
AND (T1.ASSOCID = T5.ASSOCID)
*****EXTRA CONDITION ENDSHERE*************
AND (T1.CKISSUEST = 'H' AND T1.CKPYDTE = '2008-04-18')
AND T2.NAMCDE = '2'
Newly included table may or may not have record for ASSOCID. When I run this query this gives me only 1 record. This is the record for which table T5 has entry.
So Basically I need to subtract based on condition of existence here.
I tried below mentioned case statement but seems like its missing some other condition as well..
Code:
SELECT T1.ASSOCID, T1.ASSOCTP,
CASE T5.MISCFEE
WHEN (SELECT COUNT(*) FROM JISPRO.FCS_DRA_T
WHERE T1.FCCASENO = T5.FCCASENO) > 0
THEN 1
WHEN (SELECT COUNT(*) FROM JISPRO.FCS_DRA_T
WHERE T1.FCCASENO = T5.FCCASENO) = 0
THEN 0
END AS MISCFEE,
(T1.ASPYAMT - T1.ASPROFEE - T5.MISCFEE),
T2.LSTNAM,T2.FSTNAM,
T4.LSTNAM,T4.FSTNAM
Thanks for the reply,
I did try COALESCE function but it ain't working. It results in 1 record only for which misc_fee table has entry.
COALESCE is also missing the records for which entry in MISC_FEE is not found.
SELECT T1.ASSOCID, T1.ASSOCTP,
(T1.ASPYAMT - T1.ASPROFEE - COALESCE(T5.MISCFEE,0)),
T2.LSTNAM,T2.FSTNAM,
T4.LSTNAM,T4.FSTNAM
FROM ASSOC_MASTER_PYT T1 ,
ASSOC_ASS_NAMES T2 ,
(SELECT * FROM ASSOC_ASS_NAMES WHERE NAMCDE = '1') T4
WHERE (T1.ASSOCID = T2.ASSOCID)
AND (T1.ASSOCID = T4.ASSOCID)
AND (T1.CKISSUEST = 'H' AND T1.CKPYDTE = '2008-04-18')
AND T2.NAMCDE = '2'
Here, I am not changing WHERE condition but making changes only to SELECT Statement for inclution of COALESCE.
This gives me result set of 10 records. It is always double number of records than original result set. I believe the reason would be it runs loops twice. Once with MISC_FEE Amount and second without MISC_FEE amount.
However I am not quite sure on why this would happen.
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
Posted: Wed Apr 30, 2008 7:02 pm Post subject:
rajen,
Since you are having fetching the fee column from a different table , it may or may not have a corresponding column to the key column table 1. You need to pad zero for every non matching record from ASSOC_MISC_FEE table. In order to do that you can try something like like (untested code)
Code:
(SELECT A.FCCASENO
,B.MISCFEE AS MISCFEE
FROM ASSOC_MASTER_PYT A
,ASSOC_MISC_FEE B
WHERE B.CKPYDTE = '2008-04-18'
AND A.FCCASENO = B.FCCASENO
UNION
SELECT A.FCCASENO
,INT(0) AS MISCFEE
FROM ASSOC_MASTER_PYT A
,ASSOC_MISC_FEE B
WHERE B.CKPYDTE = '2008-04-18'
AND A.FCCASENO <> B.FCCASENO) T5
The first query fetches all the rows which have a matching record in both ASSOC_MASTER table and ASSOC_MISC_FEE table
and the second query fetches all the rows which do NOT have a match in both tables. with a value of integer 0 as fee (change it decimal(0) if the fee column is decimal) and we join these 2 results using an union.
Now you can store this resultant table as a another table and join it to the main table.
Code:
SELECT T1.ASSOCID,
,T1.ASSOCTP
,(T1.ASPYAMT - T1.ASPROFEE - T5.MISCFEE),
,T2.LSTNAM
,T2.FSTNAM,
,T4.LSTNAM
,T4.FSTNAM
FROM ASSOC_MASTER_PYT T1
,ASSOC_ASS_NAMES T2
(SELECT * FROM ASSOC_ASS_NAMES WHERE NAMCDE = '1') T4
(SELECT A.FCCASENO
,B.MISCFEE AS MISCFEE
FROM ASSOC_MASTER_PYT A
,ASSOC_MISC_FEE B
WHERE B.CKPYDTE = '2008-04-18'
and A.FCCASENO = B.FCCASENO
UNION
SELECT A.FCCASENO
,INT(0) AS MISCFEE
FROM ASSOC_MASTER_PYT A
,ASSOC_MISC_FEE B
WHERE B.CKPYDTE = '2008-04-18'
and A.FCCASENO <> B.FCCASENO) T5
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