Solved

Order by doesn't work in MS SQL

Posted on 2010-09-04
2
322 Views
Last Modified: 2012-06-22
I have the following SQL statement that returns the correct rows, but they are out of order. They are the days of the week, but I want them in the correct order, such as Sunday, Monday, Teuesday, etc.  The order by on this statement does not return any rows. If I take the order by off, it returns the correct rows, just not in the order I would like. Here it is:

Dim strDayQuery As String = "Select id, InstrumentID, TeacherID, DATENAME(dw , EventStart) As Day, DATEPART(dw, EventStart) As Day2 FROM Events WHERE id IN (SELECT MAX(id) FROM Events WHERE InstrumentID = @InstrumentID And TeacherID = @TeacherID And Available <> 'False' Group by DATENAME(dw , EventStart) Order by DATEPART(dw, EventStart) )"
0
Comment
Question by:mlg101
2 Comments
 
LVL 6

Accepted Solution

by:
apresence earned 250 total points
Comment Utility
Your ORDER BY clause is in the sub-query, you want it in the parent query.  See attached SQL statement (I cleaned it up a little bit):
Your ORDER BY query is buried in the subquery.  Try moving it to the outer query:

SELECT

  id,

  InstrumentID,

  TeacherID,

  DATENAME(dw , EventStart) AS Day,

  DATEPART(dw, EventStart) AS Day2

FROM

  Events

WHERE

  id IN (

    SELECT

      MAX(id)

    FROM

      Events

    WHERE

      InstrumentID = @InstrumentID And

      TeacherID = @TeacherID And

      Available <> 'False'

    GROUP BY

      DATENAME(dw, EventStart)

  )

ORDER BY

  DATEPART(dw, EventStart)

Open in new window

0
 
LVL 8

Assisted Solution

by:Mohit Vijay
Mohit Vijay earned 250 total points
Comment Utility
try this

Dim strDayQuery As String = "Select id, InstrumentID, TeacherID, DATENAME(dw , EventStart) As Day, DATEPART(dw, EventStart) As Day2 FROM Events WHERE id IN (SELECT MAX(id) FROM Events WHERE InstrumentID = @InstrumentID And TeacherID = @TeacherID And Available <> 'False' Group by DATENAME(dw , EventStart)) Order by DATEPART(dw, EventStart)"
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now