We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Sql query question

Medium Priority
221 Views
Last Modified: 2013-11-16
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

Comment
Watch Question

Commented:
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)
Commented:
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 ;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.