jesse
jesse

Reputation: 71

Oracle SQL Sort Groups

I have a query that returns PartNum, OrderNum, and NeedDate:

Part    Order    Date 
1       A        8/1 
2       B        6/3
2       C        1/1
3       D        7/1
3       E        6/1

I need to sort this to the following:

Part    Order    Date
2       C        1/1
2       B        6/3

3       E        6/1
3       D        7/1

1       A        8/1

Essentially I need them grouped by part#, and sorted by the minimum date for each part. Is it possible to do this with one query?

Upvotes: 1

Views: 78

Answers (1)

sstan
sstan

Reputation: 36483

You can use an analytic function to associate the minimum NeedDate to each PartNum group, and then order on that:

SELECT PartNum, OrderNum, NeedDate
  FROM SomeTable
 ORDER BY MIN(NeedDate) OVER (PARTITION BY PartNum), PartNum

Sql Fiddle

EDIT: Thank you Bulat for the SQLFiddle link. :)

Upvotes: 2

Related Questions