如何计算表中的相似条目?

时间:2011-10-13 13:51:39

标签: sql sql-server-2008 tsql

我有一张包含以下数据的表

companyid    subscriptionid
1            21
1            23
2            89
4            99
4            101

我想计算每个独特公司出现的次数,我该如何使用t-sql进行此操作?

所以例如我想看1 = 2次......但我不介意它是否在表中我不需要打印数据....

3 个答案:

答案 0 :(得分:3)

SELECT companyid, COUNT(*) AS CompanyCount
    FROM YourTable
    GROUP BY companyid

答案 1 :(得分:2)

SELECT t.CompanyId, COUNT(1)
FROM schema.table AS t
GROUP BY t.CompanyId

答案 2 :(得分:1)

SELECT companyid, count(companyid) as TotalEntries
FROM YourTable
GROUP BY companyid