Solved

Order by doesn't work in MS SQL

Posted on 2010-09-04
2
364 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Logical Operator should return Integer value in SSIS 9 36
Need SSIS project 2 27
Split string into 3 separate fields 5 20
Need age at date of document 5 17
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

749 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