I'm very new to SQL. I have constructed a query such that each row designates the occurrence of an event. Here is a sample (this is stored in a temporary table in the query).
Id Date Acct FirstName LastName
--------------------------------------------
123 2016-02-02 456789 Joan Smith
123 2016-02-03 456789 Joan Smith
456 2016-02-02 987654 John Adams
789 2016-02-04 123789 Matt James
789 2016-02-07 123789 Matt James
What I'm looking to do is take that table and count the total number of times the event occurred for each person, but also have the ability to see which dates the event occurred on.
So the final table should be something like this:
ID Acct FirstName LastName 2/1/2016 2/2/2016 2/3/2016 2/4/2016 2/5/2016 2/6/2016 2/7/2016 Total
123 456789 Joan Smith 1 1 2
456 987654 John Adams 1 1
789 123789 Matt James 1 1 2
I am able to get a total number of occurrences, but I don't know how to get the dates as new columns. Would CASE
work? Or PIVOT
? I'm not sure how I would use those two commands to accomplish this.
Here is the query that I have constructed so far and the output
select
ID,
Acct,
FirstName,
LastName,
Count(Date) as Total
from
##tempTable
group by
Acct, PlayerID, FirstName, LastName
Output:
ID Acct FirstName LastName Total
-----------------------------------------
123 456789 Joan Smith 2
456 987654 John Adams 1
789 123789 Matt James 2