简体   繁体   English

SQL - 如何计算列的唯一组合

[英]SQL - how to count unique combination of columns

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.但我想要计算文件夹表中文件夹 ID 和用户 ID 的唯一组合的数量。 Is there a way to do this?有没有办法做到这一点?

select count(*) from (
  select distinct folderid, userid from folder
)
select count(*) from (
    select folderId, userId
    from folder
    group by folderId, userId
) t

This will give you the count of unique folderid and userid combinations: 这将为您提供唯一的folderid和userid组合的计数:

SELECT count(*)
  FROM (
        SELECT DISTINCT
               folderid,
               userid
          FROM folder
);

Hope it helps... 希望能帮助到你...

SELECT super_group_account_id, 选择 super_group_account_id,
sub_group_account_id, sub_group_account_id,
Concat (super_group_account_id, ' ', sub_group_account_id), Concat (super_group_account_id, ' ', sub_group_account_id),
Count ( Concat (super_group_account_id, ' ', sub_group_account_id)) 计数( Concat (super_group_account_id, ' ', sub_group_account_id))
FROM super_sub_group_mapping FROM super_sub_group_mapping
GROUP BY Concat (super_group_account_id, ' ', sub_group_account_id) GROUP BY Concat (super_group_account_id, ' ', sub_group_account_id)
HAVING Count ( Concat (super_group_account_id, ' ', sub_group_account_id)) > 1 HAVING Count ( Concat (super_group_account_id, ' ', sub_group_account_id)) > 1

i think you can try to group the select statement with folder id 我想你可以尝试将select语句与文件夹ID分组

eg. 例如。

i have a table 我有一张桌子

folderid userid folderid用户标识

1 11 1 11

1 11 1 11

2 12 2 12

2 12 2 12

3 13 3 13

3 13 3 13

Query is 查询是

select count(folderid) from testtable group by folderid, userid
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 ;

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

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