I'm running queries in SQL Server 2008.. I have a sales
table and a payments
table.. sometimes a sale has multiple methods of payment (part giftcard + part cash or part credit + part cash etc..) so what I want to do is list the sales and the payments for each sale in a table.
If I do a LEFT JOIN ON sales.SaleID = payments.SaleID
I get duplicate sales rows when there are more than one matching payment rows..
So what I have been doing is getting all the sales and a count of how many matching payment rows there are with (SELECT COUNT(*) FROM payments WHERE payments.SaleID = sales.SaleID) AS NumOfPayments
. Then in my PHP script I check the number of payments and if it is > 1
I then run another query to get the payment details.
The output I am trying to get would look something like this
-----------------------------------------------------
| SaleID | SaleDate | Amount | Payments |
-----------------------------------------------------
| 123 | 2013-07-23 | $ 19.99 | Cash: $ 19.99 |
| 124 | 2013-07-23 | $ 7.53 | Cash: $ 7.53 |
| 125 | 2013-07-23 | $174.30 | Credit: $124.30 |
| | | | GiftCard: $ 50.00 |
| 126 | 2013-07-23 | $ 79.99 | Cash: $ 79.99 |
| 127 | 2013-07-23 | $100.00 | Credit: $ 90.00 |
| | | | Cash: $ 10.00 |
-----------------------------------------------------
Where sale 125 and 127 have multiple payments listed but the sale information only appears once and is not duplicated for each payment.
The sales
and payments
tables look like this:
Sales Payments
--------------------------------- --------------------------------------------
| SaleID | SaleDate | Amount | | PaymentID | SaleID | PmtMethod | PmtAmt |
--------------------------------- --------------------------------------------
| 123 | 2013-07-23 | $ 19.99 | | 158 | 123 | 4 | $ 19.99 |
| 124 | 2013-07-23 | $ 7.53 | | 159 | 124 | 4 | $ 7.53 |
| 125 | 2013-07-23 | $174.30 | | 160 | 125 | 2 | $124.30 |
| 126 | 2013-07-23 | $ 79.99 | | 161 | 125 | 3 | $ 50.00 |
| 127 | 2013-07-23 | $100.00 | | 162 | 126 | 4 | $ 79.99 |
--------------------------------- | 163 | 127 | 2 | $ 90.00 |
| 164 | 127 | 4 | $ 10.00 |
--------------------------------------------
I feel like if I can do it with just SQL it will be faster. Is there a way to accomplish this with pure SQL instead of having to use server side code to run conditional queries.