简体   繁体   中英

How to get count of records for some specific where condition in SQL

I have a data in which there are 2 columns: USER_ID and CONTRACT_ID. There can be 'n' number of users in a contract id.

1    0303000
2    0303000
3    1234567
1    0303000
2    0303000
3    1234567 
1    0303000
2    0303000
3    1234567
1    0303000
2    0303000
3    1234567 

Now i want something like this:

Count    CONTRACT_ID
8        0303000
4        1234567

Someone please help me how to create a sql query to get this kind of result.

You can just do a GROUP BY on CONTRACT_ID and COUNT(*) . Like this:

SELECT 
  COUNT(*) as count,
  CONTRACT_ID 
FROM 
  table1 
GROUP BY 
  CONTRACT_ID

You need to use Group By

SELECT count(*) as `Count`,CONTRACT_ID 
from tablename Group By CONTRACT_ID

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