Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql : Get segmented records between multiple date range

Tags:

select

mysql

Table structure and sample data

CREATE TABLE IF NOT EXISTS `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `customer_id` int(11) NOT NULL,
  `restaurant_id` int(11) NOT NULL,
  `bill_id` int(11) NOT NULL,
  `source_id` int(1) NOT NULL,
  `order_medium_id` int(11) NOT NULL,
  `purchase_method` varchar(255) NOT NULL,
  `totalamount` int(11) NOT NULL,
  `delivery_charg` int(11) NOT NULL,
  `discount` int(11) NOT NULL,
  `vat` int(11) NOT NULL,
  `total_price` int(11) NOT NULL DEFAULT '0',
  `date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `customer_id` (`customer_id`),
  KEY `source_id` (`source_id`),
  KEY `restaurant_id` (`restaurant_id`),
  KEY `bill_id` (`bill_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;

--
-- Dumping data for table `orders`
--

INSERT INTO `orders` (`id`, `customer_id`, `restaurant_id`, `bill_id`, `source_id`, `order_medium_id`, `purchase_method`, `totalamount`, `delivery_charg`, `discount`, `vat`, `total_price`, `date_created`) VALUES
(1, 1, 1, 1, 1, 0, 'cash', 1600, 0, 0, 0, 1600, '2016-05-29 13:05:40'),
(2, 1, 1, 2, 2, 1, 'cash', 1820, 0, 0, 0, 1820, '2016-06-27 07:21:25'),
(4, 1, 1, 3, 3, 0, 'cash', 1770, 0, 0, 0, 1770, '2016-05-31 13:05:56'),
(5, 1, 1, 4, 2, 1, 'cash', 1300, 0, 0, 0, 1300, '2016-06-27 07:21:31'),
(6, 1, 1, 5, 1, 0, 'cash', 950, 0, 0, 0, 950, '2016-06-02 13:06:15'),
(7, 1, 1, 6, 1, 0, 'cash', 1640, 0, 0, 0, 1640, '2016-06-03 13:06:24'),
(8, 1, 1, 7, 2, 2, 'cash', 1600, 0, 0, 0, 1600, '2016-06-27 07:21:36'),
(9, 1, 1, 8, 2, 2, 'cash', 1575, 0, 0, 0, 1575, '2016-06-27 07:21:40'),
(10, 1, 1, 9, 3, 0, 'cash', 1125, 0, 0, 0, 1125, '2016-06-06 13:06:48'),
(11, 1, 1, 10, 2, 3, 'cash', 1920, 0, 0, 0, 1920, '2016-06-27 07:21:51');

Requirement :

I want to segment records as per customer as following. 
1. customers who ordered in last 2 week then give ratingflag 5
2. customers who ordered between 2 weeks to 4 week then give ratingflag 3
3. customers who ordered between 4 weeks to 8 week then give ratingflag 2
and so on.

Customer should be unique. We write a query for getting records according to requirement. I don't have so much data in my table, so i made change in condition according to my data.

I tried following . I would appreciate if you could help me with better approach of doing the same :

Query i tried

select customer_id,rating from (select `customer_id`,5 as rating from orders where `restaurant_id` = 1 and  (DATE(`date_created`) between DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND DATE(NOW())) GROUP BY customer_id
UNION ALL
select `customer_id`,4 as rating from orders where `restaurant_id` = 1 and  (DATE(`date_created`) BETWEEN DATE(DATE_SUB(NOW(), INTERVAL 4 DAY)) AND DATE(DATE_SUB(NOW(), INTERVAL 2 DAY))) GROUP BY customer_id
UNION ALL
select `customer_id`,3 as rating from orders where `restaurant_id` = 1 and  (DATE(`date_created`) BETWEEN DATE(DATE_SUB(NOW(), INTERVAL 8 DAY)) AND DATE(DATE_SUB(NOW(), INTERVAL 5 DAY))) GROUP BY customer_id
UNION ALL
select `customer_id`,2 as rating from orders where `restaurant_id` = 1 and  (DATE(`date_created`) BETWEEN DATE(DATE_SUB(NOW(), INTERVAL 20 DAY)) AND DATE(DATE_SUB(NOW(), INTERVAL 9 DAY))) GROUP BY customer_id
UNION ALL
select `customer_id`,1 as rating from orders where `restaurant_id` = 1 and  (DATE(`date_created`) BETWEEN DATE(DATE_SUB(NOW(), INTERVAL 40 DAY)) AND DATE(DATE_SUB(NOW(), INTERVAL 21 DAY))) GROUP BY customer_id) as temp group by customer_id order by rating desc;
like image 706
Navjot Singh Avatar asked Mar 18 '26 01:03

Navjot Singh


1 Answers

Hmmm, I think you just want the date of the most recent order and a big case statement. Something like this:

select o.customer_id,
       (case when max(date_created) >= date_sub(now(), interval 2 week) then 1
             when max(date_created) >= date_sub(now(), interval 4 week) then 2
             when max(date_created) >= date_sub(now(), interval 8 week) then 3
             . . .
        end) as rating
from orders o
group by o.customer_id;

Note that the case evaluates the conditions in order. This is why between is not necessary. The first matching condition ends the processing of the case.

like image 78
Gordon Linoff Avatar answered Mar 19 '26 15:03

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!