2

Can anybody help me to calculate median value for each date?

activity
user_id login_time  bet
105 2018-04-01  20966119
102 2018-04-01  2027700
105 2018-04-01  5478000
104 2018-04-01  78448383
104 2018-04-06  49730093
101 2018-04-06  2750000
103 2018-04-15  16625000
105 2018-04-16  
106 2018-04-19  3095584

Output

login_time   median
2018-04-01   13222060
2018-04-06    26240047

My query

SELECT login_time, IF(SUM((IFNULL(bet, 0)) / 2) % 2 = 0, SUM(IFNULL(bet, 0)) 
    / 2 + 1, SUM(IFNULL(bet, 0)) / 2) 
 AS median
FROM activity
WHERE login_time BETWEEN '2018-04-01' AND '2018-04-06'
GROUP BY login_time;

Whay is wrong? Thanks!

Vanojx1
  • 5,574
  • 2
  • 23
  • 37

1 Answers1

0

There is a group_concat() trick for this, if your data is not too large. This works pretty well:

select a.login_time,
       substring_index(substring_index(group_concat(bet order by bet), ',', ceil(count(*) / 2), ',', -1)
from activity a
where a.login_time between '2018-04-05' and '2018-04-18'
group by a.login_time;

If there are an even number of examples, then this chooses the value on the lower side. Medians are not well defined for sets with an even cardinality.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • thanks, but: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'su bstring_index' – Елисей Горьков Sep 03 '18 at 14:28
  • this does not handle median complety..Correct median calculation wil use the AVG of two orderd middle rows when the items count is even..median wil pick the middle item from a order list when the item count is odd. This query does only handle the even count – Raymond Nijland Sep 03 '18 at 16:08