如何在Oracle SQL中按日期将三个联合分组在一起?

nwlqm0z1  于 2023-08-03  发布在  Oracle
关注(0)|答案(1)|浏览(81)

Here's an image if it helps
我试图将日本相扑结果的三个表连接在一起,并按2019年的降序日期对其进行排序。我设法加入他们,但不知道如何按日期(BASHO)订购。我在Oracle Apex工作。
到目前为止我试过了

Select a.* from sumo_2019 a
where a.Rikishi1_ID=12191
order by a.BASHO
Union
Select b.* from sumo_2018 b
where b.Rikishi1_ID=12191
order by b.BASHO
Union
Select c.* from sumo_2017 c
where c.RIKISHI1_ID=12191
Order by c.BASHO

字符串
和/或

Select a.* from sumo_2019 a
where a.Rikishi1_ID=12191
Union
Select b.* from sumo_2018 b
where b.Rikishi1_ID=12191
Union
Select c.* from sumo_2017 c
where c.RIKISHI1_ID=12191
Order by BASHO


但两次都收到错误(错误是“第3/17行的错误:ORA-00933:SQL命令第一次尝试未正确结束,第9/10行出现错误:ORA-00904:“BASHO”:第二次尝试的无效标识符

hpxqektj

hpxqektj1#

您可以在子查询中使用UNION,在外部查询中使用ORDER

SELECT *
FROM   (
  Select *
  from   sumo_2019
  where  Rikishi1_ID=12191
Union
  Select *
  from   sumo_2018 b
  where  Rikishi1_ID=12191
Union
  Select *
  from   sumo_2017 c
  where  RIKISHI1_ID=12191
)
Order by BASHO;

字符串
其中,对于样本数据:

CREATE TABLE sumo_2019 (BASHO, rikishi1_id) AS
  SELECT DATE '2019-01-01', 12191 FROM DUAL;

CREATE TABLE sumo_2018 (BASHO, rikishi1_id) AS
  SELECT DATE '2018-01-01', 12191 FROM DUAL;

CREATE TABLE sumo_2017 (BASHO, rikishi1_id) AS
  SELECT DATE '2017-01-01', 12191 FROM DUAL;


输出:
| RIKISHI1_ID| RIKISHI1_ID |
| --| ------------ |
| 12191| 12191 |
| 12191| 12191 |
| 12191| 12191 |
fiddle

相关问题