Groups | Blog | Home
all groups > sql server (alternate) > february 2004 >

sql server (alternate) : Select Distinct - Incorrect result


Pat L
2/21/2004 11:24:39 PM
I have a function that is designed to return a variable that contains
concatenated values from a partinular field in the returned rows:

DECLARE @output varchar(8000)
SELECT
@output =
CASE
WHEN @output IS NULL THEN CAST(TSD.ScheduledTime AS
varchar(4))
ELSE @output+ ', '+ ISNULL(CAST(TSD.ScheduledTime AS
varchar(4)),'')
END
FROM TSD
WHERE ClientGUID = 2000001447020001 AND
ParentGUID = 6000006684068001
Select @output

The variable returned with this code contains:

"1200, 1400, 1200, 1400"

I want to only get the unique values so that the variable returns "1200,
1400". Seems simple enough just to add DISTINCT to the SELECT statement.
However, what is returned is simply "1400".

I cannot figure out why that is the case. Is there any explanation to this
result?

Side note: I can work around this by using a cursor but I would like to
know why DISTINCT does not work.

Many thanks in advance for any help that can be provided!

Pat


Erland Sommarskog
2/21/2004 11:39:24 PM
Pat L (istuff@telusZ.net) writes:
[quoted text, click to view]

The result of this query is undefined. You may get what you want, you
may get something else. It appears that when you use DISTINCT, the latter
applies.

See http://support.microsoft.com/default.aspx?scid=287515. Pay
particular attention to the first paragraph under CAUSE.

So a cursor is the way to go this time.


--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
Pat L
2/22/2004 12:11:46 AM

[quoted text, click to view]

Thanks you so much for the quick response Erland!

I read the article but since it does not actually use or refer to DISTINCT,
and I don't use the order by clause, where is the connection?

Pat

David Portas
2/22/2004 9:54:45 AM
DISTINCT performs an implict sort of the table with the same effect.

From the doc that Erland posted: "The correct behavior for an aggregate
concatenation query is undefined". In other words the behaviour your query
assumes is an undocumented feature and there are no guarantees that the
result will always be what you expect.

The result you described with SELECT DISTINCT is actually correct according
to BOL:

SELECT @local_variable is usually used to return a single value into the
variable. It can return multiple values if, for example, expression is the
name of a column. If the SELECT statement returns more than one value, the
variable is assigned the last value returned.

http://msdn.microsoft.com/library/en-us/tsqlref/ts_sa-ses_978l.asp

Returning a comma-delimited list from a table is a presentational task which
may be better done in your client application or reporting tool.

--
David Portas
SQL Server MVP
--

Erland Sommarskog
2/22/2004 2:07:01 PM
Pat L (istuff@telusZ.net) writes:
[quoted text, click to view]

There is no connection to DISTINCT, but there is a connection to things
like "SELECT @x = @x + col FROM tbl" in general, and the article says
that a statement like this has no defined result.


--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
Pat L
2/22/2004 8:03:49 PM
Thanks David (and again to Erland).

Pat

[quoted text, click to view]

AddThis Social Bookmark Button