Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Order by doesn't work in MS SQL

Posted on 2010-09-04
2
Medium Priority
?
390 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
[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
2 Comments
 
LVL 6

Accepted Solution

by:
apresence earned 1000 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 1000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

610 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