Using Oracle 11
I’m wanting to pull data from a table, and add a column of calculated values which come from a subquery statement.
Example Table1 is:
COLUMN1: COLUMN2:
1 Group1
3 Group2
5 Group3
6 Group4
The subquery returns different values each time the query is run, but for the sake of this example let’s say it returns thes values:
2
4
7
8
What I need is to combine the results
COLUMN1: COLUMN2: COLUMN3: (subquery - order does NOT matter)
1 Group1 2
3 Group2 4
5 Group3 7
6 Group4 8
But because the subquery is being calculated with each row return from the table, all I am getting is.
COLUMN1: COLUMN2: COLUMN3:
1 Group1 2
3 Group2 2
5 Group3 2
6 Group4 2
There is no way that I can think of to relate the table and the subquery, I’ve tried playing with ROWNUM but with no luck.
UPDATE
SELECT GD.*, N
FROM patrongroupsdesc GD,
(SELECT n
FROM patrongroupsdesc GD2,
(SELECT n, ROWNUM
FROM ( SELECT ROWNUM n
FROM DUAL
CONNECT BY LEVEL <= 100)
WHERE n >= 1) SUB
WHERE SUB.n = GD2.groupid(+) AND GD2.groupid IS NULL) SUB2
table PATRONGROUPSDESC is formatted as follows
groupid (NUMBER)
description (VARCHAR2)
...other data
SUB returns the values 1-100
SUB2 returns the gaps in groupid numbers
I’m essentially duplicating all data in this table except for one column
First you must know that if you don’t have an
ORDER BYclause then the order of the results comes with no guarantee at all.Then, you can do:
The trick here is that
ROWNUMis assigned as a result comes out from aSELECTstatement. Therefore if you want to paste tables like this, you must SELECT from your tables and store rownum in another column, and then from those two subselects you can join.