[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Sql query question

I need to modify this query to make it return paymain records even if there isn't a paymysupdetails record associated with it. I tried putting the (+) after the join clause but it still returns nothing.

Also, I need to rewrite it to run in both oracle and sql server.

Thanks
SELECT paymysupdetails.DATEFROM AS DateFrom,
paymysupdetails.DATETHRU As DateThru,
bpamain.id As BpaMainId, 
bpamain.name As Name, 
paymain.myparentid As MyParentId, 
paymain.MYPAYERID As MyPayerId, 
paymain.MYENDLEVELID As MyEndLevelId, 
paymysupdetails.PAYSUBTYPE As PaySubType, 
paymysupdetails.PAYMENTVOLUME As PaymentVolume, 
paymysupdetails.RATE As Rate, 
paymysupdetails.paymentamount as PaymentAmount,
paymain.AMOUNT As Amount
FROM paymain, 
bpamain, 
(SELECT paymainpkey,
datefrom,
datethru,
paysubtype,
status, 
sum(PAYMENTVOLUME) paymentvolume,
max(rate) rate,
sum(PAYMENTAMOUNT) paymentamount
FROM paymysupdetails
WHERE paymysupdetails.catholdflag <> '1'
GROUP BY paymainpkey, status, datefrom, datethru, paysubtype) paymysupdetails
WHERE paymain.pkey = paymysupdetails.PAYMAINPKEY(+)
AND paymain.BENEFICIARYBPAMAINPKEY = bpamain.pkey 
AND paymain.myphase in ('1','4')
AND paymain.myaudithold <> '1'
AND paymain.MyReviewHold <> '1'
AND paymain.status <> 'd'
AND paymysupdetails.STATUS <> 'd'
AND bpamain.status <> 'd'
AND paymain.pkey = '00100000005osqwk'
ORDER BY paymysupdetails.datefrom, 
paymysupdetails.datethru, 
paymain.myparentid, 
paymain.mypayerid, 
bpamain.id, 
paymysupdetails.paysubtype

Open in new window

0
rutledgj
Asked:
rutledgj
1 Solution
 
dqmqCommented:
This line will never be true when the details row is missing

AND paymysupdetails.STATUS <> 'd'

Change to:

AND (paymysupdetails.STATUS <> 'd' or paymysupdetails.STATUS IS NULL)
0
 
dportasCommented:
SELECT paymysupdetails.DATEFROM AS DateFrom,
paymysupdetails.DATETHRU As DateThru,
bpamain.id As BpaMainId,
bpamain.name As Name,
paymain.myparentid As MyParentId,
paymain.MYPAYERID As MyPayerId,
paymain.MYENDLEVELID As MyEndLevelId,
paymysupdetails.PAYSUBTYPE As PaySubType,
paymysupdetails.PAYMENTVOLUME As PaymentVolume,
paymysupdetails.RATE As Rate,
paymysupdetails.paymentamount as PaymentAmount,
paymain.AMOUNT As Amount
FROM paymain
INNER JOIN bpamain
ON paymain.BENEFICIARYBPAMAINPKEY = bpamain.pkey
LEFT JOIN
(SELECT paymainpkey, datefrom, datethru, paysubtype, status,
 SUM(PAYMENTVOLUME) paymentvolume,
 MAX(rate) rate,
 SUM(PAYMENTAMOUNT) paymentamount
 FROM paymysupdetails
 WHERE paymysupdetails.catholdflag <> '1'
 GROUP BY paymainpkey, status, datefrom, datethru, paysubtype) paymysupdetails
ON paymain.pkey = paymysupdetails.PAYMAINPKEY
AND paymysupdetails.STATUS <> 'd'
WHERE paymain.myphase in ('1','4')
AND paymain.myaudithold <> '1'
AND paymain.MyReviewHold <> '1'
AND paymain.status <> 'd'
AND bpamain.status <> 'd'
AND paymain.pkey = '00100000005osqwk'
ORDER BY paymysupdetails.datefrom,
paymysupdetails.datethru,
paymain.myparentid,
paymain.mypayerid,
bpamain.id,
paymysupdetails.paysubtype ;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now