0

I have a table with two columns of interest: apc and tid. The same apc can be in a row with multiple tids, and an apc - tid pair can appear multiple times in the database (with different timestamps).

I want to get the apcs with a count of how many unique tids they match with. 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 onapc and tid, but I get all of the duplicates as unique counts (so 3 and 3 above). I tried throwing in distinct in a few places, but that didn't work either.

1
  • 1
    This is a basic aggregation query with count(distinct). Commented Jun 12, 2014 at 19:23

1 Answer 1

1

You need to do a Count(Distinct tid):

Select  apc, Count(Distinct tid) as Count
From    Table
Group by apc
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.