?
Solved

Select Distinct Rows Sql Server

Posted on 2010-08-31
8
Medium Priority
?
765 Views
Last Modified: 2012-06-22
I need to select distinct rows based on 2 columns. If both columns have the same data, then it should only select one. Here is my select statement without distinct:

"SELECT TeacherID, InstrumentID, ScheduleID, right(convert(varchar(19), EventStart, 100),6) As Time, EventStart, EventEnd FROM Schedule WHERE InstrumentID = @InstrumentID and TeacherID = @TeacherID and DatePart(dw, EventStart) = @DayofWeekID"

The 2 columns that it should check for distinct values are:
1 - right(convert(varchar(19), EventStart, 100),6) As Time
2 - DatePart(dw, EventStart)

Basically the only column is "EventStart", but I am extracting the Time from EventStart, and I'm extracting the DayOfWeek from EventStart, which is a datetime field. So if the Day of Week and the Time are the same, I only want unique rows.

Thank you.
0
Comment
Question by:mlg101
7 Comments
 
LVL 8

Accepted Solution

by:
Mikkk earned 1600 total points
ID: 33574176
SELECT TeacherID, InstrumentID, ScheduleID, right(convert(varchar(19), EventStart, 100),6) As Time, EventStart, EventEnd
FROM Schedule
WHERE ID IN ("SELECT MAX(ID) FROM Schedule WHERE InstrumentID = @InstrumentID and TeacherID = @TeacherID and DatePart(dw, EventStart) = @DayofWeekID
Group by right(convert(varchar(19), EventStart, 100),6) As Time, DatePart(dw, EventStart) )


// ID is the primary key of the table
0
 
LVL 7

Expert Comment

by:jhp333
ID: 33574181
Just put "DISTINCT" after the "SELECT":
SELECT DISTINCT TeacherID, InstrumentID, ScheduleID....
0
 
LVL 8

Assisted Solution

by:paololabe
paololabe earned 400 total points
ID: 33574205
only a correction on Mikk answer

SELECT TeacherID, InstrumentID, ScheduleID, right(convert(varchar(19), EventStart, 100),6) As Time, EventStart, EventEnd
FROM Schedule
WHERE  ID IN ("SELECT MAX(ID) FROM Schedule WHERE InstrumentID = @InstrumentID  and TeacherID = @TeacherID and DatePart(dw, EventStart) = @DayofWeekID
Group by right(convert(varchar(19), EventStart, 100),6) , DatePart(dw, EventStart) )

as is not allowed in group by statement
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 8

Expert Comment

by:Mikkk
ID: 33576180
:_(
0
 
LVL 1

Author Comment

by:mlg101
ID: 33577740
Mikkk, in my rush, I thought it was you that corrected your own answer. I should have awarded The points to you because I had it figured out before poalobe posted that. I figured out it didn't need the "as" as soon as I tried running the SQL statement the first time. Again, I thought it was you. I will try to figure out how to change it to accept your answer.
0
 
LVL 8

Expert Comment

by:Mikkk
ID: 33577923
mlg101, don't worry!

See you!
0
 
LVL 1

Author Closing Comment

by:mlg101
ID: 33605107
Changed the award because I accidently awarded all points to the wrong person, although he did help.
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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

850 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