Solved

Select Row Where Column 1 is Unique

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Trigger or Function that updates table with old values 5 44
SQL Agent Timeout 5 59
Set the max value for a column 7 39
Query 14 55
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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