0

I am trying to run a query in SQL Server to loop through the associates and count how many transactions they processed.

select 
    Id, submitter_name, submitter_id,
    count(*) as numberOfTickets
from
    Tickets 
where 
    created_at between '2018-11-13' and '2018-11-14' 
    and ',' + tags + ',' like '%,' + 'tickets' + ',%'
group by 
    Id, submitter_name, submitter_id;

This results shows the associates name over and over with one transaction. I am trying to count and show there name once with the total count for numberOfTickets.

I am not sure where I am going wrong here. Any help would be greatly appreciated!

2
  • Can you please supply some sample data and expected results? Also, your WHERE clause ',' + tags + ',' like '%,' + 'tickets' + ',%' very strongly suggests you're storing delimited data in your table. This is a really bad idea; I would strongly suggest redesigning your database to a more normalised format. (Or at least using a string splitter, and not LIKE with a leading wild card, but a redesign would be a far better idea). Commented Jan 22, 2019 at 18:39
  • Are you just looking for COUNT(DISTINCT)? Commented Jan 22, 2019 at 18:42

1 Answer 1

1

I'm going to make the assumption that Id is the Id of the ticket. You don't want that included in your select and groupby because that is the number you want to count.

select submitter_name, submitter_id,
       count(*) as numberOfTickets
from  Tickets 
where created_at between '2018-11-13' and '2018-11-14' and
      ',' + tags + ',' like '%,' + 'tickets' + ',%'
group by submitter_name, submitter_id;

As a side note, for the love of all that is holy, don't store your tags like that. Your RDBMS will hate you for it. Use a many-to-many relationship.

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.