Solved

Select Distinct Rows Sql Server

Posted on 2010-08-31
8
754 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
8 Comments
 
LVL 8

Accepted Solution

by:
Mikkk earned 400 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 100 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

786 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