我有两个表(A)和(B),在不同的日期几乎具有相同的信息。
表(A)
+----+------------+----------+-------+
|ID | Date | Status | Amount|
+----+------------+----------+-------+
| A1 | 01/14/2011 | Received |15 |
| A2 | 01/18/2011 | Received |65 |
| T3 | 01/9/2011 | Received |85 |
| X7 | 02/16/2011 | Received |35 |
| A5 | 01/3/2011 | Received |10 |
+----+------------+----------+-------+
表(B)
+----+------------+----------+-------+
|ID | Date | Status | Amount|
+----+------------+----------+-------+
| A1 | 03/31/2012 | Approved |15 |
| A2 | 03/31/2012 | Denied |65 |
| A3 | 03/31/2012 | Approved |85 |
| A7 | 03/31/2012 | Received |35 |
| A5 | 03/31/2012 | pending |10 |
+----+------------+----------+-------+
我正在尝试将它们合并,以获得新的表(结果),如下所示:
表格(结果)
+----+------------+----------+-------+
|ID | Date | Status | Amount|
+----+------------+----------+-------+
| A1 | 01/14/2011 | Received |15 |
| A1 | 03/31/2012 | Approved |15 |
| A2 | 01/18/2011 | Received |65 |
| A2 | 03/31/2012 | Denied |65 |
| A5 | 01/3/2011 | Received |10 |
| A5 | 03/31/2012 | pending |10 |
+----+------------+----------+-------+
我使用了以下代码:
SELECT *
FROM table1
JOIN table2
ON table1.id = table2.id
ORDER BY table1.id;
这是结果:
+----+------------+----------+-------+------------+------------+---------+
| ID | Date | Status | Amount| Date_1 | Status_1 | Amount_1|
+----+------------+----------+-------+------------+------------+---------+
| A1 | 01/14/2011 | Received |15 |03/31/2012 | Approved |15 |
+----+------------+----------+-------+------------+------------+---------+
工作正常,但这不是我想要的。我不想并排重复的列。我当时正在考虑使用INSERT INTO或UNION ALL,但不确定。
答案 0 :(得分:1)
在UNION中,每个表仅包含其他表中具有相应行的行:
SELECT * FROM TableA a
WHERE EXISTS (
SELECT 1 FROM TableB
WHERE ID = a.ID AND Amount = a.Amount AND Status <> a.Status
)
UNION
SELECT * FROM TableB b
WHERE EXISTS (
SELECT 1 FROM TableA
WHERE ID = b.ID AND Amount = b.Amount AND Status <> b.Status
)
ORDER BY ID;
我不确定条件Amount = x.Amount
。也许您想删除它。
答案 1 :(得分:1)
这是一个简单的联合所有查询来完成工作:
select * from a where id in (select id from b)
union all
select * from b where id in (select id from a)
order by id, date;
答案 2 :(得分:0)
select x.id, x.status, x.amount, y.status, y.amount
from
(
select 'A1' id, 'received' status, 15 amount from dual
union all
select 'A2' id, 'received' status, 65 amount from dual
) x,
(
select 'A1' id, 'approved' status, 15 amount from dual
union all
select 'A2' id, 'denied' status, 65 amount from dual
) y
where x.id = y.id
希望有帮助!
答案 3 :(得分:0)
尝试一下:
<class 'numpy.ndarray'>
[[9.630769e-01 4.302222e-02 8.238154e-02]
[7.661538e-01 3.153453e-01 4.497161e-01]
[5.753846e-01 6.559556e-01 8.274414e-01]
[4.027350e-01 9.705709e-01 1.175706e+00]
...
]
答案 4 :(得分:0)
如果目的(您尚未真正描述的)是报告来自两个表中都存在ID的任一表中的行,那么您可能想要这样做:
select id, somedate, status, amount
from ( select id, somedate, status, amount from table_a
union all
select id, somedate, status, amount from table_b )
where id in
( select id from table_a
intersect
select id from table_b )
答案 5 :(得分:-1)
使用UNION:
SELECT ID, Date, Status, Amount
FROM table1
UNION
SELECT ID, Date, Status, Amount
from table2
ORDER BY ID;