Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have three tables .....
1. ClientDetailTable(clientId as primary key )
2. PaymentDetailTable (paymentId as primary key ) (clientId as foreign key )
3. AddressDetailTable (AddId as primary key) (clientId as foreign key)


now i want to select all clients record which exist in "ClientDetailTable" and their addresses from

"AddressDetailTable " . i have no problem in this (record for each client exist only once in both "AddressDetailTable " and "ClientDetailTable" ) , but when i am trying to join corressponds record from "PaymentDetailTable " i have arise a problem . Actually in "PaymentDetailTable " record exist more than once (paymentId is set identity(1,1)) now i want to select record from "PaymentDetailTable " which has maximum "paymentId " for that client ....
and in this way i want to join these three tables ....pls give suggestion bros......

i hope u understand the problem , waiting for response ......
Posted
Comments
abdussalam143 31-May-13 9:06am    
as i consider you want to display all records of paymentDetail with client and address detail am i right

or

please add some discribtion some sort of record you want to get like blow

clientId | addressid | paymentid
1 | 12 | 101
2 | 13 | 103
1 | 12 | 102
GDdixit 1-Jun-13 7:10am    
i want to show in this manner .

clientId | addressid | paymentid
1 | 12 | 102
2 | 13 | 103


i.e. with max paymentid of that client ....

how to do this ?

This may help

1.You can group by the ClientID in PaymentDetailTable and get the Count(paymentId) from PaymentDetailTable
2.take the max of the result set and join with the Other tables
 
Share this answer
 
SELECT ClientDetailTable.clientId, ClientDetailTable.clientName, AddressDetailTable.Address,PaymentDetailTable.paymentId
FROM ClientDetailTable INNER JOIN
AddressDetailTable ON ClientDetailTable.clientId = AddressDetailTable.clientId INNER JOIN
PaymentDetailTable ON ClientDetailTable.clientId=PaymentDetailTable.clientId
AND PaymentDetailTable.paymentId IN (SELECT MAX(paymentId) FROM PaymentDetailTable GROUP BY clientId)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900