Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Question of the month October 2011 - Duplicate columns in the GROUP BY Clause

Oct 12 2011 1:07AM by Madhivanan   

Consider the following table

create table #t(id int, names varchar(100))
insert into #t
select 1,'name1'
union all
select 2,'name2'

Consider the following select statement

select id from #t
order by id,id

It returns the error

Msg 169, Level 15, State 1, Line 3
A column has been specified more than once in the order by list. Columns in the order by list must be unique.

Becuase in ORDER BY clause the column id is duplicated.

Run the following statement

select id from #t
group by id,id

It returns the resultset without any error.

Why does SQL Server generate error for duplicate columns in ORDER BY Clause and not in GROUP BY Clause?

Tags: tsql, SQL Server, #TSQL, #SQLServer,


Madhivanan
3 · 39% · 12440
10
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

13  Comments  

  • I think that the strong relation between ORDER BY and TABLE colums (one-to-one) is only the question of internal ORDER BY statement realisation with unknown reasons :)

    declare @t table (id int, names varchar(100))
    insert into @t select 1,'name1' union all select 2,'name2'

    select id,id from @t order by id
    --error
    select id,id1 from @t order by id
    --ok
    select id from @t order by id,id
    1
    --ok
    select id from @t order by id,id1,id1
    --error
    select id from @t order by id,id*1,id/1
    --ok

    commented on Oct 13 2011 9:45AM
    ix
    2895 · 0% · 2
  • limitation of group by clause is to have the column in select list, and that condition is satisfied so the statement runs successfully is my guess

    commented on Oct 14 2011 3:21AM
    uzairahm
    514 · 0% · 71
  • Blockquote

    Its a run-time error, however the sql can be parsed.

    When at runtime for ORDER BY, the parser gets the ordinal position of the column specified. If the ordinal positions are repeating, the runtime error occurs. If you are naming id as "id1" as alias and using that, the ordinal position gets changed and executing it fine.

    commented on Oct 14 2011 4:58AM
    Latheesh NK
    55 · 4% · 1125
  • It can cause an invalid memory access when the specification contains two or more duplicate ORDER BY columns.No such problem for GROUP BY

    commented on Oct 17 2011 5:50AM
    Sineetha
    105 · 2% · 492
  • @uzairahm If you're saying that GROUP BY expressions must be in the SELECT clause, that is completely incorrect. I have had several queries where I did not need one of the grouped expressions in the select clause and SQL Server happily accepts that.

    commented on Oct 20 2011 12:02PM
    ErikEckhardt
    65 · 3% · 887
  • I have came accross the same question at Microsoft TechNet Forum and here is an answer to that;

    The reason which i know why Duplicate Column Names are not allowed in ORDER BY Cluase is pretty simple;

    Please see the below SQL what you think it might generate as output, if SQL allows this kind of syntax it will obviously very hard for optimizer to sort which thing to do. To avoid such cases MS SQL doesn't allow Duplicate Column Names.

    All Columns appeared in the ORDER BY Cluase must be unique. Similarly you can't referrence a Column Name by its position in the SELECT list if its Column Name is appearing in the ORDER BY Clause.

    Both SQL statement which i have mentioned below are incorrect in syntax.

    select id from #t order by id,id desc

    select names ,id from #t order by 2,id

    Please let me know if this doesn’t work for you. Hope I have answered you correctly. Also i have put these details on MSDN Library so others users can benefit from it; Take a look at below;

    http://msdn.microsoft.com/en-us/library/ms188385.aspx

    Thanks, Hasham

    commented on Oct 23 2011 2:46AM
    HashamNiaz
    440 · 0% · 88
  • In SQL Server, qualified column names and aliases are resolved to columns listed in the FROM clause. The value must be unique among all columns listed in the SELECT statement as it has to determine the sequence of the sort columns in the ORDER BY clause for the organization of the sorted result set.

    commented on Oct 23 2011 10:48AM
    Kashif Aslam. Follow me on Twitter @KashifAslamM
    2706 · 0% · 3
  • I have tried this query without getting any error....

    Select id From #t Order By id,Case When id>0 Then id Else id End

    commented on Oct 24 2011 12:51AM
    Vinod
    2895 · 0% · 2
  • First and foremost, the difference is in the functionality of the two clauses. GROUP BY is used to group or aggregate values whereas the ORDER BY is used to order the result set. Ordering cannot be done on the same column twice - that is why ORDER BY returns an error.

    GROUP BY allows us to use duplicate GROUPING SETS, which is why the use of the same expression (in this case, a column) twice in the GROUP BY is perfectly legal.

    commented on Oct 25 2011 6:05AM
    Nakul Vachhrajani
    4 · 33% · 10587
  • An ORDER BY clasue changes the order in which result are returned whereas a GROUP BY clause has no effect on the order in which results are returned. Therefore, if ducplicate columns exist within an ORDER BY clause, SQL Server is unable to determine the correct order of a result set.

    commented on Nov 1 2011 12:11AM
    Jeff Wharton
    51 · 4% · 1171
  • Yes you can use GROUPING SETS in a GROUP BY clause however I'm pretty sure that the syntax you need to use is GROUP BY GROUPING SETS (.....) which means that the GROUP BY statement in this question does not use GROUPING SETS. I could be wrong though :-)

    Using GROUP BY with ROLLUP, CUBE, and GROUPING SETS

    commented on Nov 1 2011 6:04AM
    Jeff Wharton
    51 · 4% · 1171
  • also if Order By allow duplicate, some will ask it to sort the first into ASC and the second to DESC, in that case what SQL Server do?????

    commented on Dec 2 2011 6:30PM
    Haneesh
    1493 · 0% · 13
  • Think about what this means: ORDER BY foobar ASC, foobar DESC

    Sequences are not sets; duplicates can matter.

    commented on Jun 5 2012 9:43AM
    jcelko
    444 · 0% · 87

Your Comment


Sign Up or Login to post a comment.

"Question of the month October 2011 - Duplicate columns in the GROUP BY Clause" rated 5 out of 5 by 10 readers
Question of the month October 2011 - Duplicate columns in the GROUP BY Clause , 5.0 out of 5 based on 10 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]