ewang1205
asked on
select rows into a list using DB2 SQL
I have the following rows. I like a simple select to get the result like 10, 20, 30, 40, 90 ...
Thanks!
emp_no
10
20
30
40
90
Thanks!
emp_no
10
20
30
40
90
Hi Dave,
Betcha this is a columns to rows issue. :)
Hi Ewang,
Are you looking to put all of the items on a single line?
Kent
Betcha this is a columns to rows issue. :)
Hi Ewang,
Are you looking to put all of the items on a single line?
Kent
ASKER
Yes, I like to put all of the items (rows) on a single line. Basically, I like to put all the rows (single column) into one row seperated by comma. Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I looked at https://www.experts-exchange.com/questions/24110747/how-to-convert-multiple-rows-into-a-single-column-with-comma-separated-value.html . I see the following. But, I cannot finish the following sample code. Maybe the author was trying to write a function, but my DB2 skill is not as advanced. Please help.
--
-- Create a test table and sample data
--
CREATE TABLE t (
id integer not null generated by default as identity,
st varchar (2));
INSERT INTO t (st) VALUES ('CA');
INSERT INTO t (st) VALUES ('NJ');
INSERT INTO t (st) VALUES ('NY');
INSERT INTO t (st) VALUES ('PA');
--
-- Build the string
--
with tt (id, v, current, final)
as
(
SELECT id, id, st, cast (st as varchar (100))
from t t1
union all
select t1.id, t2.id, t1.st, cast (t2.final || ',' || t1.st as varchar (100))
from t t1, tt t2
where t2.id < t1.id
and locate (t1.st, t2.final) = 0
)
select * from tt;
Open in New Window Select All
--
-- Create a test table and sample data
--
CREATE TABLE t (
id integer not null generated by default as identity,
st varchar (2));
INSERT INTO t (st) VALUES ('CA');
INSERT INTO t (st) VALUES ('NJ');
INSERT INTO t (st) VALUES ('NY');
INSERT INTO t (st) VALUES ('PA');
--
-- Build the string
--
with tt (id, v, current, final)
as
(
SELECT id, id, st, cast (st as varchar (100))
from t t1
union all
select t1.id, t2.id, t1.st, cast (t2.final || ',' || t1.st as varchar (100))
from t t1, tt t2
where t2.id < t1.id
and locate (t1.st, t2.final) = 0
)
select * from tt;
Open in New Window Select All
Hi ewang,
The SQL start with the word 'with' actually builds the string. The CREATE TABLE and INSERT statements generate a test case to demonstrate the recursive SQL.
Post your table definition (at least the relevent parts) and your rules for selecting items to be on the same line. I'll be glad to put the SQL together for you.
Kent
The SQL start with the word 'with' actually builds the string. The CREATE TABLE and INSERT statements generate a test case to demonstrate the recursive SQL.
Post your table definition (at least the relevent parts) and your rules for selecting items to be on the same line. I'll be glad to put the SQL together for you.
Kent
ASKER
Here is the table:
CREATE TABLE t (
id integer not null generated by default as identity,
st varchar (2));
INSERT INTO t (st) VALUES ('CA');
INSERT INTO t (st) VALUES ('NJ');
INSERT INTO t (st) VALUES ('NY');
INSERT INTO t (st) VALUES ('PA');
.
How to have a SQL or stored procedure to have the following output? Can the recursive SQL be part of the stored procedure?
CA, NJ, NY, PA
CREATE TABLE t (
id integer not null generated by default as identity,
st varchar (2));
INSERT INTO t (st) VALUES ('CA');
INSERT INTO t (st) VALUES ('NJ');
INSERT INTO t (st) VALUES ('NY');
INSERT INTO t (st) VALUES ('PA');
.
How to have a SQL or stored procedure to have the following output? Can the recursive SQL be part of the stored procedure?
CA, NJ, NY, PA
Hi ewang,
I'm confused. That's the table that I used in the example.
So what's the question?
Kent
I'm confused. That's the table that I used in the example.
So what's the question?
Kent
ASKER
The problem is the following select returns more rows than I needed. Another problem is if can I put the recursive SELECT into a store procedure? Thanks.
select * from tt;
select * from tt;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here is the stored procedure I wrote using table t. Works fine. The only problem is to fin the last row and then LEAVE fetch_loop. How to find the last row ?
CREATE PROCEDURE LOOP_UNTIL_last_row(OUT tstring vARCHAR(1000) )
LANGUAGE SQL
BEGIN
DECLARE v_st VARCHAR(15);
--declare v_counter integer default 0;
declare v_tstring VARCHAR(1000) default '';
DECLARE c1 CURSOR FOR
SELECT st FROM t;
--DECLARE EXIT HANDLER FOR NOT FOUND
-- SET counter = -1;
OPEN c1;
fetch_loop:
LOOP
FETCH c1 INTO v_st;
IF v_st = 'NY' THEN
LEAVE fetch_loop;
END IF;
-- SET v_counter = v_counter + 1;
set v_tstring = v_tstring ||','||v_st;
END LOOP fetch_loop;
SET tstring= substr(v_tstring,2);
CLOSE c1;
END
CREATE PROCEDURE LOOP_UNTIL_last_row(OUT tstring vARCHAR(1000) )
LANGUAGE SQL
BEGIN
DECLARE v_st VARCHAR(15);
--declare v_counter integer default 0;
declare v_tstring VARCHAR(1000) default '';
DECLARE c1 CURSOR FOR
SELECT st FROM t;
--DECLARE EXIT HANDLER FOR NOT FOUND
-- SET counter = -1;
OPEN c1;
fetch_loop:
LOOP
FETCH c1 INTO v_st;
IF v_st = 'NY' THEN
LEAVE fetch_loop;
END IF;
-- SET v_counter = v_counter + 1;
set v_tstring = v_tstring ||','||v_st;
END LOOP fetch_loop;
SET tstring= substr(v_tstring,2);
CLOSE c1;
END
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you simply looking to put the results in ascending order?
How about this?
Open in new window