我在Oracle数据库中有以下数据:
Name Place Color
------- --------- --------
John Paris Blue
John Miami Blue
Ryan Boston Red
Ryan Boston Red
Jim Miami Blue
Ryan Oslo Red
Jason Rome Green
Jim Paris Blue
Jason Rome Green
Jim Paris Blue
Ryan Boston Red
如果两列相同(名称/地点),我需要进行重复数据删除,然后按第三列计数。我试图用嵌套选择分组两次,但我一直收到ORA-00933错误。
select Color, count(Color)
from
(
select TO_CHAR(Name)||'-'||(Place) as unique_ident from mytable
group by TO_CHAR(Name)||'-'||(Place)
) as inline
group by Color
我会回报类似的东西
Blue 4
Red 2
Green 1
感谢任何帮助。
答案 0 :(得分:2)
我认为你正在寻找这样的东西:
SELECT Color, COUNT(DISTINCT name || '-' || place) ColorCnt
FROM yourtable
GROUP BY Color
导致:
COLOR ColorCnt
-----------------
Green 1
Blue 4
Red 2
答案 1 :(得分:0)
这不能为您提供所需的结果吗?
select TO_CHAR(Name)||'-'||(Place) , color, count(*) as color_count
from mytable
group by name, place, color
答案 2 :(得分:0)
sgeddes的答案很好。您的查询的问题是您的外部查询引用color
,但它不在您的内部查询中。您可以通过执行以下操作来解决此问题:
select Color, count(Color)
from (select TO_CHAR(Name)||'-'||(Place) as unique_ident, color
from mytable
group by TO_CHAR(Name)||'-'||(Place), color
) as inline
group by Color;
然而,count(distinct)
配方可能更可取。