Get the minimum amount in a week - php

Hi I have a property_rate table and values like
id start_date end_date rate
1 2017-11-19 2017-11-21 100
2 2017-11-22 2017-11-24 300
3 2017-11-25 2017-11-28 500
4 2017-11-29 2017-11-30 200
Here i want to get the row having lowest price in a week like
id start_date end_date rate
1 2017-11-19 2017-11-21 100
What i have tried the below query but it is returning null
SELECT MIN(rate_per_week) AS min_value FROM property_rate WHERE start_date >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 7 DAY)

Try this:
SELECT id, start_date, end_date, rate FROM <Table> ORDER BY rate ASC LIMIT <amount of rows to show>;

You have to place your table name.
here is the working code
SELECT * FROM `table name` ORDER BY `rate` ASC LIMIT 1

I have achieved it by below query
SELECT MIN(rate_per_week) AS min_value FROM property_rate WHERE start_date >= '2017-21-11' group by week('2017-21-11')

Related

SQL query for extract the highest value at a given time range?

I have a problem on a sql query. I need to take the last 30 days and then remove only the 4 results with the "voting" value greater
db structure
Id | time | voting
1 | unix time | 3
2 | unix time | 2
3 | unix time | 4
4 | unix time | 1
5 | unix time | 6
I would like to take me only the data of: 5-3-1-2
I have tried with
select a.*
from table a
inner join
( select votingng, max(time) as latest from table group by voting) v
on a.time = v.latest
and a.voting = v.voting
order by time desc limit
It sounds like you are trying to get the top 4 voting results in the past 30 days. Does either of these give you what you want?
SELECT a.*
FROM table a
WHERE a.time > UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY))
ORDER BY a.voting DESC
LIMIT 4;
or
SELECT a.*
FROM table a
WHERE DATEDIFF( NOW(), FROM_UNIXTIME(a.time) ) <= 30
ORDER BY a.voting DESC
LIMIT 4;
I think this is what you want:
select v.*
from voting v
where timestamp >= unix_timestamp(date_sub(curdate(), interval 1 month)
order by voting desc
limit 4;
Hope this is what you are looking for:
Select *
From
Voting
Where
time Between CURDATE() And DATE_SUB(CURDATE(),INTERVAL 30 DAY)
Order By voting Desc
Limit 4
Try this if you are using teradata
SELECT * from table
qualify row_number () over(order by time desc)=1 ;
or
select * from
(select table.*, row_number () over(order by time desc) as RANK from table)
where RANK=1

Mysql SUM amount Between Month And Year

I need report of products amount, between month and years...
am use this query but fetch amount is 0
SELECT SUM(amount) as amt
FROM `products`
WHERE `cid`='14'
AND `sid`='24'
AND MONTH(`date`)='03'
AND YEAR(`date` BETWEEN '2014' AND '2016')
Try this: (will bring data from 03/2014, 03/2015, 03/2016)
SELECT SUM(amount) as amt
FROM `products`
WHERE `cid`='14'
AND `sid`='24'
AND MONTH(`date`)='03'
AND YEAR(`date`) BETWEEN '2014' AND '2016'
If you want to bring all data between these two dates, try this:
SELECT SUM(amount) as amt
FROM `products`
WHERE `cid`='14'
AND `sid`='24'
AND `date` >= '2014-03-01 00:00:00' AND `date` < '2016-04-01 00:00:00'
Try this Query:-[Tested myself]
Table Data-
id cid sid amount date
1 14 24 200 2014-03-09
2 14 24 30 2014-03-09
3 14 24 50 2015-05-08
Query:-
SELECT SUM(amount) as amt FROM products WHERE cid='14' AND sid='24' AND MONTH(date)='3' AND YEAR(date) BETWEEN '2014' AND '2016'
Output:-
amt
230
Note:- Make sure your Date column's datatype is DATE

Trying to find out subscription end month notification

Hi i have this mysql table
id amount substart years subend
1 200 2012-01-10 1 2013-01-09
2 250 2012-02-15 2 2014-02-14
3 100 2012-02-11 1 2013-02-10
4 260 2012-03-22 3 2015-03-21
What i want is that to give notification a month before the end date. The current query is:
select count(subend) as count,curdate()
from subdur where status='active'
and (date_sub(subend,interval 1 month))>=curdate()
and (date_sub(subend,interval 1 month))<date_add(curdate(),interval 1 month)
order by subend;
The query is not giving me proper answer.
Thanks in advance
Try this::
select
count(subend) as count,
curdate()
from subdur
where status='active' and
subend BETWEEN (date_sub(curdate(),interval 1 month)) and curdate()
order by subend
Another way would be using date_diff:
select count(subend) as count, curdate()
from subdur
where status='active'
and date_diff(subend, curdate()) = 30 // >= 30 days or more, = 30 days exact
order by subend
;

First and last records from group by. Cannot get last

I have a table
Date value1 value2
2012-09-07 1 1
2012-09-06 2 2
2012-09-05 3 3
2012-09-04 4 4
2012-09-03 5 5
2012-08-31 6 6
2012-08-30 7 7
2012-08-29 8 8
2012-08-28 9 9
2012-08-27 10 10
2012-08-24 11 11
2012-08-23 12 12
2012-08-22 13 13
values in the table is not ascending like in example. There are random numbers.
I need to get the date of the week start, value1 on the beginning of the week and value2 at the end of the week.
Date field is unique, and it's stores day dates only so no duplicate dates are allowed.
I tried to use the query below:
SELECT MIN(`Date`) as Date,
(SELECT `value1` ORDER BY `Date` ASC LIMIT 1) as Start,
(SELECT `value2` ORDER BY `Date` DESC LIMIT 1) as End
FROM table
GROUP BY YEAR(`Date`), WEEK(`Date`,7)
The query returns grouped weeks and value1 correctly but value2 is also from the row of the week start i.e.
2012-08-27 10 10
but I need:
2012-08-27 10 6
What do I do wrong?
How about something like this
SELECT `date`, value1 as Start,
(SELECT value2 FROM photos WHERE t.date >= adddate(`Date`, INTERVAL 1-DAYOFWEEK(`Date`) DAY) AND t.date <= adddate(`Date`, INTERVAL 7-DAYOFWEEK(`Date`) DAY) ORDER BY date DESC LIMIT 1) as endDate
from table t
GROUP BY YEAR(`Date`), WEEK(`Date`,7)
There may be a more optimal way to do it.. but this works

calculate price between given dates

Hotel_id Room_id Room_type Start_date End_date Price
----------------------------------------------------------------
13 2 standard 2012-08-01 2012-08-15 7000
13 2 standard 2012-08-16 2012-08-31 7500
13 2 standard 2012-09-01 2012-09-30 6000
13 3 luxury 2012-08-01 2012-08-15 9000
13 3 luxury 2012-08-16 2012-08-31 10000
13 3 luxury 2012-09-01 2012-09-30 9500
Hi this is the structure and data of my table.
I need to create a mysql query for hotel booking, that would match in database user entered data:
Date when they want to checkin and checkout
Room type
For Ex:
If user selects Hotel with luxury room based on these dates (2012-08-30 to 2012-09-04)
the total cost would be (10000*2) for 30th and 31st Aug + (9500*3) for 1st,2nd and 3rd Sep(4th checkout day don't include)
that means total price will be 20000+28500=48500
So query should filter total price based on the Hotel_id,Room_id,Start_date,End_date and Price
Thanks
Use this solution:
SELECT SUM(
CASE WHEN a.Start_date = b.min_sd AND a.Start_date <> b.max_sd THEN
(DATEDIFF(a.End_date, '2012-08-30')+1) * a.Price
WHEN a.Start_date = b.max_sd AND a.Start_date <> b.min_sd THEN
DATEDIFF('2012-09-04', a.Start_date) * a.Price
WHEN (a.Start_date,a.Start_date) IN ((b.min_sd,b.max_sd)) THEN
(DATEDIFF('2012-09-04', '2012-08-30')+1) * a.Price
WHEN a.Start_date NOT IN (b.min_sd, b.max_sd) THEN
(DATEDIFF(a.End_date, a.Start_date)+1) * a.Price
END
) AS totalprice
FROM rooms a
CROSS JOIN (
SELECT MIN(Start_date) AS min_sd,
MAX(Start_date) AS max_sd
FROM rooms
WHERE Room_type = 'luxury' AND
End_date >= '2012-08-30' AND
Start_date <= '2012-09-04'
) b
WHERE a.Room_type = 'luxury' AND
a.End_date >= '2012-08-30' AND
a.Start_date <= '2012-09-04'
Replace occurances of 2012-08-30 and 2012-09-04 with your input start and end dates respectively.
This will account for start and end dates being in the same month as well as spanning across multiple months.
SQLFiddle Demo
You can use MySQL's BETWEEN ... AND ...
operator to find the date ranges in which the desired booking falls (remember to take one day off of the given checkout
date as, like you say, there is no night's stay), then group the results by room and take the
SUM() of price times number of nights (which can
be calculated using MySQL's LEAST() and
GREATEST() functions):
SELECT Room_id,
SUM(Price * (1 + DATEDIFF(
LEAST(End_date, '2012-09-04' - INTERVAL 1 DAY),
GREATEST(Start_date, '2012-08-30')
))) AS Total
FROM mytable
WHERE Room_type = 'luxury' AND (
'2012-09-04' - INTERVAL 1 DAY
BETWEEN Start_date AND End_date
OR '2012-08-30' BETWEEN Start_date AND End_date
)
GROUP BY Room_id
See it on sqlfidde.
try this:
set #Hotel_id :=13;
set #Room_id :=3;
set #Start_date :='2012-08-30' ;
set #End_date :='2012-09-04';
select sum(b.TotalPrice-b.deductions) as total_cost from
( select a.Price,a.StartDate,a.EndDate,price*(DATEDIFF(a.EndDate,a.StartDate)+1) as TotalPrice
,case when a.EndDate=#End_date then a.Price else 0 end as deductions
from
(select price,case when #Start_date>=Start_date then #Start_date else Start_date end as StartDate
,case when #End_date<=End_date then #End_date else End_date end as EndDate
from h_booking h1
where Hotel_id=#Hotel_id
and Room_id=#Room_id
and (#Start_date between Start_date and End_date or #End_date between Start_date and End_date ))a )b

Categories