[英]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
关键字。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. counts
和colours
子查询在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) (除了不复制颜色列外其他相同)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.