473,425 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

query result as comma-separated list

Hi,
I'n in an environment where I cannot make stored procedures. Now I need
to make a query with a subquery in the SELECT part which gives a comma
separated list of results:

SELECT
p.id,
listFunction(SELECT name FROM names WHERE name_parent=p.id) AS
'nameList'
FROM projects AS p

This query should return something like:
1, "john,mike,petra"
2, "bob,carl,sandra,peter,
etc

listFunction is (of course) not (yet) defined. Is this possible without
the use of stored procedures?
Mike

Jul 23 '05 #1
12 29046
insomniux (mi*************@hccnet.nl) writes:
I'n in an environment where I cannot make stored procedures. Now I need
to make a query with a subquery in the SELECT part which gives a comma
separated list of results:

SELECT
p.id,
listFunction(SELECT name FROM names WHERE name_parent=p.id) AS
'nameList'
FROM projects AS p

This query should return something like:
1, "john,mike,petra"
2, "bob,carl,sandra,peter,
etc

listFunction is (of course) not (yet) defined. Is this possible without
the use of stored procedures?


In SQL 2000, it is actually not possible even with stored procedures
yo write such a function. You will have to write an iterative
solution that iterates over the table, and which uses a temp table
to aggregate the columns.

In SQL2005 you can do this in a single query, thanks to some of the
new XML stuff in SQL 2005.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
I know there are various aggregate functions for numeric columns (like
min, max, ...). Wouldn't it be possible to write an aggregate function
for string-type columns which would enable to write the query as:

SELECT
p.id,
(SELECT listFunction(name) FROM names WHERE name_parent=p.id GROUP
BY name_parent) AS
'nameList'
FROM projects AS p

Erland Sommarskog wrote:
insomniux (mi*************@hccnet.nl) writes:
I'n in an environment where I cannot make stored procedures. Now I need to make a query with a subquery in the SELECT part which gives a comma separated list of results:

SELECT
p.id,
listFunction(SELECT name FROM names WHERE name_parent=p.id) AS
'nameList'
FROM projects AS p

This query should return something like:
1, "john,mike,petra"
2, "bob,carl,sandra,peter,
etc

listFunction is (of course) not (yet) defined. Is this possible without the use of stored procedures?


In SQL 2000, it is actually not possible even with stored procedures
yo write such a function. You will have to write an iterative
solution that iterates over the table, and which uses a temp table
to aggregate the columns.

In SQL2005 you can do this in a single query, thanks to some of the
new XML stuff in SQL 2005.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

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


Jul 23 '05 #3
Hi

If this is for a client to display the information, then the best place to
do this would be on the client itself.

John

"insomniux" <mi*************@hccnet.nl> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,
I'n in an environment where I cannot make stored procedures. Now I need
to make a query with a subquery in the SELECT part which gives a comma
separated list of results:

SELECT
p.id,
listFunction(SELECT name FROM names WHERE name_parent=p.id) AS
'nameList'
FROM projects AS p

This query should return something like:
1, "john,mike,petra"
2, "bob,carl,sandra,peter,
etc

listFunction is (of course) not (yet) defined. Is this possible without
the use of stored procedures?
Mike

Jul 23 '05 #4
I have access to a webinterface which I can feed with SQL, but some
statements are blocked.
I would prefer to create the statement in pure SQL but I think this is
not possible. I know how to get the first and the last value (using top
1 and sorting), but most subqueries return more than 3 rows. I know it
is possible to put all these values in separate columns, but the SQL
statements will become somewhat terrible.

Jul 23 '05 #5
insomniux wrote:
Hi,
I'n in an environment where I cannot make stored procedures. Now I need
to make a query with a subquery in the SELECT part which gives a comma
separated list of results:

SELECT
p.id,
listFunction(SELECT name FROM names WHERE name_parent=p.id) AS
'nameList'
FROM projects AS p

This query should return something like:
1, "john,mike,petra"
2, "bob,carl,sandra,peter,
etc

listFunction is (of course) not (yet) defined. Is this possible without
the use of stored procedures?
Mike


This *should* be very easy to do in your client, looking up the appropriate
functions may be much faster than trying to force it into SQL. The precise
steps you want to be able to execute are (in pseudocode):

// step 1, execute the query in whatever syntax your
// client language uses
//
$some_query = query_command("select ....")

// step 2, pluck out the values of one column to an array,
// your client should have some function for this
//
$some_array = extract_column($some_query,"ColumnA");

// step 3, in PHP the function is "implode", you want a function
// that collapses an array into a delimited string
//
$some_list = implode(",",$some_array);

Best of luck, HTH.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 23 '05 #6
insomniux (mi*************@hccnet.nl) writes:
I know there are various aggregate functions for numeric columns (like
min, max, ...). Wouldn't it be possible to write an aggregate function
for string-type columns which would enable to write the query as:


No, you cannot write your own aggregate functions in SQL 2000.

In SQL 2005 you can - using a CLR language. However, as I understand it,
it does not work very well for this case, at least not if you want the
lists to be ordered.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7
Kenneth Downs wrote in message
insomniux wrote:
Hi,
I'n in an environment where I cannot make stored procedures. Now I need
to make a query with a subquery in the SELECT part which gives a comma
separated list of results:

SELECT
p.id,
listFunction(SELECT name FROM names WHERE name_parent=p.id) AS
'nameList'
FROM projects AS p

This query should return something like:
1, "john,mike,petra"
2, "bob,carl,sandra,peter,
etc

listFunction is (of course) not (yet) defined. Is this possible without
the use of stored procedures?
Mike


This *should* be very easy to do in your client, looking up the appropriate
functions may be much faster than trying to force it into SQL. The precise
steps you want to be able to execute are (in pseudocode):

// step 1, execute the query in whatever syntax your
// client language uses
//
$some_query = query_command("select ....")

// step 2, pluck out the values of one column to an array,
// your client should have some function for this
//
$some_array = extract_column($some_query,"ColumnA");

// step 3, in PHP the function is "implode", you want a function
// that collapses an array into a delimited string
//
$some_list = implode(",",$some_array);

Best of luck, HTH.


Unfortunately my client application does not allow me to use this kind
of syntax. Only SQL.
Thanks
Jul 23 '05 #8
-P-
"insomniux" <mi*************@hccnet.nl> wrote in message news:11**********************@g14g2000cwa.googlegr oups.com...
I know there are various aggregate functions for numeric columns (like
min, max, ...). Wouldn't it be possible to write an aggregate function
for string-type columns which would enable to write the query as:

SELECT
p.id,
(SELECT listFunction(name) FROM names WHERE name_parent=p.id GROUP
BY name_parent) AS
'nameList'
FROM projects AS p

Yes, it would. The engineers at iAnywhere (SQL Anywhere) figured this one out nearly a decade ago...

SELECT list( [distinct] expression )
FROM tablename

is their aggregate on strings. You can even order the list (in addition to ordering the entire result set), and specify
custom delimiter characters (if you don't like commas).

www.ianywhere.com

-Paul Horan-
Sr. Architect
VCI Springfield, Mass
Jul 23 '05 #9

"insomniux" <mi*************@hccnet.nl> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,
I'n in an environment where I cannot make stored procedures. Now I need
to make a query with a subquery in the SELECT part which gives a comma
separated list of results:

SELECT
p.id,
listFunction(SELECT name FROM names WHERE name_parent=p.id) AS
'nameList'
FROM projects AS p

This query should return something like:
1, "john,mike,petra"
2, "bob,carl,sandra,peter,
etc

listFunction is (of course) not (yet) defined. Is this possible without
the use of stored procedures?
Mike


This is a function I use to do what you describe. I found the function
someplace on the Internet (don't remember where). Maybe you can modify it
for your use.

DECLARE @NbrList VarChar(300)
Declare @InvoiceID Varchar(30)

SELECT @NbrList = COALESCE(@NbrList + ', ', '') +
CAST(C.TRACKING_NO AS varchar(30))

FROM dbo.SHIPPER S INNER JOIN
dbo.SHIPPER_LINK SL ON S.PACKLIST_ID = SL.PACKLIST_ID
INNER JOIN
dbo.CARTON_LINE CL ON SL.CARTON_ID = CL.CARTON_ID AND
SL.CARTON_LINE_NO = CL.LINE_NO INNER JOIN
dbo.CARTON C ON CL.CARTON_ID = C.ID
WHERE S.INVOICE_ID = @InvoiceID
SELECT @NbrList
GO

Kevin
Jul 23 '05 #10
Mike (mi*************@hccnet.nl) writes:
Unfortunately my client application does not allow me to use this kind
of syntax. Only SQL.


And your client code is written in? Or why do suggest that you can do
this in client code?

You can do it in SQL, but it is just that it kludgy. And you will have
to accept a upper limit on how long the output may be.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #11
Kevin Haugen (kh*****@pacbell.net) writes:
This is a function I use to do what you describe. I found the function
someplace on the Internet (don't remember where). Maybe you can modify it
for your use.

DECLARE @NbrList VarChar(300)
Declare @InvoiceID Varchar(30)

SELECT @NbrList = COALESCE(@NbrList + ', ', '') +
CAST(C.TRACKING_NO AS varchar(30))


1) Mike wants his list for several rows in this table, so he would
still have to iterate over each id he wants to aggregate for.

2) The above relies on undocumented and undefined behaviour, why I
recommend against using it. The only safe way is build the list
iteratively. For more information look at
http://support.microsoft.com/default.aspx?scid=287515.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #12
> > Hi,
I'n in an environment where I cannot make stored procedures. Now I need to make a query with a subquery in the SELECT part which gives a comma separated list of results:

SELECT
p.id,
listFunction(SELECT name FROM names WHERE name_parent=p.id) AS
'nameList'
FROM projects AS p

This query should return something like:
1, "john,mike,petra"
2, "bob,carl,sandra,peter,
etc

listFunction is (of course) not (yet) defined. Is this possible without the use of stored procedures?
Mike

This is a function I use to do what you describe. I found the

function someplace on the Internet (don't remember where). Maybe you can modify it for your use.

DECLARE @NbrList VarChar(300)
Declare @InvoiceID Varchar(30)

SELECT @NbrList = COALESCE(@NbrList + ', ', '') +
CAST(C.TRACKING_NO AS varchar(30))

FROM dbo.SHIPPER S INNER JOIN
dbo.SHIPPER_LINK SL ON S.PACKLIST_ID = SL.PACKLIST_ID
INNER JOIN
dbo.CARTON_LINE CL ON SL.CARTON_ID = CL.CARTON_ID AND
SL.CARTON_LINE_NO = CL.LINE_NO INNER JOIN
dbo.CARTON C ON CL.CARTON_ID = C.ID
WHERE S.INVOICE_ID = @InvoiceID
SELECT @NbrList
GO

Looks interesting (although behaviour may be unexpected), it may be
useful for me.
I'll let you know
Mike

Jul 23 '05 #13

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

Similar topics

2
by: Lin Ma | last post by:
Greetings, In my search application, user can type a number to search. I use LIKE in my query. If a query result generates over 10,000 recordsets, it may several minutes to run. Is there a...
11
by: Surajit Laha | last post by:
I am firing a query like: SELECT TaskName, StartDate FROMTasks WHERE StartDate >= '01-Aug-2003' Now the result comes as: TaskName StartDate -------------------------- Task1 ...
3
by: Jack | last post by:
Hi, I have a form when loaded, retrieves record from an access table. Among other fields there is a check box called FinalUpdate. This is tied to a field in Access of type Yes/No. The form...
2
by: Wei Wang | last post by:
Hi, I want to do a select in dynamic command, something like: TRIGGER FUNCTION DECLARE table_name_suffix text; temp_result RECORD; temp_result2 RECORD;
2
by: Martin Sarsale | last post by:
Dear All: Im looking for solutions (Free Software is better) to do query result caching. Thanks to the people from #postgresql I know that postgres doesn't do that by himself and the solution...
1
by: RookieDan | last post by:
Greetings fellow Accessers! Im new but in Access, but I have some background in different coding. I have a programme loading customer data into Access belonging to BMW dealers in Europe. ...
1
by: Maarten van der Cammen | last post by:
Hi, In a table I have two numeric fields (e.g. FieldA and FieldB). Both fields have double precicion (field size "double"), Format "Fixed" and Decimal places "3" since I want to calculate with...
3
by: arial | last post by:
Hi, I am having trouble checking a sql query result = null in c#. my query is like, sqlcommand cmd.commandText = "select cqinum from table1"; how can cmpare this result to null?
1
by: CCHDGeek | last post by:
How can I tell if a query result empty (ie there are no records with the specified criteria). I want to change a form's design based on the result of the query it is based how. Does anyone know how...
1
ddtpmyra
by: ddtpmyra | last post by:
how can I capture the query result in PHP? I have two queries below: # Fetch the file information $query ="update filestorage set approved ='Y' where FileID = {$id}"; $query1 ="select...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.