Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Get first occurance of records in a group

$
0
0

Hello,

I have a query that I'm trying to get the first occurance of each item grouping when an item changes.  However, the is grouping all similar items together. For example;

Select LocationNumber, ShelfNumber, item, recvDate,

ROW_NUMBER() over (partition by item order by LocationNumber, ShelfNumber, recvDate) as roworder

The query returns something like:

LocationNumber, ShelfNumber, item, recvDate, Roworder

1                         1                 123   01/6/14 16:38:00        3

1                         1                 123   01/04/14 05:35:15      2

1                         1                 456   01/03/14 07:18:23       1

1                         1                 123   01/01/14 08:25:15      1

When I grab all the row_numbers  = 1 this will give me 2 records (should be 3). 

The query needs have the row_number start over whenever an item changes.  In this case there should be 3 records returned.  How would I do this? for example:

LocationNumber, ShelfNumber, item, recvDate, Roworder

1                         1                 123   01/6/14 16:38:00      2

1                         1                 123   01/04/14 05:35:15       1

1                         1                 456   01/03/14 07:18:23       1

1                         1                 123   01/01/14 08:25:15       1

Thanks in advance!


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>