Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Earliest Start for Multiple Distinct Records

Posted on 2014-03-03
5
Medium Priority
?
171 Views
Last Modified: 2014-05-02
Ok, I am not sure if I worded the title correctly.  I have looked at numerous examples on here and on SO, but nothing seems to work as they all have 'Where' clauses that I can't get to work.  

I have a table that has a date/time column.  I want to get the earliest time for each distinct item in another column.  Here, take a look at my data.

SQL Table
'Machine' is my distinct column.  You can see the highlighted records that I want it to return.  I want to get the orderNumber, Tool, and Color for each of the earliest dates.  I have tried numerous things, but nothing seems to really work.  Thank you for any help you can give me on this - it has been driving me crazy for days.
0
Comment
Question by:G Scott
5 Comments
 
LVL 1

Author Comment

by:G Scott
ID: 39900250
SELECT        *
FROM            (SELECT        *, ROW_NUMBER() OVER (PARTITION BY masch_nr
                          ORDER BY fulltime) rn
FROM            u_order_start_times) q
WHERE        rn = 1


That did it.  I will leave it here to help others.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 39900293
I am glad you figured that out.  If you select your comment above as the solution, it will close the question (so the Experts understand you solved the problem); however, it will leave the topic available to help others.

With that in mind, here is a link for future readers that may help understand the solution.
http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_1555-Analytical-SQL-Where-do-you-rank.html
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39900408
>I want to get the orderNumber, Tool, and Color for each of the earliest dates.
There's an article called SQL Server GROUP BY Solutions, where point #5 'Aggregate AND values from a single row that make up the aggregate:  Subquery' is a demo of exactly what you're trying to pull off.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39902306
That did it.  I will leave it here to help others.
Ouch!  You may want to look into using a CTE in this case.  You may find that a tad more efficient and easier to read.
0
 
LVL 35

Accepted Solution

by:
David Todd earned 1500 total points
ID: 39902417
Hi,

select
    orderNumber, machine, Tool, Color , min( StartTime ) as EarliestDate
from dbo.u_order_start_times
group by
    orderNumber, machine, Tool, Color
;

HTH
  David
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question