473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Cannot use ORDER BY with DISTINCT and CONVERT

The following SQL code works fine if I don't use the CONVERT to make my column two characters wide. When using it as follows the error message reads:
"ORDER BY items must appear in the select list if SELECT DISTINCT is specified." If I just use:
SELECT DISTINCT MONTH(CTNoteDate) it works fine.

How can I run this query with the column width set and sort the results?

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT CONVERT(varchar(2),MONTH(CTNoteDate)) MO,
  2.                 DATENAME(month,CTNoteDate) MONTH 
  3. FROM CTNotes WHERE YEAR(CTNoteDate)=2014
  4. ORDER BY MONTH(CTNoteDate)
Thanks! ~GregM
May 14 '14 #1

✓ answered by Rabbit

Please use code tags when posting code or formatted data.

If you're going to use an ORDER BY with a DISTINCT, whatever is in the ORDER BY must also be in the SELECT.

Therefore you have 2 solutions, include what's in your ORDER BY in your SELECT:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT CONVERT(varchar(2),MONTH(CTNoteDate)) MO,
  2.                 DATENAME(month,CTNoteDate) MONTH,
  3.                 MONTH(CTNoteDate)
  4. FROM CTNotes WHERE YEAR(CTNoteDate)=2014
  5. ORDER BY MONTH(CTNoteDate)
Or change your ORDER BY to match the expression in the SELECT:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT CONVERT(varchar(2),MONTH(CTNoteDate)) MO,
  2.                 DATENAME(month,CTNoteDate) MONTH 
  3. FROM CTNotes WHERE YEAR(CTNoteDate)=2014
  4. ORDER BY CONVERT(varchar(2),MONTH(CTNoteDate))

3 2260
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code or formatted data.

If you're going to use an ORDER BY with a DISTINCT, whatever is in the ORDER BY must also be in the SELECT.

Therefore you have 2 solutions, include what's in your ORDER BY in your SELECT:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT CONVERT(varchar(2),MONTH(CTNoteDate)) MO,
  2.                 DATENAME(month,CTNoteDate) MONTH,
  3.                 MONTH(CTNoteDate)
  4. FROM CTNotes WHERE YEAR(CTNoteDate)=2014
  5. ORDER BY MONTH(CTNoteDate)
Or change your ORDER BY to match the expression in the SELECT:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT CONVERT(varchar(2),MONTH(CTNoteDate)) MO,
  2.                 DATENAME(month,CTNoteDate) MONTH 
  3. FROM CTNotes WHERE YEAR(CTNoteDate)=2014
  4. ORDER BY CONVERT(varchar(2),MONTH(CTNoteDate))
May 14 '14 #2
Very sorry about forgetting the code tags.

Thank you very much -- it makes all the sense in the world, I just couldn't figure out HOW to include the select code in the order.

Have a great day!
May 14 '14 #3
Rabbit
12,516 Expert Mod 8TB
No problem, good luck with the rest of your project!
May 14 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: issac | last post by:
Hi folks Im trying to do a simple query involving the distinct keyword and an access 2000 db, but have been frittering with it for amost and hour and a half and I cant make it work. This is...
2
by: mavis | last post by:
Could you please help me with this xsd definition? I need to define a set of elements occur in any order with multiple occurrences, but the same kind of elements need to group together... Sth...
14
by: JimN1 | last post by:
Hi, I am getting the following error message when I execute my query. I am trying to get my results in XML Explicit format. The error message is:Name cannot begin with the '.' character, hexadecimal...
1
by: TheLearner | last post by:
(Newbie) Greetings I am trying to create an Index page in Serif Webplus10. Regretfully the application doesn't have that feature I need. I would like to load 4/8 images ( picture +...
2
by: atulsaxena1981 | last post by:
Dear All, I am working exchange server & fetching task of outlook 2007 through MSXML2.XMLHTTP30 now I am facing the following error "Name cannot begin with the '0' character, hexadecimal...
1
by: ebusiness | last post by:
I am trying to create nicknames for tables in a remote database. The federated database and remote database are both DB2 UDB V8.2, both OS are AIX. Could anyone tell me how to create a nickname...
4
by: =?Utf-8?B?UmljaEI=?= | last post by:
Can someone confirm if my no spam alias is set up correctly to get answers to questions. I have been talking to the concierge for over a week to get an answer to a question. Each time I am told to...
1
by: meskeleton | last post by:
Hello everyone... When i try to access the site helm.manashosting.biz\ or http:\\69.50.194.212:8086\ The webpage does not display On clicking diagnose the problem link, It says cannot...
8
by: penfold33 | last post by:
Hi I need to generate a CSV of names and address from a table and I would like only one result (it doesn't matter which) per distinct email address. If possible, they also need to be ordered by...
1
by: Steven Payne | last post by:
I am getting a "mysqlcppconn-static.lib(mysql_driver.obj) : fatal error LNK1313: ijw/native module detected; cannot link with pure modules" error message in Visual C++ Stan Edit 2008. I am simply...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.