0

I have a location table now I have to keep only 100 record for each customer and have to move rest of them to another server.

using this query We can get customers list

SELECT user_id FROM user_location_history WHERE (TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE()) < 30) GROUP BY user_id HAVING COUNT(1) < 100

Now suppose we have a list of customer like

I am trying to write a single query to get all the record for each customer which is greater then 100.

  • 125452
  • 412555
  • 554114
  • 258471

Please suggest ....

Jobins John
  • 1,265
  • 23
  • 45

2 Answers2

0

This will get the latest 100 records of each customer

SELECT   user_id, substring_index(group_concat(sample_id ORDER BY sample_id DESC SEPARATOR ','), ',', 100) limit100, FROM     user_location_history  GROUP BY user_id ;
Gopalakrishnan
  • 957
  • 8
  • 19
0

Try this query it generates a row number for each record :-

set @num := 0, @group := '';
select *
from 
(
   select user_id,
      @num := if(@group = `user_id`, @num + 1, 1) as row_number,
      @group := `user_id` as dummy
  from user_location_history
  WHERE (TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE()) < 30)
) as x 
where x.row_number <= 100;
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20