Solved

Select Row Where Column 1 is Unique

Posted on 2008-10-13
3
183 Views
Last Modified: 2012-05-05
I need help with a select statement.  I would like to have something like the SQL below, except only return values where the Project.JobNumber is unique.  I would like also to take the last ProjectStatus.StatusDate as the tie breaker.  Please let me know how this can be done.  Thanks!
SELECT        Project.JobNumber, ProjectStatus.StatusType, ProjectStatus.StatusDate, ProjectStatus.AssignDate, Project.ID, StatusType.Description

FROM            Project INNER JOIN

                         ProjectStatus ON Project.ID = ProjectStatus.ProjectID INNER JOIN

                         StatusType ON Project.Status = StatusType.ID AND ProjectStatus.StatusType = StatusType.ID

ORDER BY Project.JobNumber, ProjectStatus.StatusDate

Open in new window

0
Comment
Question by:deloused
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 300 total points
ID: 22707502
Something like this should work - took the latest status date, but if you change the order by in the OVER clause to ASC then you will get first status date instead.
WITH projectsCTE AS (

	SELECT        Project.JobNumber, ProjectStatus.StatusType, ProjectStatus.StatusDate, ProjectStatus.AssignDate, Project.ID, StatusType.Description,

					row_number() OVER (PARTITION BY Project.JobNumber ORDER BY ProjectStatus.StatusDate DESC) As rNum

	FROM            Project INNER JOIN

                ProjectStatus ON Project.ID = ProjectStatus.ProjectID INNER JOIN

                         StatusType ON Project.Status = StatusType.ID AND ProjectStatus.StatusType = StatusType.ID

)

SELECT JobNumber, StatusType, StatusDate, AssignDate, ID, Description

FROM projectsCTE

WHERE rNum = 1

ORDER BY JobNumber, StatusDate

Open in new window

0
 

Author Closing Comment

by:deloused
ID: 31505719
Thank you!  Worked great
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22707548
You are welcome!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

8 Experts available now in Live!

Get 1:1 Help Now