Solved

Select Distinct Rows Sql Server

Posted on 2010-08-31
8
759 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 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