I have 3 tables: user_transaction, transaction and users
I am trying to create a summary table of distinct transactions
on a each date basis
for each employee
for each resource
as follows: (filtering datetime to date using format specifier)
select
count(distinct(user_transaction.trans_id)) as obj,
DATE_FORMAT(DATE, '%Y-%m-%d') as tdate
from
user_transaction, transaction,users
where
users.id = user_transaction.user_id
and users.employeeid = 'samuel'
and user_transaction.trans_id=transaction.id
and resource_id =1 and transactiontext like '%<-->%'
and DATE_FORMAT(user_transaction.date,'%Y-%m-%d')
between '2011-08-30' and '2011-11-28'
group by
DATE_FORMAT(user_transaction.DATE, '%Y-%m-%d')
result of the above query is I get distinct transaction count for user ‘samuel’ on each date in the range
e.g. 2011-08-30…………22;
2011-09-01…………431;
2011-09-03…………64 ;
etc..
now I sum up all these values I get a count like total for the user : 1486
This count is not matching up with my modified query for the same user and date range if I do a direct count without the group by on Date field.
i.e. If I run the query below without date grouping :
select
count(distinct(user_transaction.trans_id)) as obj,
from
user_transaction, transaction,users
where
users.id = user_transaction.user_id and users.employeeid = 'samuel'
and user_transaction.trans_id=transaction.id
and resource_id =1 and transactiontext like '%<-->%'
and DATE_FORMAT(user_transaction.date,'%Y-%m-%d')
between '2011-08-30' and '2011-11-28'
This gives me a count like 1452,
the count over the range with date grouping and summing it to get total is generally more for every user, not sure why.
I need to have the counts calculated for each date for my summary table.
The count I get directly without using the group by clause is correct, checked it in the table.
What exactly am I doing wrong in the group by clause?
Since you are counting the distinct values and grouping by date, it may be possible that you have the same trans_id on multiple days, thus giving you a greater count in the query with the group by than that without.