Solved

Earliest Start for Multiple Distinct Records

Posted on 2014-03-03
5
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

728 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