简体   繁体   中英

Getting count of distinct values

resulttable:

+-------------+-----------+
| resultSetID | projectID |
+-------------+-----------+
|           1 |         1 |
|           1 |         2 |
|           1 |         3 |
|           2 |         1 |
|           2 |         2 |
|           3 |         1 |
|           3 |         2 |
|           3 |         3 |
+-------------+-----------+

Query:

SELECT
    COUNT( projectID ) 
FROM
    resulttable
WHERE
    projectID = 3

... correctly returns 2. However, I want the counts of each ID without using the WHERE condition, how do I do that?

Do you just want group by ?

SELECT projectId, COUNT( * ) 
FROM resulttable
GROUP BY projectID;

The following query is used to get all the data for the projectId but if you want to get a specific query data without where you can use having instead

SELECT projectId, COUNT( * ) 
FROM resulttable
GROUP BY projectId having projectId = 3;

The following query should work for you:

SELECT projectID, SUM(resultSetID) 
FROM resulttable
GROUP BY projectID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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