简体   繁体   English

我在 oracle live sql 中的查询有什么问题?

[英]what is wrong with my query in oracle live sql?

I ran the following code in oracle live SQL but it gives an error.我在 oracle live SQL 中运行了以下代码,但它给出了一个错误。 What is the problem?问题是什么?

select * from bricks full join 
((select count(*) from bricks) as "counts"
inner join (select count(*) from bricks group by colour) as "colours" 
on counts.colour=colours.colour)
on bricks.colour=bricks.colour; --a dummy condition just for connecting tables without a specific column

output: output:

ORA-00907: missing right parenthesis

The problems are:问题是:

  • AS before a table/sub-query alias is invalid syntax in Oracle. Remove the AS keywords. AS before a table/sub-query alias is invalid syntax in Oracle. 删除AS关键字。
  • The aliases you are using are quoted identifiers which are case-sensitive and you do not use quoted identifiers with the same case when you refer to the aliases.您使用的别名是带引号的标识符,区分大小写,并且在引用别名时不使用具有相同大小写的带引号标识符。 Remove the double quotes.删除双引号。
  • Neither the counts nor colours sub-query has a colour column in the SELECT clause and you cannot subsequently refer to non-existent columns in the ON clause of the join condition. countscolours子查询在SELECT子句中都没有colour列,您随后不能在连接条件的ON子句中引用不存在的列。

You can fix it using:您可以使用以下方法修复它:

select *
from   bricks
       CROSS JOIN (select count(*) AS cnt from bricks) counts
       LEFT OUTER JOIN (
         select colour, count(*) AS colour_cnt
         from   bricks
         group by colour
       ) colours
       on bricks.colour=colours.colour

Which, for the sample data:其中,对于示例数据:

CREATE TABLE bricks (id, colour) AS
SELECT 1, 'red' FROM DUAL UNION ALL
SELECT 2, 'red' FROM DUAL UNION ALL
SELECT 3, 'red' FROM DUAL UNION ALL
SELECT 4, 'green' FROM DUAL UNION ALL
SELECT 5, 'green' FROM DUAL UNION ALL
SELECT 6, 'blue' FROM DUAL;

Outputs:输出:

ID ID COLOUR颜色 CNT碳纳米管 COLOUR颜色 COLOUR_CNT颜色_CNT
1 1个 red红色的 6 6个 red红色的 3 3个
2 2个 red红色的 6 6个 red红色的 3 3个
3 3个 red红色的 6 6个 red红色的 3 3个
4 4个 green绿色 6 6个 green绿色 2 2个
5 5个 green绿色 6 6个 green绿色 2 2个
6 6个 blue蓝色的 6 6个 blue蓝色的 1 1个

However, you probably want to simplify things and use the analytic COUNT() OVER (...) function and eliminate the self-joins:但是,您可能想简化事情并使用分析COUNT() OVER (...) function 并消除自连接:

select b.*,
       COUNT(*) OVER () AS cnt,
       COUNT(*) OVER (PARTITION BY colour) AS colour_cnt
from   bricks b;

Which outputs:哪些输出:

ID ID COLOUR颜色 CNT碳纳米管 COLOUR_CNT颜色_CNT
6 6个 blue蓝色的 6 6个 1 1个
5 5个 green绿色 6 6个 2 2个
4 4个 green绿色 6 6个 2 2个
2 2个 red红色的 6 6个 3 3个
1 1个 red红色的 6 6个 3 3个
3 3个 red红色的 6 6个 3 3个

(identical other than not duplicating the colours column) (除了不复制颜色列外其他相同)

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM