简体   繁体   English

使用两列选择唯一计数

[英]Select unique count using two columns

I have a table with two columns of interest: apc and tid . 我有一个有两列感兴趣的表: apctid The same apc can be in a row with multiple tid s, and an apc - tid pair can appear multiple times in the database (with different timestamps). 同一apc可以与多个tid连续出现,并且apc - tid对可以在数据库中出现多次(使用不同的时间戳)。

I want to get the apc s with a count of how many unique tid s they match with. 我想获得apc的数量与它们匹配的唯一tid的计数。 So for: 因此对于:

apc        tid
---------------
abc        012
abc        012
abc        322
def        322
def        432
def        000

I want to get: 我想得到:

apc        count
-----------------
abc          2
def          3

I tried doing a group by on apc and tid , but I get all of the duplicates as unique counts (so 3 and 3 above). 我尝试在apctid上进行分组,但我将所有重复项作为唯一计数(因此上面的3和3)。 I tried throwing in distinct in a few places, but that didn't work either. 我尝试在几个地方投掷不同球,但那也不起作用。

You need to do a Count(Distinct tid) : 您需要做一个Count(Distinct tid)

Select  apc, Count(Distinct tid) as Count
From    Table
Group by apc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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