sql server - SQL query one to many relationship join without duplicates -


i'm running queries in sql server 2008.. have sales table , payments table.. sale has multiple methods of payment (part giftcard + part cash or part credit + part cash etc..) want list sales , payments each sale in table.

if left join on sales.saleid = payments.saleid duplicate sales rows when there more 1 matching payment rows..

so have been doing getting sales , count of how many matching payment rows there (select count(*) payments payments.saleid = sales.saleid) numofpayments. in php script check number of payments , if > 1 run query payment details.

the output trying 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 , 127 have multiple payments listed sale information appears once , not duplicated each payment.

the sales , payments tables 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 if can sql faster. there way accomplish pure sql instead of having use server side code run conditional queries.

i wouldn't mix data retrieval , data display, think asking about. have sort of column indicate payment should displayed first? i'm thinking like:

select columnlist,  rn = row_number() on (partition sales.salesid order payment.paymentid) sales join payments on sales.salesid=payments.salesid 

then, in gui, display values first 3 columns rn = 1, , blank out values rn > 1.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -