I am trying to query a database that has these params:
Transaction Date, User Email Address
What I have done is use this query:
SELECT [User Email Address], COUNT(*) AS 'count'
FROM
[DATABASE].[TABLE]
GROUP BY [User Email Address]
which displays a table with params:
Email Address, count
In this case the count column shows the number of occurrences of the user email in the original table.
What I am trying to do next is look at the Transaction Date column for the last year up to today, and compare the count column for this subset with the count column of the orginal (which goes back some 3 years). Specifically, I want my end resultant table to be:
User User Email Address, countDiff
where countDiff is the difference in counts from the one year subset and the original subset.
I have tried:
SELECT [User Email Address], [Transaction Date], [count - COUNT(*)] AS 'countdDifference'
FROM (
SELECT [User Email Address], COUNT(*) AS 'count'
FROM
[DATABASE].[TABLE]
GROUP BY [User Email Address]
) a
WHERE a.[Transaction Date] >= '2011-08-07 00:00:00.000'
ORDER BY [count] DESC
But I get the error that [Transaction Date]
is not in the Group By clause or aggregate. If I put it in the Group By next to [User Email Address]
, it messes up the data.
This is actually a common problem I've had. Any ways to circumvent this?