Solved

Order by doesn't work in MS SQL

Posted on 2010-09-04
2
344 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
ID: 33605633
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
ID: 33605637
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 31
stored procedures times out after 2 hours in sql server 2014 16 35
sql server insert 12 30
Can > be used for a Text field 6 39
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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

776 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