Split date range and get combined results in mysql - php

I have two tables
orders_status_history
id | order_id | status | date
1 | 1201 | 2 | 2015-01-20
2 | 1124 | 4 | 2015-02-01
3 | 1245 | 1 | 2015-02-14
4 | 1365 | 2 | 2015-03-10
saved_shipping_invoices
id | order_id | product_id | date
1 | 1348 | 12541 | 2015-12-18
2 | 1298 | 11485 | 2016-01-02
3 | 1319 | 14521 | 2016-05-14
4 | 1441 | 10124 | 2016-05-14
and one year date range i.e.
$start_date = '2015-09-30';
$end_date = '2016-09-30';
Data in table "shipping_invoice_history" started from 2015-12-18. Now I want to break date interval on 2015-12-18. There will be two intervals now
$interval1_start = '2015-09-30';
$interval_end = '2015-12-17';
$interval2_start = '2015-12-18';
$interval2_end = '2016-09-30';
When it breaks the date interval, I want to get data from table "orders_status_history" on the first interval but on the second interval it gets data from "shipping_invoice_history" .Also I need to show the data in single table.
$week = '2015-09-30 ## 2016-09-30';
$week_ranges = explode('##', $week);
if ($week_ranges[0] > '2015-12-18' )
{
$invoices_shipped_qty = tep_db_query("SELECT * FROM `saved_shipping_invoices` WHERE `date_created` BETWEEN '".$week_ranges[0]."' AND '".$week_ranges[1]."'");
$invoices_shipped_arr = tep_db_fetch_array($invoices_shipped_qty);
$total_sent = $invoices_shipped_arr['shiped_qty'];
}
else {
$total_order_status_complete = tep_db_query("SELECT * FROM `orders_status_history` WHERE `orders_status_id` = '17' AND `date_added` BETWEEN '".$week_ranges[0]." 00:00:00' AND '".$week_ranges[1]." 23;59:59'");
$total_order_status_complete_arr = tep_db_fetch_array($total_order_status_complete);
Please suggest me how can i break date interval into two, get result from two tables and display in single run.

Probably this is what you want
select
'order_status_history' as table,
id,
order_id,
status,
null as product_id,
date from order_status_history
where
date between '2015-09-30' and '2015-12-17'
union all
select
'saved_shipping_invoices' as table,
id,
order_id,
null as status,
product_id,
date
from saved_shipping_invoices
where
date between '2015-12-18' and '2016-09-30'

Depending on how dynamic is your query, I like to do something like this instead of union.
I prefer this option:
select
if(date <='2015-12-17', 'Phase 1',
if(date >='2015-12-18', 'Phase 2',
'None')),
id,
order_id,
status,
null as product_id,
date from order_status_history
where
date between '2015-09-30' and '2016-09-30'
or
select
if(date between '2015-09-30' and '2015-12-17', 'Phase 1',
if(date between '2015-12-18' and '2016-09-30', 'Phase 2',
'None')),
id,
order_id,
status,
null as product_id,
date from order_status_history
where
date between '2015-09-30' and '2016-09-30'

Related

MySQL select the create date's display number

I have a columns that has the time stamp when a row is created.
I need to find a way to get all the rows from the same 'day', and give each row a display number to show something like this:
+---------------------+
| order_date | // output should be:
+---------------------+
| 2016-05-11 13:01:41 | // 2016051101
| 2016-05-11 15:54:14 | // 2016051102
| 2016-05-11 17:16:18 | // 2016051103
| 2016-05-11 17:35:12 | // 2016051104
| 2016-05-11 17:55:16 | // 2016051105
| 2016-05-12 07:16:48 | // 2016051201
| 2016-05-12 09:30:57 | // 2016051202
| 2016-05-12 09:56:19 | // 2016051203
| 2016-05-13 11:27:21 | // 2016051301
| 2016-05-14 10:15:56 | // 2016051401
| 2016-05-15 11:35:07 | // 2016051501
| 2016-05-15 11:39:48 | // 2016051502
| 2016-05-15 11:40:12 | // 2016051503
+---------------------+
Can Mysql do this? or should the program, like php, handle this?
Final query I use:
SELECT t.order_date,
concat(
DATE_FORMAT(t.order_date, '%Y%m%d'),
LPAD(
(SELECT COUNT(*) FROM hz_order s WHERE s.order_date<= t.order_date AND DATE(s.order_date) = DATE(t.order_date)),
2,
0))display
FROM hz_order t
ORDER BY `display` ASC
Yes, you can do it with a correlated query, like this:
SELECT t.order_date,
concat(DATE_FORMAT(t.order_date, '%Y%m%d'),
(SELECT COUNT(*) FROM YourTable s
WHERE s.order_date<= t.order_date
AND DATE(s.order_date) = DATE(t.order_date)))
FROM YourTable t
This will generate the date as yyyymmdd+the count.
Yes, You can do this by -
SELECT CONCAT(DATE_FORMAT(date_field, '%Y%m%d'),
LPAD(COUNT(date_field), 2, '0'))
FROM table_name
GROUP BY DATE(date_field)
ORDER BY date_field DESC
DATE_FORMAT(date_field, '%Y%m%d') would format the date like yyyymmdd.
LPAD(COUNT(date_field), 2, '0') would generate a string with left padding (with 0) to 2 places with the count of that date.
Here's one way using MySQL. Other methods will be faster on larger data sets.
SELECT x.*
, CONCAT(DATE_FORMAT(x.order_date,'%Y%m%d'),LPAD(COUNT(*),2,0))oi
FROM my_table x
JOIN my_table y
ON DATE(y.order_date) = DATE(x.order_date)
AND y.order_date <= x.order_date
GROUP
BY x.order_date;

Unable to fetch the accurate rooms availability in specific date range

Here is my database structure
Table name :set_inventory, and columns are below
inventory_id | room_id | quantity_start_date | quantity_end_date | total_rooms
1 | 2 | 2015-10-10 | 2015-10-12 | 5
2 | 2 | 2015-10-13 | 2015-10-14 | 10
3 | 2 | 2015-10-15 | 2015-10-17 | 0
Another Table
Table name : rooms, amd columns are
room_id | room_type | room_picture | room_description
2 | standard | pic_link | demo description
Description: In inventory table admin able to set the inventory based on multiple date ranges.
My query:
SELECT rooms.room_id, rooms.room_pic1, rooms.room_type, rooms.maximum_adults,
rooms.maximum_children, rooms.room_amenities,set_inventory.room_id,
set_inventory.quantity_start_date, set_inventory.quantity_start_date,
set_inventory.total_rooms
from rooms, set_inventory
WHERE rooms.room_id = set_inventory.room_id
AND quantity_start_date <= '2015-10-11'
AND quantity_end_date > '2015-10-13'
It shows me the inventory only 5, based in above query,
What i am looking for :actually, i am looking for the result,
for example my check_in_date "2015-10-11" and check_out_date is "2015-10-17"
In this i will be able to notify the customer that
"room is only available for 4 days,
Plz help me , thanks
UPDATED
Try this WHERE condition:
WHERE rooms.room_id = set_inventory.room_id AND DATE(quantity_start_date) <= '2015-10-11' AND DATE(quantity_end_date) > '2015-10-13';
So, minding you want to have the amount of days between these to dates, your query should contain somwthing like this:
SELECT ABS(DATEDIFF(a1.start_date, a2.end_date)) FROM (
SELECT quantity_start_date as start_date FROM set_inventory WHERE quantity_start_date <= '2015-10-11' ORDER BY start_date DESC LIMIT 1) a1, (
SELECT quantity_end_date as end_date FROM set_inventory WHERE quantity_end_date > '2015-10-13' ORDER BY end_date ASC LIMIT 1) a2;
If you try it, you'll get 4 in result.

How to get the latest data of a specific day? [MYSQL]

How can I get the latest data of a specific day in MySQL?
Let's assume that I have a column of dates recorded on my database
dates | time | value
---------------------------------------------
2015-08-05 | 11:03:02 | 200
2015-08-05 | 23:04:22 | 2400
2015-08-07 | 8:00:22 | 500
2015-08-08 | 13:00:11 | 400
2015-08-08 | 13:23:11 | 200
2015-08-09 | 17:00:23 | 2200
2015-08-09 | 17:06:00 | 1290
2015-08-09 | 19:22:00 | 900
2015-08-13 | 01:01:22 | 1010
I want to get the latest data or transaction of a specific date, my desired result would be like this
dates | time | value
---------------------------------------------
2015-08-05 | 23:04:22 | 2400
2015-08-07 | 8:00:22 | 500
2015-08-08 | 13:23:11 | 200
2015-08-09 | 19:22:00 | 900
2015-08-13 | 01:01:22 | 1010
Only the latest data of a spefic or distinct date is chosen, what is the possible query with this?
You need to do this way to get that for each dates
select t1.dates,t1.time,t1.value from table as t1 inner join
(
select dates,max(time) as time from table group by dates
) as t2 on t1.dates=t2.dates and t1.time=t2.time
Use
SELECT * FROM TABLE_NAME GROUP BY dates ORDER BY time desc LIMIT 1;
Or
SELECT * FROM TABLE_NAME GROUP BY dates HAVING time <= '23:59:59' LIMIT 1;
Try this:
SELECT `dates`, MAX(`time`) AS `time`, MAX(`value`) AS `value`
FROM `tbl_name`
GROUP BY `dates`
ORDER BY `dates` ASC
Try this query:
SELECT * FROM `table` GROUP BY dates ORDER BY time DESC;

MySQL get the sum of all rows without retrieving all of them

This may be a little confusing but please bear with me. Here's the thing:
I have a database that contains ~1000 records, as the following table illustrates:
+------+----------+----------+
| id | date | amount |
+------+----------+----------+
| 0001 | 14/01/15 | 100 |
+------+----------+----------+
| 0002 | 14/02/04 | 358 |
+------+----------+----------+
| 0003 | 14/05/08 | 1125 |
+------+----------+----------+
What I want to do is this:
Retrieve all the records beginning at 2014 and until yesterday:
WHERE `date` > '14-01-01' AND `date` < CURDATE()
But also get the sum of amount up to the current date, this is:
WHERE `date` < CURDATE()
I've already got this working by just selecting all the records based on the second condition, getting the sum, and then excluding those which don't match the first condition. Something like this:
SELECT `id`, `date`, `amount` FROM `table`
WHERE `date` < CURDATE()
And then:
$rows = fetchAll($PDOStatement);
foreach($rows as $row) {
$sum += $row->amount;
if (
strtotime($row->date) > strtotime('14-01-01') &&
strtotime($row->date) < strtotime(date('Y-m-d'))
) {
$valid_rows[] = $row;
}
}
unset $rows;
Is there a way to achieve this in a single query, efficiently? Would a transaction be more efficient than sorting out the records in PHP? This has to be SQL-standard compliant (I'll be doing this on MySQL and SQLite).
Update:
It doesn't matter if the result ends up being something like this:
+------+----------+----------+-----+
| id | date | amount | sum |
+------+----------+----------+-----+
| 0001 | 14/01/15 | 100 | 458 |
+------+----------+----------+-----+
| 0002 | 14/02/04 | 358 | 458 |
+------+----------+----------+-----+
| 0003 | 14/05/08 | 1125 | 458 |
+------+----------+----------+-----+
The worst case would be when the resulting set ends up being the same as the set that gives the sum (in this case appending the sum would be irrelevant and would cause an overhead), but for any other regular cases the bandwith save would be huge.
You can create a special record with your sum and add it at the end of your first query
SELECT * FROM `table` WHERE `date` > '14-01-01' AND `date` < CURDATE()
UNION
SELECT 9999, CURDATE(), SUM(`amount`) FROM `table` WHERE `date` < CURDATE()
Then you will have all your desired record and the record with id 9999 or whatever is your sum
This could be achieved by correlated subquery, something like below:
SELECT *, (SELECT SUM(amount) FROM t WHERE t.date < t1.date) AS PrevAmount
FROM t AS t1
WHERE `date` > '14-01-01' AND `date` < CURDATE()
However it is very unefficient if the number of records is large.
It's hackish, but:
> select * from foo;
+------+------+
| id | val |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+------+------+
5 rows in set (0.02 sec)
> select * from foo
left join (
select sum(val)
from foo
where id < 3
) AS bar ON 1=1
where id < 4;
+------+------+----------+
| id | val | sum(val) |
+------+------+----------+
| 1 | 1 | 3 |
| 2 | 2 | 3 |
| 3 | 3 | 3 |
+------+------+----------+
Basically, do your summing in a joined subquery. That'll attach the sum result to every row in the outer table's results. You'll waste a bit of bandwidth sending that duplicated value out with every row, but it does get you the results in a "single" query.
EDIT:
You can get the SUM using a LEFT OUTER JOIN.
SELECT t1.`id`, t1.`date`, t2.sum_amount
FROM
`table` t1
LEFT OUTER JOIN
(
SELECT SUM(`amount`) sum_amount
FROM `table`
WHERE `date` < CURDATE()
) t2
ON 1 = 1
WHERE t1.`date` > STR_TO_DATE('01,1,2014','%d,%m,%Y') AND t1.`date` < CURDATE();
This will do what you want it to do...optimizing the subquery is the real challenge:
SELECT id,date,amount,(SELECT SUM(amount) FROM table) AS total_amount
FROM table
WHERE date BETWEEN '14-01-01' AND DATE_ADD(CURDATE(), INTERVAL -1 DAY)

Calculate price between different dates

Hi everyone I'm new and I need help about my booking hotel, I've this table:
id, idhotel, room, data_start, data_end, price
Now I need to calculate the total price when samebody search the rooms between many days
I'm trying this query but the result is 0.
$query = "SELECT room, SUM(price) FROM price WHERE idhotel='".$_GET['id']."' BETWEEN 'data_start' AND 'data_end'
ORDER BY room";
Can someone one help me?
If I understand correctly you are probably looking for something like this
$date_start = $_GET['date_start'];
$date_end = $_GET['date_end'];
$hotel_id = $_GET['id'];
$query = "SELECT room,
SUM(price) total
FROM tablename
WHERE data_start >= '$date_start'
AND date_end <= '$date_end'
AND idhotel = $hotel_id
GROUP BY room";
I think you need something like this:
SELECT idhotel, room, price, (DATEDIFF(data_end, data_start) * price) as total FROM rooms
For testing create next table and fill itselect * :
create table rooms (id int not null primary key auto_increment, idhotel int, room int, data_start date, data_end date, price int);
+----+---------+------+------------+------------+-------+
| id | idhotel | room | data_start | data_end | price |
+----+---------+------+------------+------------+-------+
| 1 | 1 | 1 | 2013-05-09 | 2013-05-12 | 100 |
| 2 | 1 | 1 | 2013-05-20 | 2013-05-20 | 100 |
+----+---------+------+------------+------------+-------+
Use next query for get total cost (modifed for prevent losing one day):
SELECT idhotel, room, price, ((DATEDIFF(data_end, data_start) + 1) * price) as total FROM rooms;
Result:
+---------+------+-------+-------+
| idhotel | room | price | total |
+---------+------+-------+-------+
| 1 | 1 | 100 | 400 |
| 1 | 1 | 100 | 100 |
+---------+------+-------+-------+
Extend
If you want get room total sum for period, try use this code:
SELECT
idhotel, room, SUM(total) as total_sum
FROM
(
SELECT
idhotel,
room,
price,
(DATEDIFF(data_end, data_start) * price) as total
FROM
rooms
) as t
GROUP BY
idhotel, room;
Result:
+---------+------+-----------+
| idhotel | room | total_sum |
+---------+------+-----------+
| 1 | 1 | 150 |
+---------+------+-----------+

Categories