Reputation: 3566
I want to select rows from two different tables(TABLE_A and TABLE_B). I'm using oracle db and hibernate. Here are my tables:
Here is TABLE_A
+----+--------+---------+-----------+---------------------+---------------------+
| ID | AMOUNT | BALANCE | STATUS | CREATE_DATE | END_DATE |
+----+--------+---------+-----------+---------------------+---------------------+
| 1 | 5 | 5 | FINISHED | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
| 2 | 12 | 17 | TIMED_OUT | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
| 3 | 21 | 38 | COMMITED | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
+----+--------+---------+-----------+---------------------+---------------------+
Here is TABLE_B
+----+----------------+---------------+----------------+---------------------+
| ID | PAYMENT_AMOUNT | TOTAL_BALANCE | PAYMENT_STATUS | REQUEST_TIMESTAMP |
+----+----------------+---------------+----------------+---------------------+
| 1 | 3 | 23 | FAILED | 2014-02-27 15:10:20 |
| 2 | 12 | 11 | FULFILLED | 2014-02-27 15:10:20 |
| 3 | 2 | 9 | TIMED_OUT | 2014-02-27 15:10:20 |
+----+----------------+---------------+----------------+---------------------+
What I want to do is that I need to get a result like the following with a single query:
+--------+---------+-----------+---------------------+---------------------+
| AMOUNT | BALANCE | STATUS | DATE | END_DATE |
+--------+---------+-----------+---------------------+---------------------+
| 5 | 5 | FINISHED | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
| 12 | 17 | TIMED_OUT | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
| 21 | 38 | COMMITED | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
| 3 | 23 | FAILED | 2014-02-27 15:10:20 | null |
| 12 | 11 | FULFILLED | 2014-02-27 15:10:20 | null |
| 2 | 9 | TIMED_OUT | 2014-02-27 15:10:20 | null |
+--------+---------+-----------+---------------------+---------------------+
The results are ordered by DATE
. Column descriptions are the following:
AMOUNT = TABLE_A.AMOUNT or TABLE_B.PAYMENT_AMOUNT
BALANCE = TABLE_A.BALANCe or TABLE_B.TOTAL_BALANCE
STATUS = TABLE_A.STATUS or TABLE_B.PAYMENT_STATUS
DATE = TABLE_A.CREATE_DATE or TABLE_B.REQUEST_TIMESTAMP
END_DATE = TABLE_A.END_DATE or null
Is there any way to get those kind of results in oracle db and any way to bind those results to a hibernate object in java?
Upvotes: 1
Views: 1318
Reputation: 136042
try something like this
select * from (
select id as amount, ... from t1
union all
select id as amount, ... from t2
) t order by date
Upvotes: 2