Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

rows to columns

RaunaqFeb 12 2015 — edited Feb 13 2015

hi ,

i am on 11g

I have a query which returns data inthe following format ,

YEAR COUNT

2015 445

2015 115

2015 577

2015 11

2015 28

2015 30

2015 270

2015 87

2015 62

2015 68

2015 15

2015 10

2015 5

2015 12

2015 34

2015 118

2015 19

2014 1541

2014 25

2014 581

2014 22

2014 18

2014 59

2014 7828

2014 7

2014 4

Now i want all the data to be displayed in one row for each year

Example:

col1   col2 col3 col4......

2015 445 115 577......

2014 25 581...

2013....

.

.

.

.

and so on ....

i am getting the above data by running the following query:

{code}

select to_char(create_dt,'YYYY') "YEAR",

COUNT(*) Count

FROM SCH1.Tab1 , table2

Where tab1.c1=table2.c1

group by

to_char(create_dt,'YYYY') ,

tab1.col1

Comments

Cookiemonster76

Seeing as your create table doesn't work, that's hardly surprising.

You can't have a column called number.

Your code refers to a variable called number that doesn't exist - and couldn't exist if you actually declared it due to the name. You probably want to use p_od instead.

Fix your naming and try again. If you're still having problems post the revised code, along with the details of any errors you get and we'll help you out.

Saubhik
Answer

This can be done in single SQL like

INSERT INTO numbers

select lvl from

(select level lvl from dual

   connect by level <=100

)

where mod(lvl,2)=0 ;

By the way DO NOT name a column as NUMBER, this is bad practice as that is a Oracle key word.

Assuming this is homework and you need to do like what teacher suggested then you can do something like

CREATE OR REPLACE PROCEDURE insert_even IS

BEGIN

for i in 1..100 loop

   IF mod(i,2)=0

   then

    INSERT INTO numbers values(i);

   end if;

end loop;

end;

CREATE OR REPLACE PROCEDURE insert_even IS

p_od NUMBER:=1;

p_do NUMBER:=100;

BEGIN

  loop

   IF mod(p_od,2)=0

   then

    INSERT INTO numbers values(p_od);

   end if;

   p_od := p_po+1;

   exit when p_od>p_do;

end loop;

end;

NOTE: I haven't tested any of the above, So you need to write, debug and test.

Marked as Answer by xiobijaph · Sep 27 2020
Manik

Saubhik wrote:

This can be done in single SQL like

INSERT INTO numbers

select lvl from

(select level lvl from dual

connect by level <=100

)

where mod(lvl,2)=0 ;

Without subquery:

------------------------

SELECT LEVEL * 2 lvl

      FROM DUAL

CONNECT BY LEVEL <= 50;

Another way using sequence :

create sequence seq_even start with 2 increment by 2;

Cheers,

Manik.

xiobijaph

I changed names and still doesn't work
pastedImage_0.png

Cookiemonster76

Of course it doesn't.

IF mod(another,2) =0

That refers to a PL/SQL variable/parameter called 'another'.

There's no such variable declared.

There's a column of that name but you have no use for it at that point.

Likewise

INSERT INTO excercise values(another)

Trying to insert the column into itself doesn't really make sense does it?

Meanwhile you've got a variable called p_od which you are incrementing in the loop. Don't you think it would make more sense to use that instead?

BrunoVroman

Hello,

you have already seen previous answers, I won't repeat the same; but apart form using a simple statement (INSERT ... LEVEL ... -I would insert " 2 * LEVEL"-), two additional remarks:

-A- writing code like "p_od := 1; LOOP p_od := p_od + 1: IF mod( p_od, 2 ) = 0 THEN ... EXIT WHEN p_od = p_do ..." looks really sad...

To give simply one example: you might jump the odd numbers: FOR i IN 1..50 LOOP INSERT ... 2 * i ... END LOOP;

-B- and please take the 2 minutes necessary to modify your "display name" to something more human-friendly, currently you look like a "bot"... Please have a look at

Best regards,

Bruno Vroman.

P.S. better late than never: Season's greetings, Oracle-mathematical way

xiobijaph

Saubhik napisał(-a):

This can be done in single SQL like

INSERT INTO numbers

select lvl from

(select level lvl from dual

connect by level <=100

)

where mod(lvl,2)=0 ;

Yeah, its work, but I need to use procedure

I used your second option

pastedImage_1.png
No errors, but still 'no data found'

And third example:pastedImage_2.png

Saubhik

You need to RUN the procedure you have created. Creating procedure is different from EXECUTING the procedure.

BEGIN

your_procedure_name;

END;

Gaz in Oz

Using a while loop, here's an example anonymous PL/SQL block demonstrating, well, a while loop:

declare

  p_od NUMBER := 2;

  p_do NUMBER := 100;

begin

  while true loop

      -- INSERT INTO numbers (p_od) VALUES (p_od);

      dbms_output.put_line(p_od);

      p_od := p_od + 2;

      exit when p_od > p_do;

  end loop;

end;

/

...your variable naming is not good. Naming variables the same as the columns in a table you are trying to manipulate will result in maintenance issues because the code looks ambiguous.

Manik

Gaz in Oz wrote:

Using a while loop, here's an example anonymous PL/SQL block demonstrating, well, a while loop:

  1. declare
  2. p_odNUMBER:=2;
  3. p_doNUMBER:=100;
  4. begin
  5. whiletrueloop
  6. --INSERTINTOnumbers(p_od)VALUES(p_od);
  7. dbms_output.put_line(p_od);
  8. p_od:=p_od+2;
  9. exitwhenp_od>p_do;
  10. endloop;
  11. end;
  12. /

...your variable naming is not good. Naming variables the same as the columns in a table you are trying to manipulate will result in maintenance issues because the code looks ambiguous.

Even if you remove 'while true'  it would work

Cheers,

Manik.

Cookiemonster76

If you're going to demo a while loop, wouldn't it be an idea to have one that actually acts like a while loop? (where the WHILE check actually evaluates to false at some point and exits that way)

e.g.

declare

  p_od NUMBER := 2;

  p_do NUMBER := 100;

begin

  while p_od <= p_do loop

      -- INSERT INTO numbers (p_od) VALUES (p_od);

      dbms_output.put_line(p_od);

      p_od := p_od + 2;

  end loop;

end;

/

xiobijaph

Ok, now I have results. Thank you

L. Fernigrini

Please then take 15 seconds to mark correct and useful answers, and remember to set your user name to something more meaningful than "9910aa55-1382-4bbb-8a9b-338b89308abe"

xiobijaph

I changed user name 1 minute after register but still doesnt work:
pastedImage_0.png

Cookiemonster76

Most of us can't read Polish, and we can't copy and paste the text from that image into google translate.

xiobijaph

Nothing important:

"Thanks for joining to community Oracle, you will find here answers on your questions. You can contact with others people...

Display name: xiobijaph"

Nevermind, maybe after some time my name will update

1 - 16
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 13 2015
Added on Feb 12 2015
6 comments
239 views