472,378 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 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 28945
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.