Solved

Earliest Start for Multiple Distinct Records

Posted on 2014-03-03
5
137 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 59

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 65

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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 124
Counting Distinct values in two columns 3 15
backups - Strategies 1 13
Join vs where 2 11
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now