Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of unique entries per day

I have a table structure like this :

ID    commission     transdate           token 

1      10         2013-11-22 08:24:00    token1
2      10         2013-11-22 08:24:00    token1
3      10         2013-11-22 08:24:00    token1
4      10         2013-11-22 08:24:00    token1
5      10         2013-11-22 08:24:00    token1
6      29         2013-11-22 06:24:00    token2
7      29         2013-11-22 06:24:00    token2

The thing is that I have duplicate entries in my table for some data and I need to filter it out while taking the SUM. If the transdate is same and the token is same, I only need to consider one entry for the sum. The sum I need is sum of all the commission for all token grouped by date.

How can I do this through query ?

like image 752
Happy Coder Avatar asked Dec 07 '25 00:12

Happy Coder


2 Answers

Use DATE with GROUP BY:

SELECT 
  SUM(comission), 
  token, 
  DATE(transdate) 
FROM
  t 
GROUP BY 
  token, 
  DATE(transdate)
like image 138
Alma Do Avatar answered Dec 08 '25 13:12

Alma Do


try below query,

To consider only one entry for date for a particular token:

SELECT commission,
       date(transdate),
       token 
FROM tokens 
GROUP BY date(transdate),
         token;

To get a sum of commission for date

SELECT SUM(commission),
       DATE(transdate),
       token 
FROM tokens 
GROUP BY DATE(transdate),
         token;

Sql Fiddle Example

like image 35
Suhel Meman Avatar answered Dec 08 '25 14:12

Suhel Meman