[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

selecting from tables, avoiding multiple results

phil2phil

3/19/2007 9:20:00 PM

Hi,
I have two table, Customers and CustomerOrders. Customers has columns
like CustID, email, firstName, lastName, etc...where CustID is the
primary key for each Customer. CustomerOrders has the Order info for
each customer, so if a customer bought 5 times it would have 5
records, CustomerOrders has a primary key called CUORID and a foriegn
key of CustID to try back to the user.
I've got a request to get a list of all Customers and their last order
amount and date of purchase, both amount and purchase date are ni
CustomerOrders. I can pull the customer data fine from Customers, but
doing a join to CustomerOrders is bring me all their orders, but for
this request i need just the
last one from CustomerOrders. Is there a way to do this? Or do i have
to use TempTables, building a query to pull all CustomerOrders entries
with just last orders from each customer and then join that tempTables
entries to Customers to get their name, etc...
Or is there a better and easier way?
Thanks.

2 Answers

David Portas

3/19/2007 9:39:00 PM

0

On 19 Mar, 21:19, "phil2phil" <philtwop...@yahoo.com> wrote:
> Hi,
> I have two table, Customers and CustomerOrders. Customers has columns
> like CustID, email, firstName, lastName, etc...where CustID is the
> primary key for each Customer. CustomerOrders has the Order info for
> each customer, so if a customer bought 5 times it would have 5
> records, CustomerOrders has a primary key called CUORID and a foriegn
> key of CustID to try back to the user.
> I've got a request to get a list of all Customers and their last order
> amount and date of purchase, both amount and purchase date are ni
> CustomerOrders. I can pull the customer data fine from Customers, but
> doing a join to CustomerOrders is bring me all their orders, but for
> this request i need just the
> last one from CustomerOrders. Is there a way to do this? Or do i have
> to use TempTables, building a query to pull all CustomerOrders entries
> with just last orders from each customer and then join that tempTables
> entries to Customers to get their name, etc...
> Or is there a better and easier way?
> Thanks.

SELECT C.CustId, C.FirstName, C.LastName,
O.PurchaseDate, O.PurchaseAmount
FROM Customers AS C
OUTER APPLY
(SELECT TOP 1 CustId, PurchaseDate, PurchaseAmount
FROM CustomerOrders
WHERE CustId = C.CustId
ORDER BY PurchaseDate DESC, CustID DESC) AS O;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/librar...(en-US,SQL.90).aspx
--

xyb

3/20/2007 1:52:00 AM

0

On 3?20?, ??5?19?, "phil2phil" <philtwop...@yahoo.com> wrote:
> Hi,
> I have two table, Customers and CustomerOrders. Customers has columns
> like CustID, email, firstName, lastName, etc...where CustID is the
> primary key for each Customer. CustomerOrders has the Order info for
> each customer, so if a customer bought 5 times it would have 5
> records, CustomerOrders has a primary key called CUORID and a foriegn
> key of CustID to try back to the user.
> I've got a request to get a list of all Customers and their last order
> amount and date of purchase, both amount and purchase date are ni
> CustomerOrders. I can pull the customer data fine from Customers, but
> doing a join to CustomerOrders is bring me all their orders, but for
> this request i need just the
> last one from CustomerOrders. Is there a way to do this? Or do i have
> to use TempTables, building a query to pull all CustomerOrders entries
> with just last orders from each customer and then join that tempTables
> entries to Customers to get their name, etc...
> Or is there a better and easier way?
> Thanks.
sql2000 :
SELECT C.CustId, C.FirstName, C.LastName,
O.PurchaseDate, O.PurchaseAmount
FROM Customers AS C
JOIN
(SELECT CustId, MAX(PurchaseDate), PurchaseAmount
FROM CustomerOrders GROUP BY CustId,PurchaseAmount ) AS O
ON C. CustId = O.CustId;