Solved

Select Row Where Column 1 is Unique

Posted on 2008-10-13
3
189 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 60

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 60

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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