Mysql 使用两列选择唯一计数
我有一个包含两列的表:Mysql 使用两列选择唯一计数,mysql,sql,Mysql,Sql,我有一个包含两列的表:apc和tid。相同的apc可以与多个tids连成一行,并且apc-tid对可以在数据库中多次出现(具有不同的时间戳) 我想获得apcs,并计算它们与多少唯一tids匹配。因此: apc tid --------------- abc 012 abc 012 abc 322 def 322 def 432 def 000 我想得到: apc count ----
apc
和tid
。相同的apc
可以与多个tid
s连成一行,并且apc
-tid
对可以在数据库中多次出现(具有不同的时间戳)
我想获得apc
s,并计算它们与多少唯一tid
s匹配。因此:
apc tid
---------------
abc 012
abc 012
abc 322
def 322
def 432
def 000
我想得到:
apc count
-----------------
abc 2
def 3
我试着在
apc
和tid
上进行分组,但我得到的所有重复项都是唯一计数(上面的3和3)。我试着在一些地方加入distinct,但也没用。你需要做一个计数(distinct tid)
:
这是一个基本的聚合查询,具有
count(distinct)
。
Select apc, Count(Distinct tid) as Count
From Table
Group by apc