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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select Customer and Last Transaction

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)
Name
Address....

Transaction Table:
TransactionId(primary key)
CustomerId (foreign key)
TxnDate
amount
....

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?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 c
CROSS APPLY (SELECT TOP 1 amount
FROM Transaction
WHERE CustomerId=c.CustomerId
ORDER BY TransactionId DESC)tn[/code]
Go to Top of Page

geossl
Yak Posting Veteran

85 Posts

Posted - 2008-09-04 : 21:17:30
Yes. Updated the first thread.
Transaction table is having a transaction date

quote:
Originally posted by madhivanan

How do you know "Last customer transaction"?
Do you have date column?

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-05 : 03:05:00
[code]SELECT c.Name,c.Address,tn.amount,...
FROM Customer c
CROSS APPLY (SELECT TOP 1 amount
FROM Transaction
WHERE CustomerId=c.CustomerId
ORDER BY Transactiondate DESC)tn
[/code]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -