SQL - 如何计算列的唯一组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8519747/
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
SQL - how to count unique combination of columns
提问by Kaskade
I'm not sure if this is possible but I want to count the number of unique value in a table. I know to count the number of unique folderIDs I do:
我不确定这是否可行,但我想计算表中唯一值的数量。我知道要计算我所做的唯一文件夹 ID 的数量:
select count(folderid) from folder
but I want the count of the number of unique combination of folderid and userid in the folder table. Is there a way to do this?
但我想要文件夹表中 folderid 和 userid 的唯一组合的数量。有没有办法做到这一点?
回答by tobiasbayer
select count(*) from (
select distinct folderid, userid from folder
)
回答by Micha? Powaga
select count(*) from (
select folderId, userId
from folder
group by folderId, userId
) t
回答by Ollie
This will give you the count of unique folderid and userid combinations:
这将为您提供唯一文件夹 ID 和用户 ID 组合的计数:
SELECT count(*)
FROM (
SELECT DISTINCT
folderid,
userid
FROM folder
);
Hope it helps...
希望能帮助到你...
回答by Murtaza
i think you can try to group the select statement with folder id
我认为您可以尝试将 select 语句与文件夹 id 分组
eg.
例如。
i have a table
我有一张桌子
folderid userid
1 11
1 11
2 12
2 12
3 13
3 13
文件夹 ID 用户 ID
1 11
1 11
2 12
2 12
3 13
3 13
Query is
查询是
select count(folderid) from testtable group by folderid, userid
回答by Rahul Kamble
select APPRSL_ID,SECTION_ID, count(APPRSL_ID||SECTION_ID)
from REMARKSBYEACHSECTION
group by APPRSL_ID,SECTION_ID
having count(APPRSL_ID||SECTION_ID)>1 ;