SQL 如何计算 Oracle 中的唯一行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26538625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count unique rows in Oracle
提问by mck
I have an oracle database table with a lot of columns. I'd like to count the number of fully unique rows. The only thing I could find is:
我有一个包含很多列的 oracle 数据库表。我想计算完全唯一的行数。我唯一能找到的是:
SELECT COUNT(DISTINCT col_name) FROM table;
This however would require me listing all the columns and I haven't been able to come up with syntax that will do that for me. I'm guessing the reason for that is that this query would be very low performance? Is there a recommended way of doing this?
但是,这需要我列出所有列,而我无法想出可以为我做到这一点的语法。我猜这是因为这个查询的性能会非常低?有推荐的方法吗?
回答by okaram
How about
怎么样
SELECT COUNT(*)
FROM (SELECT DISTINCT * FROM Table)
回答by DLLB
It depends on what you are trying to accomplish. To get a count of the distinct rows by specific column, so that you know what data exists, and how many of that distinct data there are:
这取决于您要实现的目标。要按特定列获取不同行的计数,以便您知道存在哪些数据,以及有多少不同数据:
SELECT DISTINCT
A_CODE, COUNT(*)
FROM MY_ARCHV
GROUP BY A_CODE
--This informs me there are 93 unique codes, and how many of each of those codes there are.
--这告诉我有 93 个唯一代码,以及每个代码有多少。
Another method
另一种方法
--How to count how many of a type value exists in an oracle table:select A_CDE
, --the value you need to countcount(*) as numInstances
--how many of each valuefrom A_ARCH
-- the table where it residesgroup by A_CDE
-- sorting method
--如何统计一个oracle表中某个类型值有多少个:select A_CDE
,--需要统计count(*) as numInstances
的值from A_ARCH
--每个值有多少个--所在表group by A_CDE
--排序方法
Either way, you get something that looks like this:
无论哪种方式,您都会得到如下所示的内容:
A_CODE Count(*)
1603 32
1600 2
1605 14
回答by T I
I think you want a count of all distinct rows from a table like this
我想你想从这样的表中计算所有不同的行
select count(1) as c
from (
select distinct *
from tbl
) distinct_tbl;
回答by Nerav
SELECT DISTINCT col_name, count(*) FROM table_namegroup by col_name
SELECT DISTINCT col_name, count(*) FROM table_namegroup by col_name