Mysql count group by several criteria - mysql

An example of a table that I use looks:
id_user | process_number | date
---------------------------------------
01 | 1 | 2012-05-04
01 | 1 | 2012-05-04
02 | 0 | 2012-05-04
01 | 1 | 2012-05-05
01 | 2 | 2012-05-05
01 | 0 | 2012-05-05
02 | 0 | 2012-05-05
03 | 1 | 2012-05-05
04 | 2 | 2012-05-05
05 | 1 | 2012-05-05
05 | 1 | 2012-05-05
06 | 0 | 2012-05-05
07 | 3 | 2012-05-05
The result that I want to get is the number of unique processes (group by id_user), number of total process (group by date) and count distinct user when process_number is not equal to "0" (grouped by date and by user). I've tried to achieve this with query:
SELECT COUNT( DISTINCT id_user ) AS user_process, COUNT( * ) AS total_process, DATE( date) AS date_process, SUM( IF( process_number = '0', 0, 1 ) ) notification FROM a_proces GROUP BY DATE( date ) ORDER BY DATE( date ) DESC LIMIT 0 , 10
This query adds all users in a given day, who had more than one process_number that is greater than zero in notification column. The correct result in this example should look like:
date_process | user_process | total_process | notification
------------------------------------------------------------------------------
2012-05-04 | 2 | 3 | 1
2012-05-05 | 7 | 10 | 6
Thanks for any help.

You should be able to use the following:
SELECT DATE_FORMAT( date, '%d/%m/%Y' ) AS date_process,
COUNT(DISTINCT id_user) AS user_process,
COUNT( * ) AS total_process,
count(distinct case when process_number <> 0 then id_user end) notification
FROM a_proces
GROUP BY DATE( date )
ORDER BY DATE( date ) DESC
LIMIT 0 , 10;
See SQL Fiddle with Demo
If the notification result that you want is the total count of each distinct process_number for each id_user, then you can use:
SELECT DATE_FORMAT( p.date, '%d/%m/%Y' ) AS date_process,
COUNT(DISTINCT p.id_user) AS user_process,
COUNT( * ) AS total_process,
d.notification
FROM a_proces p
INNER JOIN
(
select count(process_number) notification, date
from
(
select distinct id_user, process_number,
DATE_FORMAT( date, '%d/%m/%Y' ) date
from a_proces
where process_number <> 0
) d
group by date
) d
on DATE_FORMAT( p.date, '%d/%m/%Y' ) = d.date
GROUP BY DATE( p.date ), d.notification
ORDER BY DATE( p.date ) DESC
LIMIT 0 , 10;
See SQL Fiddle with Demo

Related

MySQL get latest consecutive rows set by date

I am retrieving following rows with query,
SELECT id, entry_id, DATE(entry_date)
FROM entries
WHERE entry_id = 51
ORDER BY entry_date DESC
+-----+----------+---------------------+
| id | entry_id | entry_date |
+-----+----------+---------------------+
| 84 | 51 | 2021-02-27 xx:xx:xx |<---
| 81 | 51 | 2021-02-26 xx:xx:xx | |
| 76 | 51 | 2021-02-25 xx:xx:xx | |-- consecutive set
| 74 | 51 | 2021-02-25 xx:xx:xx | |
| 73 | 51 | 2021-02-24 xx:xx:xx |<---
| 52 | 51 | 2021-02-20 xx:xx:xx |
| 44 | 51 | 2021-02-19 xx:xx:xx |
| 32 | 51 | 2021-02-18 xx:xx:xx |
| . | .. | ... |
| . | .. | ... |
+-----+----------+---------------------+
entry_date's data type is timestamp. The time does not matter here in entry_date. I am only concerned with the dates without time.
I want to get rows only with "latest consecutive dates" OR first and last date of the latest consecutive set for an "entry_id".
for example, for entry_id = 51, I want only rows,
+-----+----------+------------+
| id | entry_id | entry_date |
+-----+----------+------------+
| 84 | 51 | 2021-02-27 |
| 81 | 51 | 2021-02-26 |
| 76 | 51 | 2021-02-25 |
| 74 | 51 | 2021-02-25 |
| 73 | 51 | 2021-02-24 |
+-----+----------+------------+
OR I want to get first and last date of "latest consecutive dates" set for entry_id = 51
eg. in this case entry_date 2021-02-24 and 2021-02-27.
I don't have any experience with writing such queries. I can get all the records order by DESC for entry_id = 51 and write a script to get latest consecutive rows but since there are hundreds of thousands of rows which can be sometimes inefficient to process just to get latest consecutive rows.
Please note that there can be some entries with the same date (in this case: 2021-02-25) which are also considered in the result.
Edit: I am using MySQL 5.6.
This is a type of gaps-and-islands problem solved using lead() to determine where there is a gap of more than one day.
select entry_id, min(entry_date), max(entry_date)
from (select e.*,
sum(case when entry_date < next_entry_date - interval 1 day then 1 else 0 end) over (partition by entry_id order by entry_date desc) as grp
from (select e.*,
lead(entry_date) over (partition by entry_id order by entry_date) as next_entry_date
from entries e
) e
) e
where grp = 0
group by entry_id;
Then the cumulative sum is done in reverse order. So the last group has a cumulative sum of 0.
Here is a db<>fiddle.
You might try finding the gap dates with a self join, using group_concat to get them together, then using substring_index to split out the first two, then use that to get all rows bounded by the dates.
SELECT q.*
FROM entries q
JOIN (
SELECT entry_id,
SUBSTRING_INDEX(dts, ',', 1) AS upperDate,
SUBSTRING_INDEX(SUBSTRING_INDEX(dts, ',', 2), ',', -1) AS lowerDate
FROM (
SELECT x.entry_id,
GROUP_CONCAT(if(y.entry_date IS NULL, x.entry_date, NULL) ORDER BY x.entry_date DESC) AS dts
FROM entries x
LEFT JOIN entries y ON (
x.entry_id = y.entry_id AND
x.entry_date = y.entry_date - interval 1 DAY
)
GROUP BY 1
) z
) bounds ON (
q.entry_id = bounds.entry_id AND
q.entry_date <= bounds.upperDate AND
q.entry_date > bounds.lowerDate
)
You can avoid the self join with some variables, but that adds a certain level of complexity of its own and makes the logic a little harder to read and maintain.
SELECT entry_id, entry_date FROM (
SELECT entry_id, entry_date, elapsed,
NOT #latch AS keep,
#latch:= if(elapsed > 1, TRUE, #latch),
#latch := if(#currID <> entry_id, FALSE, #latch),
#currID := entry_id
FROM (
SELECT entry_id, entry_date,
TIMESTAMPDIFF(DAY, #prevDate, entry_date) AS elapsed,
#prevDate := entry_date
FROM (
SELECT entry_id, entry_date
FROM entries
JOIN (SELECT #currID := 0, #prevDate := null, #latch:=false) v
ORDER BY entry_id, entry_date ASC
) z
) y ORDER BY entry_id, entry_date DESC
) x WHERE keep
In MySQL 8, it can be done like this:
WITH e2 AS (
SELECT entry_id, entry_date
, COALESCE(LAG(DATE(entry_date))
OVER (PARTITION BY entry_id
ORDER BY entry_date DESC)
- DATE(entry_date), 0)
AS date_diff
FROM entries
), e3 AS (
SELECT e2.*
, MAX(date_diff)
OVER (PARTITION BY e2.entry_id
ORDER BY e2.entry_date DESC
ROWS UNBOUNDED PRECEDING)
AS max_diff
FROM e2
)
SELECT entry_id
, DATE(MIN(entry_date)) AS min_date
, DATE(MAX(entry_date)) AS max_date
FROM e3
WHERE max_diff <= 1
GROUP BY entry_id;
Result
entry_id
min_date
max_date
51
2021-02-24
2021-02-27
See DB Fiddle for demo.
If you want the WHERE entry_id = 51 condition, it should be added to the first WITH query.

MySQL UNION does not seem to work correctly

I have an SQL query I am using to pull data from an orders database. I am querying 2 tables and combining the results using UNION ALL. However, the UNION ALL does not seem to work as expected. Here is the query I am using:
SELECT year(oc_order.date_added) AS year, COUNT(oc_order.order_id) as cnt, SUM( ifnull(oc_order.new_total,oc_order.total) ) as total
FROM oc_order
WHERE oc_order.order_status_id IN (1,3,5)
AND MONTH(oc_order.date_added) BETWEEN '01' AND '02'
AND DAY(oc_order.date_added) BETWEEN '01' AND '31'
GROUP BY year(oc_order.date_added)
UNION ALL
SELECT ifnull(year(str_to_date(oc_return_custom.date_added,'%d-%m-%Y %H:%i:%s')),year(str_to_date(oc_return_custom.date_added,'%Y-%m-%d %H:%i:%s')) ) AS year, COUNT(oc_return_custom.return_id) as cnt, SUM( oc_return_custom.total ) as total
FROM oc_return_custom
WHERE ifnull(MONTH(str_to_date(oc_return_custom.date_added,'%d-%m-%Y %H:%i:%s')),MONTH(str_to_date(oc_return_custom.date_added,'%Y-%m-%d %H:%i:%s')) ) BETWEEN '01' AND '02'
AND ifnull(DAY(str_to_date(oc_return_custom.date_added,'%d-%m-%Y %H:%i:%s')),DAY(str_to_date(oc_return_custom.date_added,'%Y-%m-%d %H:%i:%s')) ) BETWEEN '01' AND '31'
GROUP BY ifnull(year(str_to_date(oc_return_custom.date_added,'%d-%m-%Y %H:%i:%s')),year(str_to_date(oc_return_custom.date_added,'%Y-%m-%d %H:%i:%s')) )
ORDER BY year DESC
This is what I get from the query:
+=======+========+=======+
| year | cnt | total |
+=======+========+=======+
| 2016 | 200 | 1000 |
| 2016 | 50 | 200 |
| 2015 | 100 | 800 |
| 2015 | 10 | 50 |
+=======+========+=======+
But this is what I wanted to get:
+=======+========+=======+
| year | cnt | total |
+=======+========+=======+
| 2016 | 250 | 1200 |
| 2015 | 110 | 850 |
+=======+========+=======+
Can someone tell me what I am doing wrong???
Notes:
The oc_order table's date_added column is datetime whereas oc_return_custom 's date_added column is just text.
UNION ALL simply puts together two data sets produced by separate GROUP BY operations.
To get the expected result set you have to wrap the query in a subquery and apply an additional GROUP BY:
SELECT year, SUM(cnt) AS cnt, SUM(total) AS total
FROM ( ... your query here ...) AS t
GROUP BY year

Finding count for a Period in sql

I have a table with :
user_id | order_date
---------+------------
12 | 2014-03-23
12 | 2014-01-24
14 | 2014-01-26
16 | 2014-01-23
15 | 2014-03-21
20 | 2013-10-23
13 | 2014-01-25
16 | 2014-03-23
13 | 2014-01-25
14 | 2014-03-22
A Active user is someone who has logged in last 12 months.
Need output as
Period | count of Active user
----------------------------
Oct-2013 - 1
Jan-2014 - 5
Mar-2014 - 10
The Jan 2014 value - includes Oct -2013 1 record and 4 non duplicate record for Jan 2014)
You can use a variable to calculate the running total of active users:
SELECT Period,
#total:=#total+cnt AS `Count of Active Users`
FROM (
SELECT CONCAT(MONTHNAME(order_date), '-', YEAR(order_date)) AS Period,
COUNT(DISTINCT user_id) AS cnt
FROM mytable
GROUP BY Period
ORDER BY YEAR(order_date), MONTH(order_date) ) t,
(SELECT #total:=0) AS var
The subquery returns the number of distinct active users per Month/Year. The outer query uses #total variable in order to calculate the running total of active users' count.
Fiddle Demo here
I've got two queries that do the thing. I am not sure which one's the fastest. Check them aginst your database:
SQL Fiddle
Query 1:
select per.yyyymm,
(select count(DISTINCT o.user_id) from orders o where o.order_date >=
(per.yyyymm - INTERVAL 1 YEAR) and o.order_date < per.yyyymm + INTERVAL 1 MONTH) as `count`
from
(select DISTINCT LAST_DAY(order_date) + INTERVAL 1 DAY - INTERVAL 1 MONTH as yyyymm
from orders) per
order by per.yyyymm
Results:
| yyyymm | count |
|---------------------------|-------|
| October, 01 2013 00:00:00 | 1 |
| January, 01 2014 00:00:00 | 5 |
| March, 01 2014 00:00:00 | 6 |
Query 2:
select DATE_FORMAT(order_date, '%Y-%m'),
(select count(DISTINCT o.user_id) from orders o where o.order_date >=
(LAST_DAY(o1.order_date) + INTERVAL 1 DAY - INTERVAL 13 MONTH) and
o.order_date <= LAST_DAY(o1.order_date)) as `count`
from orders o1
group by DATE_FORMAT(order_date, '%Y-%m')
Results:
| DATE_FORMAT(order_date, '%Y-%m') | count |
|----------------------------------|-------|
| 2013-10 | 1 |
| 2014-01 | 5 |
| 2014-03 | 6 |
The best thing I could do is this:
SELECT Date, COUNT(*) as ActiveUsers
FROM
(
SELECT DISTINCT userId, CONCAT(YEAR(order_date), "-", MONTH(order_date)) as Date
FROM `a`
ORDER BY Date
)
AS `b`
GROUP BY Date
The output is the following:
| Date | ActiveUsers |
|---------|-------------|
| 2013-10 | 1 |
| 2014-1 | 4 |
| 2014-3 | 4 |
Now, for every row you need to sum up the number of active users in previous rows.
For example, here is the code in C#.
int total = 0;
while (reader.Read())
{
total += (int)reader['ActiveUsers'];
Console.WriteLine("{0} - {1} active users", reader['Date'].ToString(), reader['ActiveUsers'].ToString());
}
By the way, for the March of 2014 the answer is 9 because one row is duplicated.
Try this, but thise doesn't handle the last part: The Jan 2014 value - includes Oct -2013
select TO_CHAR(order_dt,'MON-YYYY'), count(distinct User_ID ) cnt from [orders]
where User_ID in
(select User_ID from
(select a.User_ID from [orders] a,
(select a.User_ID,count (a.order_dt) from [orders] a
where a.order_dt > (select max(b.order_dt)-365 from [orders] b where a.User_ID=b.User_ID)
group by a.User_ID
having count(order_dt)>1) b
where a.User_ID=b.User_ID) a
)
group by TO_CHAR(order_dt,'MON-YYYY');
This is what I think you are looking for
SET #cnt = 0;
SELECT Period, #cnt := #cnt + total_active_users AS total_active_users
FROM (
SELECT DATE_FORMAT(order_date, '%b-%Y') AS Period , COUNT( id) AS total_active_users
FROM t
GROUP BY DATE_FORMAT(order_date, '%b-%Y')
ORDER BY order_date
) AS t
This is the output that I get
Period total_active_users
Oct-2013 1
Jan-2014 6
Mar-2014 10
You can also do COUNT(DISTINCT id) to get the unique Ids only
Here is a SQL Fiddle

MySQL, Get number of last records in second table for each element of first, divided by specific time intervals

I have a two tables:
case_map
case_id | creation_date
________|___________________
49 | 2013-04-30
51 | 2013-05-15
82 | 2014-05-23
109 | 2013-06-01
123 | 2013-07-23
case_legend
id | case_id | operation_number | operation_date | failure
___|_________|__________________|________________|________
1 | 49 | 105 | 2013-05-03 | 0
2 | 51 | 105 | 2013-05-28 | 0
3 | 51 | 110 | 2013-05-29 | 0
4 | 51 | 115 | 2013-06-02 | 1
5 | 51 | 110 | 2013-06-05 | 0
6 | 82 | 105 | 2013-05-28 | 0
7 | 82 | 110 | 2013-05-30 | 0
8 | 82 | 115 | 2013-06-01 | 0
9 | 82 | 120 | 2013-06-01 | 0
10 | 82 | 125 | 2013-06-02 | 0
11 | 109 | 105 | 2013-06-27 | 0
12 | 123 | 105 | 2013-07-27 | 0
13 | 123 | 110 | 2013-08-10 | 0
And I want to know how many cases was recieved and how many of these cases are on operation 105 and 125 ('on operation' = value of operation_number for row with max operation_date for this case_id) in a certain time interval, for example 'from May, 2013 to Jule, 2013', splited by mounth.
For these purposes, I made ​​the following query:
SELECT
`recieved_cases`.`abbreviated_date`,
`recieved_cases`.`recieved_count`,
`operation_105`.`105_count`,
`operation_125`.`125_count`
FROM (
SELECT DATE_FORMAT( `creation_date`, '%Y-%m' ) AS `abbreviated_date`, COUNT( `case_id` ) AS `recieved_count`
FROM `case_map`
WHERE `creation_date` BETWEEN '2013-05-01' AND '2013-07-31'
GROUP BY `abbreviated_date`
) AS `recieved_cases`
LEFT JOIN (
SELECT DATE_FORMAT( `t1`.`operation_date`, '%Y-%m' ) AS `abbreviated_date`, COUNT( `t1`.`operation_number` ) AS `105_count`
FROM `case_legend` AS `t1`
WHERE `t1`.`id` = (
SELECT `t2`.`id`
FROM `case_legend` AS `t2`
WHERE `t2`.`case_id` = `t1`.`case_id`
ORDER BY `t2`.`operation_date` DESC, `t2`.`id` DESC
LIMIT 1
)
AND `operation_number` = 105
GROUP BY `abbreviated_date`
) AS `operation_105`
ON `recieved_cases`.`abbreviated_date` = `operation_105`.`abbreviated_date`
LEFT JOIN (
SELECT DATE_FORMAT( `t1`.`operation_date`, '%Y-%m' ) AS `abbreviated_date`, COUNT( `t1`.`operation_number` ) AS `125_count`
FROM `case_legend` AS `t1`
WHERE `t1`.`id` = (
SELECT `t2`.`id`
FROM `case_legend` AS `t2`
WHERE `t2`.`case_id` = `t1`.`case_id`
ORDER BY `t2`.`operation_date` DESC, `t2`.`id` DESC
LIMIT 1
)
AND `operation_number` = 125
GROUP BY `abbreviated_date`
) AS `operation_125`
ON `recieved_cases`.`abbreviated_date` = `operation_125`.`abbreviated_date`
ORDER BY `recieved_cases`.`abbreviated_date`
My problem is that such a request takes into account also cases with creation_date not in a specified time interval. Ie case, which has the last operation in the specified interval, but was created earlier - is taken into account, and it should not be. How can I fix this query?
Desired result for provided example is:
abbreviated_date | recieved_count | 105_count | 125_count
_________________|________________|___________|__________
2013-05 | 2 | 0 | 0
2013-06 | 1 | 1 | 1
2013-07 | 1 | 0 | 0
Can a temporary table helps me here? I mean if I first create table like case_legend with creation_date field. I think on this one for the last hour, but not sure how to do it.
P.S. Also, if my query is bad and you can give me an advise how to optimize it - I would be grateful.
This may be what you want:
select year(cm.creation_date), month(cm.creation_date),
count(distinct case when cl.operation_number = 105 then case_id end) as op105,
count(distinct case when cl.operation_number = 125 then case_id end) as op125
from case_map cm join
case_legend cl
on cm.case_id = cl.case_id
group by year(cm.creation_date), month(cm.creation_date)
order by 1, 2;
If not, you can start with the most recent date for each of the operations:
select cm.*,
max(case when cl.operation_number = 105 then operation_date end) as op105,
max(case when cl.operation_number = 125 then operation_date end) as op125
from case_map cm join
case_legend cl
on cm.case_id = cl.case_id
group by cm.case_id;
And then work from there.
EDIT:
Now that you supplied the desired results, the query is not too difficult:
select date_format(cm.creation_date, '%Y-%m' ) as yyyymm,
sum(cl.opeartion_number = 105) as op105,
sum(cl.opeartion_number = 125) as op125
from case_map cm join
case_legend cl
on cm.case_id = cl.case_id
where not exists (select 1
from case_legend cl2
where cl2.case_id = cl.case_id and
cl2.operation_date > cl.operation_date
)
group by date_format(cm.creation_date, '%Y-%m')
order by 1;
So the answer is: You don't need temporary tables.
Here's this fiddle:
http://sqlfiddle.com/#!2/8673a3/9
It gives you the same results but in a different format perhaps, than what you prefer.
select
DATE_FORMAT( operation_date, '%Y-%m' ) as `Date`,
count(cl.case_id), cl.operation_number,
t.count
from
case_legend cl
left join case_map cm
on cl.case_id = cm.case_id
left join (
select
DATE_FORMAT( cl.operation_date, '%Y-%m' ) as `Date`, count(cl.case_id) as `count`, 'All' as operation_number
from
case_legend cl
left join case_map cm
on cl.case_id = cm.case_id
where operation_date BETWEEN '2013-05-01' AND '2013-07-31'
GROUP BY DATE_FORMAT( operation_date, '%Y-%m' )
) t
on t.Date = DATE_FORMAT( operation_date, '%Y-%m' )
where cl.operation_number in (105, 125)
and operation_date BETWEEN '2013-05-01' AND '2013-07-31'
GROUP BY DATE_FORMAT( operation_date, '%Y-%m' ), cl.operation_number
;

MySQL SUM in different ways

I have two tables
user_raters:
| id(int) | to_id(int) | value(int) | created_at(datetime)
|1 | 2 | 1 | 2009-03-01 00:00:00
EDIT: I changed the user_rater_id. history_user_raters.user_rater_id is related to user_raters.id
history_user_raters:
| id(int) | user_rater_id(int) | value(int) | created_at(datetime)
| 1 | 1 | 1 | 2009-03-02 00:00:00
| 2 | 1 | 1 | 2009-03-02 00:00:00
| 3 | 1 | -1 | 2009-03-02 00:00:00
| 4 | 1 | 1 | 2009-03-03 00:00:00
| 5 | 1 | -1 | 2009-03-03 00:00:00
| 6 | 1 | -1 | 2009-03-03 00:00:00
| 7 | 1 | -1 | 2009-03-03 00:00:00
I want to count the sum of the values from history_user_raters as it relates to the to_id from user_raters. The result from the query should be:
| year | month | day | total | down | up
| 2009 | 3 | 2 | 1 | 1 | 2
| 2009 | 3 | 3 | -2 | 3 | 1
I have a query that is close, but it is not counting the up and down correctly. The total is right. Can some one help me write the query or new query that calculates correct up and down?
My current query:
SELECT
YEAR(history.created_at) AS `year`,
MONTH(history.created_at) AS `month`,
DAY(history.created_at) AS `day`,
SUM(history.value) as `total`,
(SELECT
abs(SUM(historydown.value))
FROM `user_raters` as raterdown
INNER JOIN `history_user_raters` AS historydown
WHERE raterdown.to_id = 2
AND historydown.value = -1
AND date(historydown.created_at)
GROUP BY history.created_at) as down,
(SELECT SUM(historyup.value)
FROM `user_raters` as raterup
INNER JOIN `history_user_raters` AS historyup
WHERE raterup.to_id = 2
AND historyup.value = 1
AND date(history.created_at)
GROUP BY raterup.to_id) as up
FROM `user_raters`
INNER JOIN history_user_raters AS history ON user_raters.id = history.user_rater_id
WHERE (user_raters.to_id = 2)
GROUP BY DATE(history.created_at)
I might see it too simply (and sorry I can't test with data at the moment), but I'm guessing the following trick with two CASE statements would do just what is needed
SELECT
YEAR(history.created_at) AS year,
MONTH(history.created_at) AS month,
DAY(history.created_at) AS day,
SUM(history.value) as total,
SUM(CASE WHEN history.value < 0 THEN history.value ELSE 0 END) as down,
SUM(CASE WHEN history.value > 0 THEN history.value ELSE 0 END) as up
FROM `user_raters`
INNER JOIN `history_user_raters` AS history
ON user_raters.id = history.user_rater_id
WHERE (user_raters.to_id = 1) -- or some other condition...
GROUP BY DATE(history.created_at)
EDIT: #OMG Ponies deleted his answer. This response make no sense now, but I am not going to delete my answer, because I think it is silly.
#OMG ponies
Your query runs, but it returns no results. I had to adjust it a bit to add the to_id in the main queries where clause
SELECT
YEAR( t.created_at ) AS `year` ,
MONTH( t.created_at ) AS `month` ,
DAY( t.created_at ) AS `day` ,
SUM( t.value ) AS `total` ,
MAX( COALESCE( x.sum_down, 0 ) ) AS down,
MAX( COALESCE( y.sum_up, 0 ) ) AS up
FROM history_user_raters AS t
JOIN user_raters AS ur ON ur.to_id = t.user_rater_id
LEFT JOIN (
SELECT hur.user_rater_id,
SUM( hur.value ) AS sum_down
FROM history_user_raters AS hur
WHERE hur.value = -1
GROUP BY hur.user_rater_id
) AS x ON x.user_rater_id = t.user_rater_id
LEFT JOIN (
SELECT hur.user_rater_id,
SUM( hur.value ) AS sum_up
FROM history_user_raters AS hur
WHERE hur.value =1
GROUP BY hur.user_rater_id
) AS y ON y.user_rater_id = t.user_rater_id
WHERE ur.to_id =1
GROUP BY YEAR( t.created_at ) , MONTH( t.created_at ) , DAY( t.created_at )