Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

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,pete
r,
etc

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


Report this thread to moderator Post Follow-up to this message
Old Post
insomniux
04-24-05 06:23 PM


Re: query result as comma-separated list
insomniux (mike.bosschaert@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,pete
r,
> 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, esquel@sommarskog.se

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

Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
04-24-05 06:23 PM


Re: query result as comma-separated list
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 (mike.bosschaert@hccnet.nl) writes: 
need 
 comma
 
 without[color=darkre
d] 
>
> 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, esquel@sommarskog.se
>
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp


Report this thread to moderator Post Follow-up to this message
Old Post
insomniux
04-24-05 06:23 PM


Re: query result as comma-separated list
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" <mike.bosschaert@hccnet.nl> wrote in message
news:1114361846.034822.103940@f14g2000cwb.googlegroups.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,pete
r,
> etc
>
> listFunction is (of course) not (yet) defined. Is this possible without
> the use of stored procedures?
> Mike
>



Report this thread to moderator Post Follow-up to this message
Old Post
John Bell
04-25-05 01:23 AM


Re: query result as comma-separated list
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.


Report this thread to moderator Post Follow-up to this message
Old Post
insomniux
04-25-05 01:23 AM


Re: query result as comma-separated list
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,pete
r,
> 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)

Report this thread to moderator Post Follow-up to this message
Old Post
Kenneth Downs
04-25-05 01:23 AM


Re: query result as comma-separated list
insomniux (mike.bosschaert@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, esquel@sommarskog.se

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

Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
04-25-05 01:23 AM


Re: query result as comma-separated list
Kenneth Downs wrote in  message
> insomniux wrote:
> 
>
> This *should* be very easy to do in your client, looking up the appropriat
e
> functions may be much faster than trying to force it into SQL.  The precis
e
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Mike
04-25-05 12:23 PM


Re: query result as comma-separated list
"insomniux" <mike.bosschaert@hccnet.nl> wrote in message news:1114367673.194349.232680@g14g
2000cwa.googlegroups.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 o
ut 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



Report this thread to moderator Post Follow-up to this message
Old Post
-P-
04-25-05 12:23 PM


Re: query result as comma-separated list
"insomniux" <mike.bosschaert@hccnet.nl> wrote in message
news:1114361846.034822.103940@f14g2000cwb.googlegroups.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,pete
r,
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Kevin Haugen
04-25-05 04:23 PM


Sponsored Links





Last Thread Next Thread
Pages (2): [1] 2 »
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 05:59 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006