| Adding a row based on a value [message #634060] |
Tue, 03 March 2015 13:11  |
 |
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 #634067 is a reply to message #634060] |
Tue, 03 March 2015 14:46   |
 |
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 #634076 is a reply to message #634069] |
Wed, 04 March 2015 00:44  |
 |
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!
...
|
|
|
|