Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi,I have a TABLE (really it's the result of a select statement) that has something like this:Customer ID, Order Amount, Order Dateand may have multiple entries per customer.I'm interested in SELECTing the first occurrence of each Customer Id, and ignoring the rest.How do I do that?Thanks.
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2012-01-04 : 17:20:50
How do you define first? For example, if you want the row with the earliest Order Date then you could do this:
select CustomerID, OrderAmount, OrderDatefrom( select CustomerID, OrderAmount, OrderDate, row_number() over (partition by CustomerID order by OrderDate) as RN from YourTable) swhere RN = 1;