I've got a table of ticket assignments showing the different groups a ticket is transferred to before its resolved. Here is a simplified table:
asgn_grp | date | ticket_id
---------|--------|----------
A | 1-1-15 | 1
A | 1-2-15 | 1
B | 1-3-15 | 1
A | 1-1-15 | 2
C | 1-2-15 | 2
B | 1-3-15 | 2
C | 1-1-15 | 3
B | 1-2-15 | 3
I need to get a count of the second distinct group that a ticket was assigned to, meaning I want to know once a ticket is transferred out of the group its in, internal transfers don't count. So the second distinct group for ticket 1
is B
, ticket 2
is C
, ticket 3
is B
. I need to get a count of these, so the end result I need is
asgn_grp | count
---------|-------
B | 2
C | 1
I've tried
SELECT distinct top 2 asgn_grp, ROW_NUMBER() OVER (ORDER BY date)
As my sub-query and pulling the second one out of that, but when I add the ROW_NUMBER()
it messes up my distinct. If I pull the ROW_NUMBER()
out of the sub-query, I have now way to order my values to ensure I get the second one after I DISTINCT
the list.
Also, let me know if I was unclear about anything.