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 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
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
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
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.
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)
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
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
"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
"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
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
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
> > 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 ...
|
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...
|
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;
|
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...
|
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.
...
|
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...
|
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?
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |