If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > SQL to display row multiple times with count

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-18-07, 23:41
xamar xamar is offline
Registered User
 
Join Date: Apr 2004
Posts: 39
SQL to display row multiple times with count

For Db2 V7 in z/OS

For table that has
col-1 col-2
aaaa 2

The SQL should produce

aaaa 1
aaaa 2

Thanks for any help!
Reply With Quote
  #2 (permalink)  
Old 02-18-07, 23:44
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
inner join with the same table with A.col2 <= B.col2
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #3 (permalink)  
Old 02-19-07, 00:17
xamar xamar is offline
Registered User
 
Join Date: Apr 2004
Posts: 39
Rahul,

I do not understand. For example the row
aaaa 1

doesn't even exist in table. How is inner join going to generate that? Please explain.

Thanks.
Reply With Quote
  #4 (permalink)  
Old 02-19-07, 00:54
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
sorry.. forgot to mention row_number() over() function

create table abc (name varchar(10), no smallint );
insert into abc values ('Rahul' ,5);
select A.name , B.no from abc A,
(select row_number() over() as no from syscat.columns ) as B
where B.no<=A.no;


NAME NO
---------- --------------------
Rahul 1
Rahul 2
Rahul 3
Rahul 4
Rahul 5
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #5 (permalink)  
Old 02-19-07, 01:05
xamar xamar is offline
Registered User
 
Join Date: Apr 2004
Posts: 39
Thanks Rahul!

But, I don't think the row_number and over() functions are available in Db2 V7 in z/OS which I am using.

But it is nice to see that in LUW it is possible now.
Reply With Quote
  #6 (permalink)  
Old 02-21-07, 04:03
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You know the maximum value for "col2"? If yes, then you could do something like this (which assumes MAX(col2) = 4):
Code:
SELECT col1, 1
FROM   ...
WHERE  col2 <= 1
UNION ALL
SELECT col1, 2
FROM   ...
WHERE  col2 <= 2
UNION ALL
SELECT col1, 3
FROM   ...
WHERE  col2 <= 3
UNION ALL
SELECT col1, 4
FROM   ...
WHERE  col2 <= 4
Alternatively, if you have recursive SQL on your platform, try this:
Code:
WITH max(v) AS
   ( SELECT MAX(col2) FROM ... ),
recurs(col, colMax, currentMax) AS
   ( SELECT col1, col2, 1
     FROM   ...
     UNION ALL
     SELECT col, colMax, currentMax + 1
     FROM   recurs, max
     WHERE  colMax < currentMax )
SELECT col, currentMax
FROM    recurs
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 02-21-07, 06:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if you don't already have one, start by creating an integers table
Code:
create table integers
  (i integer not null primary key);
insert into integers (i) values (0);
insert into integers (i) values (1);
insert into integers (i) values (2);
insert into integers (i) values (3);
insert into integers (i) values (4);
insert into integers (i) values (5);
insert into integers (i) values (6);
insert into integers (i) values (7);
insert into integers (i) values (8);
insert into integers (i) values (9);
now we can generate your results like this --
Code:
select yourtable.col1
     , integers i
  from yourtable
inner
  join integers
    on integers.i between 1 and yourtable.col2
order
    by yourtable.col1
     , integers i
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 02-23-07, 20:10
xamar xamar is offline
Registered User
 
Join Date: Apr 2004
Posts: 39
Thanks Stolze and R937 for those fantastic solutions!

Stolze, I do not have recursive SQL but I think your first solution will work.

R937, I tried out your solution and it works great!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On