简体   繁体   中英

SQL query to get max value record in last 5 or 10 day

Suppose there is a table- Restaurant

id  |  order | date_time | bill(Rs.)
[1]  [Fish] [2018-06-17 17:06:28]  [2000]
[2]  [Meat] [2018-06-16 18:08:28]  [3000]
[3]  [Rice] [2018-05-22 21:09:28]  [4000]

Now I want the highest Bill Paid in last 5 day. What query should I use?

The answer should be 3000 not 4000.
Please help me out.

select * 
from Restaurant 
where bill = (select MAX(bill) from Restaurant 
where date_time BETWEEN DATE_SUB(NOW(),INTERVAL 5 DAY) AND NOW() );

assuming that you are calculating five days before current date

   SELECT MAX(bill) FROM restaurant_table 
   WHERE DATE_TIME  BETWEEN 
   DATE_SUB(NOW(), INTERVAL 5 DAY) 
   AND NOW();

Assuming that you want previous five days data from current date

 SELECT MAX(BIL) FROM resturant
  WHERE     DATE_TIME  
 BETWEEN  
 DATE_SUB(NOW(), INTERVAL 5 DAY) AND 
 NOW();
select id,order,date_time,max(sum(bill)) as paid_bill from restaurant
   where date_time between (NOW(), INTERVAL 5 DAY) and  NOW()
   group by order;

i hope it will work.

  • in where clause date_time need two date to filter 5 days data
  • then group all data and sum them
  • which one maximum this is your max bill paid order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM