Solved

Select Row Where Column 1 is Unique

Posted on 2008-10-13
3
187 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
[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
  • 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

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

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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