28

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:

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?

1
  • select count(distinct folder) from folder
    – user330315
    Commented Dec 28, 2015 at 12:03

6 Answers 6

46
select count(*) from (
  select distinct folderid, userid from folder
)
1
  • Duh, hadn't seen this is Oracle... on MSSQL you'd need it... my bad, will remove comment again =(
    – deroby
    Commented Dec 15, 2011 at 12:26
13
select count(*) from (
    select folderId, userId
    from folder
    group by folderId, userId
) t
4
  • I tried the other solutions provided in the answers, but in my case only this works. The significant difference I can see is the presence of a 't' at then end. Can some one explain this part?
    – jazaman
    Commented Dec 24, 2015 at 5:27
  • 1
    @jazaman, t is a sub-query alias, I've added as keyword for clarification. Commented Dec 28, 2015 at 11:15
  • Oracle doesn't support AS for a table alias.
    – user330315
    Commented Dec 28, 2015 at 12:03
  • @a_horse_with_no_name, you are right. My mistake, since ~4 years only SQL Server. Commented Dec 28, 2015 at 17:06
5

This will give you the count of unique folderid and userid combinations:

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

Hope it helps...

0
1

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

eg.

i have a table

folderid userid

1 11

1 11

2 12

2 12

3 13

3 13

Query is

select count(folderid) from testtable group by folderid, userid
1
  • For your sample data, this query will return 3 rows, all with the value 2, which is not what the OP is asking for.
    – Allan
    Commented Dec 15, 2011 at 14:03
0
SELECT 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))
FROM   super_sub_group_mapping
GROUP  BY Concat(super_group_account_id, ' ', sub_group_account_id)
HAVING Count(Concat(super_group_account_id, ' ', sub_group_account_id)) > 1 
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 13, 2022 at 10:49
-1
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 ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.