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 November 2012 - Why is ORDER BY NEWID() not returning all values for Rowset concatenation?

Nov 14 2012 12:00AM by Madhivanan   

Consider the following set of data and queries

declare @t table(item_id int identity(1,1),item varchar(10))
insert into @t(item)
select 'TV' union all
select 'Computer' union all
select 'Laptop' union all
select 'VCD' union all
select 'CD' 

--Query 1
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+','+item from @t order by item_id desc
select right(@sql ,len(@sql)-1) as concatenated_items

--Query2
set @sql=''
select @sql=@sql+','+item from @t order by newid()
select right(@sql ,len(@sql)-1) as concatenated_items


The result of Query 1 is
concatenated_items
-----------------------------
CD,VCD,Laptop,Computer,TV


and the result of query 2 is
concatenated_items
-----------------------------
VCD

Note that the result of query2 is random and you get only one item always

The question is "Why is newid() not returning all the items in random order for concatenation and why does it choose only one randow item?"

 

 

Tags: 


Madhivanan
3 · 39% · 12419
6
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

4  Comments  

  • newid() returns one globally unique identifier and the result set is randomly sorted, also it return a single random row, so query2 is returning only one item in random way.

    commented on Nov 14 2012 12:38AM
    Manas Ranjan Dash
    39 · 5% · 1482
  • This issue (or behaviour) was discussed at sqlservercentral.com a few years ago as well as bug report was posted. Though MS answered that it was not a bug, this behavior was by design.

    commented on Nov 14 2012 12:47AM
    Olga Medvedeva
    66 · 3% · 843
  • To answer your question:

    Q1: Why is newid() not returning all the items in random order for concatenation and why does it choose only one randow item?

    Using expressions in ORDER BY clause can result in undefined behavior, agree with @Olga.

    Excerpt from MS KB article, link: http://support.microsoft.com/kb/287515

    SQL Server query processor builds an different execution plan when expressions are applied to columns in the query's ORDER BY clause, than when those same expressions are applied to columns in the query's SELECT list. The decision made by the query processor is based on the cost of possible execution plans. The ANSI SQL-92 specification requires that any column referenced by an ORDER BY clause match the result set, defined by the columns present in the SELECT list. When an expression is applied to a member of an ORDER BY clause, that resulting column is not exposed in the SELECT list, resulting in undefined behavior. Additionally, the ANSI specifications regarding how an ORDER BY clause is to be applied state that the effect of the ORDER BY should be the same as if you take the entire result set produced by the SELECT list, and then perform the ordering of the table based on those columns in the SELECT list.

    commented on Nov 14 2012 1:03AM
    Manoj
    245 · 1% · 183
  • The answer lies in the execution plan: Query #1 (ORDER BY item_id): The SORT operator is seen before the Compute Scalar operator is seen. The sort on item_id therefore happens first, and all that is left for the Compute Scalar operator to do is to concatenate the two strings (sql and item) based on the order referenced by the item_id.

    Query #2 (ORDER BY newid()): Here, the situation is reversed. Compute Scalar operation is performed before the SORT operation. The "newid()" returns a globally unique identifier and in this query is evaluated alongwith the concatenation operation. This value is also referenced by the sort operator, resulting in only one record being returned.

    To cross-check, a simple modification can be made to the query:

    select @sql=@sql+','+item from @t order by (SELECT newid())
    

    Running the query now with the execution plan ON shows us that for all records/items in the @t table, a Compute Scalar operation is executed which returns a newid() for each record. The SORT is now executed before the final Compute Scalar operation (which is the one actually concatenating the data) - because of which SQL Server concatenates data from all records.

    commented on Nov 14 2012 1:12AM
    Nakul Vachhrajani
    4 · 33% · 10564

Your Comment


Sign Up or Login to post a comment.

"Question of the month November 2012 - Why is ORDER BY NEWID() not returning all values for Rowset concatenation? " rated 5 out of 5 by 6 readers
Question of the month November 2012 - Why is ORDER BY NEWID() not returning all values for Rowset concatenation? , 5.0 out of 5 based on 6 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]