Solved

Select Row Where Column 1 is Unique

Posted on 2008-10-13
3
179 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
Comment Utility
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
Comment Utility
Thank you!  Worked great
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
You are welcome!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query 7 57
Need help with a query 4 43
sql help 5 48
how to eliminate duplicates in a string variable in t-sql? 30 40
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Copy Database Wizard 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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now