Home » SQL & PL/SQL » SQL & PL/SQL » Adding a row based on a value
Adding a row based on a value [message #634060] Tue, 03 March 2015 13:11 Go to next message
system243trd
Messages: 5
Registered: January 2015
Junior Member
I have the following table
tablea
AnID Aname aTotal
57 Agatha 28
58 Bertie 13
59 Guilford 17
60 Francois 12

I want to be able to create a query that displays the id aname atotal and also another column called noofrows. I would like the query to list multiple records for each AnId based on the atotal / 14 e.g. if the aTotal equals 28 then there will be two rows for Anid 57 and the Noofrows column will display a1 and a2. If there result is less than 14 and greater or equal to 1, the total rows must be 1. If the total is 0 then the rows will be 0. See below the results of this example

AnID Aname Total Noofrows
57 Agatha 28 a1
57 Agatha 28 a2
58 Bertie 13 a1
59 Guilford 56 a1
59 Guilford 56 a2
59 Guilford 56 a3
59 Guilford 56 a4
60 Francois 80 a1
60 Francois 80 a2
60 Francois 80 a3
60 Francois 80 a4
60 Francois 80 a5
60 Francois 80 a6

Can anyone help?
Re: Adding a row based on a value [message #634061 is a reply to message #634060] Tue, 03 March 2015 13:17 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/mv/msg/195921/630921/#msg_630921

If you comply with Posting Guidelines, you'll get more & better responses.
Re: Adding a row based on a value [message #634063 is a reply to message #634060] Tue, 03 March 2015 14:08 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

You have to use a row generator.
But it is not clear why you have only 1 row for id 58 and 6 rows for id 60 and where does 80 come from, as well as 56 for id 59!

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

[Updated on: Wed, 04 March 2015 00:44]

Report message to a moderator

Re: Adding a row based on a value [message #634065 is a reply to message #634063] Tue, 03 March 2015 14:28 Go to previous messageGo to next message
system243trd
Messages: 5
Registered: January 2015
Junior Member
Sorry I copied the format from Word and it went wrong!!
Re: Adding a row based on a value [message #634066 is a reply to message #634065] Tue, 03 March 2015 14:30 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

This why we have written the links we provided you.
So please read them and comply.

And answer my questions and post what I requested.

Re: Adding a row based on a value [message #634067 is a reply to message #634060] Tue, 03 March 2015 14:46 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
-- table and data (like what it would help if you would provide in any future posts):
SCOTT@orcl12c> CREATE TABLE tablea
  2    (anid	NUMBER,
  3  	aname	VARCHAR2(15),
  4  	atotal	NUMBER)
  5  /

Table created.

SCOTT@orcl12c> INSERT ALL
  2  INTO tablea VALUES (57, 'Agatha',	 28)
  3  INTO tablea VALUES (58, 'Bertie',	 13)
  4  INTO tablea VALUES (59, 'Guilford', 56)
  5  INTO tablea VALUES (60, 'Francois', 80)
  6  SELECT * FROM DUAL
  7  /

4 rows created.

SCOTT@orcl12c> SELECT * FROM tablea ORDER BY anid
  2  /

      ANID ANAME               ATOTAL
---------- --------------- ----------
        57 Agatha                  28
        58 Bertie                  13
        59 Guilford                56
        60 Francois                80

4 rows selected.


-- query:
SCOTT@orcl12c> SELECT anid "AnID", aname "Aname", atotal "Total",
  2  	    'a' || COLUMN_VALUE "Noofrows"
  3  FROM   tablea,
  4  	    TABLE
  5  	      (CAST
  6  		 (MULTISET
  7  		    (SELECT LEVEL
  8  		     FROM   DUAL
  9  		     CONNECT BY LEVEL <= CEIL (atotal / 14))
 10  		  AS SYS.ODCINUMBERLIST))
 11  ORDER  BY anid, "Noofrows"
 12  /

      AnID Aname                Total Noofrows
---------- --------------- ---------- -----------------------------------------
        57 Agatha                  28 a1
        57 Agatha                  28 a2
        58 Bertie                  13 a1
        59 Guilford                56 a1
        59 Guilford                56 a2
        59 Guilford                56 a3
        59 Guilford                56 a4
        60 Francois                80 a1
        60 Francois                80 a2
        60 Francois                80 a3
        60 Francois                80 a4
        60 Francois                80 a5
        60 Francois                80 a6

13 rows selected.

Re: Adding a row based on a value [message #634068 is a reply to message #634067] Tue, 03 March 2015 14:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Your table is not the one OP posted:
Quote:
AnID Aname aTotal
57 Agatha 28
58 Bertie 13
59 Guilford 17
60 Francois 12


Razz

Re: Adding a row based on a value [message #634069 is a reply to message #634068] Tue, 03 March 2015 15:28 Go to previous messageGo to next message
system243trd
Messages: 5
Registered: January 2015
Junior Member
That will do nicely thanks Barbara.

Michel apologies again about the format, I will ensure it is more accurate next time I post. I'm not sure what questions you asked though.

Thanks all.
Re: Adding a row based on a value [message #634076 is a reply to message #634069] Wed, 04 March 2015 00:44 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

My questions were:

Michel Cadot wrote on Tue, 03 March 2015 21:08

...
But it is not clear why you have only 1 row for id 58 and 6 rows for id 60 and where does 80 come from, as well as 56 for id 59!
...


Previous Topic: Fetching table sql inside code
Next Topic: Regexp_Substr
Goto Forum:
  


Current Time: Fri Aug 28 18:13:48 CDT 2026