?
Solved

select rows into a list using DB2 SQL

Posted on 2009-04-09
12
Medium Priority
?
1,059 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:ewang1205
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 18

Expert Comment

by:Dave Ford
ID: 24108343

Are you simply looking to put the results in ascending order?

How about this?

select m.emp_no
from   MyTable m
order by m.emp_no

Open in new window

0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24108955
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
0
 

Author Comment

by:ewang1205
ID: 24133550
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.
0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 1600 total points
ID: 24135161
0
 

Author Comment

by:ewang1205
ID: 24140261
I looked at http://www.experts-exchange.com/Database/DB2/Q_24110747.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
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24140335
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
0
 

Author Comment

by:ewang1205
ID: 24140389
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
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24140479
Hi ewang,

I'm confused.  That's the table that I used in the example.

So what's the question?


Kent
0
 

Author Comment

by:ewang1205
ID: 24140507
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;
0
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 400 total points
ID: 24140558

The recursive sql generates all the string combinations of the selected elements.  You have to select the desired string.

You can also do this in the stored procedure.  If you're more comfortable writing procedures, select the desired rows and build the string in the procedure.


Kent
0
 

Author Comment

by:ewang1205
ID: 24143040
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
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 1600 total points
ID: 24145100
after you fetch the last row, the sqlcode will be 100, so you can try

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 sqlcode = 100 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
0

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question