473,480 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Select Distinct - Incorrect result

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

Jul 20 '05 #1
5 5377
Pat L (is****@telusZ.net) writes:
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 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, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

"Erland Sommarskog" <so****@algonet.se> wrote in message
news:Xn*********************@127.0.0.1...
<snip> 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, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


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
Jul 20 '05 #3
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...a-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
--
Jul 20 '05 #4
Pat L (is****@telusZ.net) writes:
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?


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, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
Thanks David (and again to Erland).

Pat

"David Portas" <RE****************************@acm.org> wrote in message
news:3r********************@giganews.com...
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...a-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
--

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6242
by: Ralph Freshour | last post by:
I have a question about the following PHP script - I got it off a web site tutorial on how to count users logged into your site - my question is the $PHP_SELF variable - it writes the name of the...
5
11672
by: Martin Feuersteiner | last post by:
Dear Group I'm having trouble with the clause below. I would like to select only records with a distinct TransactionDate but somehow it still lists duplicates. I need to select the...
17
4967
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
6
5534
by: John M | last post by:
Hi, The line below is used to feed a combobox. (It is from a database which is used to log pupil behaviour!) The 'incidents' table contains a list of students who have been involved in incidents....
18
3032
by: mathilda | last post by:
My boss has been adamant that SELECT DISTINCT is a faster query than SELECT all other factors being equal. I disagree. We are linking an Access front end to a SQL Server back end and normally are...
6
13515
by: Bob Stearns | last post by:
I am getting unwanted duplicate rows in my result set, so I added the DISTINCT keyword to my outermost SELECT. My working query then returned the following message: DB2 SQL error: SQLCODE: -214,...
3
12828
by: chrisg | last post by:
Hi all. I'm really stuck with getting the right output from a XML query. Given this structure: create table GENCMP.SCRIPTS ( SCRIPT_ID CHAR(10) not null, PAGE_NO ...
4
3524
by: WiseG1rly | last post by:
Hey everyone! Still working on a site I posted for a while ago. Essentially I have a search function that is populated through by a database in mySQL and PHP. I need a few things to help the...
0
2643
enfuego
by: enfuego | last post by:
The code below give an error of Msg 170, Level 15, State 1, Line 5 Line 5: Incorrect syntax near '='. Msg 170, Level 15, State 1, Line 8 Line 8: Incorrect syntax near '='. I Am not sure why. Plz...
0
7044
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
6908
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
7087
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
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
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...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.