Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
addytoe
Starting Member
5 Posts |
Posted - 2009-11-15 : 22:11:35
|
| I have tried using the sub query but it keeps displaying errors to me, Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.I did research and most forums say either use IN or NOT IN but it still do not work in my case... SELECT DISTINCT req.RequestNo, ISNULL((req.RequestAmt - pre.PaymentAmt), req.RequestAmt) AS OutstandingFROM Trn_RequestPayment reqLEFT JOIN Trn_PrePayment pre ON pre.RequestCompanyID = req.RequestCompanyIDAND req.RequestNo = pre.RequestNo --> This works totally fineSample data output of 1st Query REQ091000002 405451.23REQ091000003 10000.00REQ091000004 300000.00REQ091000004 400000.00REQ091000006 22458.232REQ091000008 102325.42REQ091000010 4341.23REQ091000012 34561.87REQ091000013 265157.00REQ091000014 100000.00REQ091000015 9823.00REQ091000016 4204.00REQ091000017 12351.00REQ091000018 20000.00REQ091000019 2834.00REQ091000021 3342.00REQ091000022 54234.00REQ091000023 100000.00REQ091000024 200000.00REQ091000025 2342.00REQ091000026 2342.00REQ091100001 1780401.00REQ091100002 5202000.00REQ091100005 10000000.00REQ091100006 2000000.00REQ091100010 542000.00REQ091100011 8123989.00REQ091100012 274191.29REQ091100013 72348.23REQ091100015 3457.22REQ091100017 7624.23REQ091100018 83765.12REQ091100019 7624.23REQ091100020 83765.12(SELECT PaymentAmt AS [Approve Amount] FROM Trn_PrePayment WHERE IsApproved IN (1)) --> 2nd Query Sample data output of 2nd Query 223423.00244324.0015434.00542812.00220000.00234843.00300000.00Combining both queries together : SELECT DISTINCT req.RequestNo, ISNULL((req.RequestAmt - pre.PaymentAmt), req.RequestAmt) AS Outstanding,(SELECT PaymentAmt AS [Approve Amount] FROM Trn_PrePayment WHERE IsApproved IN (1))FROM Trn_RequestPayment reqLEFT JOIN Trn_PrePayment pre ON pre.RequestCompanyID = req.RequestCompanyIDAND req.RequestNo = pre.RequestNoError displays... Am I going about the wrong direction? I'm using SQL 2005. Thank you for your time and patience |
|
|
prakum
Starting Member
16 Posts |
Posted - 2009-11-15 : 23:17:15
|
| May i know what are the fields presents in Trn_RequestPayment and Trn_PrePayment ...Over that writing the second query inside first does not produce any meaning k...provide me with the field details of two tablesPraveen Kumar |
 |
|
|
addytoe
Starting Member
5 Posts |
Posted - 2009-11-16 : 00:43:30
|
| Hey prakum, The 1st Query or the Trn_RequestPayment is where a payee would send an invoice to the Payer and in this case, it has got a Request No, varchar(50), which is an incremental value and the other field is the Outstanding Amount, which is taking the Payee requested Amount and minus off the PaymentAmt made by the Payer. The 2nd Query or the Trn_PrePayment has got PaymentAmt, money and it is where a payer makes a payment to the invoice but it must be verified or approved first before it goes through.Thus, the field that I want to combine along with the 1st Query to display details of Request No, Outstanding and PaymentAmt. The condition of the PaymentAmt is that I want to show PaymentAmt that has been approved, IsApproved, bit. If I am missing out any details, please let me know as I wish to solve this issue.. Thanks for the time |
 |
|
|
prakum
Starting Member
16 Posts |
Posted - 2009-11-16 : 04:50:57
|
| hey addytoeAs far as i have understood your problem is that you want to display the ReqNo,Outstanding amount and the approved amount...so here is my solution just check out with this if any modification is question reply....Condition is that if that prePayment is not approved it will not show the record..ok SELECT req.RequestNo, ISNULL((req.RequestAmt - pre.PaymentAmt), req.RequestAmt) AS Outstanding,pre.PaymentAmt AS [Approve Amount]FROM Trn_RequestPayment reqLEFT JOIN Trn_PrePayment pre ON pre.RequestCompanyID = req.RequestCompanyIDAND req.RequestNo = pre.RequestNoAND pre.IsApproved=1Praveen Kumar |
 |
|
|
addytoe
Starting Member
5 Posts |
Posted - 2009-11-16 : 05:30:56
|
Thanks alot Praveen !!!It's working and displaying perfectly You made it look so simple and comparing to mine, I must have made something simple into more complex than it was supposed to be. Thanks again ... I definitely something from this |
 |
|
|
prakum
Starting Member
16 Posts |
Posted - 2009-11-16 : 23:53:59
|
| Ya its ok addy....Praveen Kumar |
 |
|
|
|
|
|
|
|