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

First value of multiple subsets

$
0
0

I have an interesting problem.  I need to query a list of customers, and retrieve the first row in the latest subset.  I have created a version, but it is way to complex.

Example:

Cust_Num     Date         Value

1002             201001     D

1002             201002    

1002             201003     D

1002             201004     D

1002             201005 

In an instance like this I need the minimum date from the second subset, where the value is D and the date I search is 201004.  So I need a query that returns the minimum date within a subset of the value D.  Subset 1 would be the row with date 201001, and the second subset would be 201004 and 201003.  So if I search for customer 1002 with a date of 201004, I need the date 201003.  The minimum date within a searched subset.  I tried to use RANK and ROW_NUMBER, but the functiuons sees all of the D valued rows as a single set.  Hope this makes sense.


Viewing all articles
Browse latest Browse all 23857

Trending Articles