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 |
|
geossl
Yak Posting Veteran
85 Posts |
Posted - 2008-09-04 : 09:32:56
|
| Dear All, There is a table of Customer and Customer Transaction, a one to many table relationship. I would like to use SQL to select the customer information with the last customer transaction. What is the SQL in SQL Server 2005 syntax?Customer Table:CustomerID(primary key)NameAddress....Transaction Table:TransactionId(primary key)CustomerId (foreign key)TxnDateamount.... |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-04 : 09:39:34
|
| How do you know "Last customer transaction"?Do you have date column?MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 09:56:48
|
| [code]SELECT c.Name,c.Address,tn.amount,...FROM Customer cCROSS APPLY (SELECT TOP 1 amount FROM Transaction WHERE CustomerId=c.CustomerId ORDER BY TransactionId DESC)tn[/code] |
 |
|
|
geossl
Yak Posting Veteran
85 Posts |
Posted - 2008-09-04 : 21:17:30
|
Yes. Updated the first thread. Transaction table is having a transaction datequote: Originally posted by madhivanan How do you know "Last customer transaction"?Do you have date column?MadhivananFailing to plan is Planning to fail
|
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-05 : 03:05:00
|
| [code]SELECT c.Name,c.Address,tn.amount,...FROM Customer cCROSS APPLY (SELECT TOP 1 amount FROM Transaction WHERE CustomerId=c.CustomerId ORDER BY Transactiondate DESC)tn[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|