3 Replies - 854 Views - Last Post: 14 November 2011 - 02:06 AM Rate Topic: -----

#1 menukadevinda   User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

order by query doesnt work

Posted 16 October 2011 - 05:25 AM

hi all,
my order by query doesnt work

here is the table

CREATE TABLE [dbo].[studentPreference](
[id] [int] IDENTITY(1,1) NOT NULL,
[indexNo] [int] NOT NULL,
[Major1] [varchar](5) NOT NULL,
[Major2] [varchar](5) NOT NULL
) ON [PRIMARY]

and here is the sql query
SELECT indexNo,Major1, Major2 FROM studentPreference GROUP BY Major1

plz help me
I got the following error
Column 'studentPreference.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

thx in advance,
menuka

Is This A Good Question/Topic? 0
  • +

Replies To: order by query doesnt work

#2 menukadevinda   User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: order by query doesnt work

Posted 16 October 2011 - 07:51 AM

but this work
select indexNo, Major1,Major1 from studentPreference GROUP BY Major1, indexNO

but output is not what I expect
Was This Post Helpful? 0
  • +
  • -

#3 menukadevinda   User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: order by query doesnt work

Posted 16 October 2011 - 09:23 AM

select indexNo, Major1,Major2 from studentPreference GROUP BY Major1,major2,indexNo

this worked, I guess that when use GROUP by we have to use aggregate functions if not use all the fields after the GROUP by and we have to put it in the order to grop it

cheers,
MEnuka.

This post has been edited by menukadevinda: 16 October 2011 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 November-06   User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 482
  • Joined: 04-January 11

Re: order by query doesnt work

Posted 14 November 2011 - 02:06 AM

You said your ORDER BY query isn't working but there is no order by in your query at all. All you did is use group by. You should not confuse the two. They have different functions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1