Solved

Select Distinct Rows Sql Server

Posted on 2010-08-31
8
756 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

839 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