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

时间:2021-05-21 04:26:01

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:

我不确定这是否可行,但我想计算表中唯一值的数量。我知道要计算我做的唯一folderID的数量:

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的唯一组合数。有没有办法做到这一点?

4 个解决方案

#1


24  

select count(*) from (
  select distinct folderid, userid from folder
)

#2


9  

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

#3


5  

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...

希望能帮助到你...

#4


1  

i think you can try to group the select statement with folder id

我想你可以尝试将select语句与文件夹ID分组

eg.

例如。

i have a table

我有一张桌子

folderid userid

folderid用户ID

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

#1


24  

select count(*) from (
  select distinct folderid, userid from folder
)

#2


9  

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

#3


5  

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...

希望能帮助到你...

#4


1  

i think you can try to group the select statement with folder id

我想你可以尝试将select语句与文件夹ID分组

eg.

例如。

i have a table

我有一张桌子

folderid userid

folderid用户ID

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