Home > Enterprise >  More same row in SQL
More same row in SQL

Time:12-28

How do I call the row it belongs to again according to the data in the table in SQL? For example, if the AMOUNT is 4 in a row, I want to print that same row 4 times.

I can't anything. Can you help me?

CodePudding user response:

A basic example of how you can accomplish is as follows.

First create a tally table

Create table Numbers (n int not null);
insert into Numbers values(1),(2),(3),(4),(5);

Then join with your query equating, in this case, Amount with the required number of rows

/* Your table */
create table t(Id int, Amount int)
insert into t values (1, 2),(2, 4);

select t.* 
from t
join Numbers n on n.n <= t.Amount;

CodePudding user response:

Your question is not clear actually, but as I understand you can use that PL/SQL too, and @Stu's answer is so suitable.

declare
cursor cur is select amount from your_tab;
begin
  for j in cur
    loop
    for i in 1..j.amount loop
      dbms_output.put_line('Amount is ' || to_char(j.amount) || ' and it printed ' || to_char(j.amount) || ' time(s).');
    end loop;
  end loop;
end;
/
  •  Tags:  
  • sql
  • Related