I need to make a query that selects from two different tables. Basically, I only want to select the rows from the dates table that have no pending orders in the orders table.
For example, the dates table has the values of July 1, July 2 and July 3. July 2 has orders with the status = PEN in the orders table so the table will only show July 1 and July 3.
Query 1 for dates:
$sql = "SELECT * FROM dates WHERE DATEDIFF(CURDATE(), date) >= 30 AND `30day`='No'";
I have yet to build a query for the orders table as I am sure this needs to be integrated into one query together, and I am not sure what to do.
I know you can do two SELECT queries in one, and I am aware of how to do this, but I am unsure of how to cause the second SELECT query to be affected by the first SELECT query.
dates database has columns id date closed 30day 60day
orders database has columns id date order status
I need this query to flag any orders with the statuses PEN BO FBO.
Thank you in advance!
Sample Data:
dates table:
Date - 30-day Value
July 1 - No
July 2 - No
July 3 - No
orders table:
Date - Orders - Status
July 1 - 7123456 - PEN
July 1 - 7123457 - SHI
July 1 - 7123487 - SHI
July 2 - 7256789 - SHI
July 2 - 7256790 - SHI
July 2 - 7256791 - SHI
July 3 - 7215368 - SHI
July 3 - 7125369 - SHI
July 3 - 7659876 - BO
July 4 - 7569235 - FBO
July 4 - 7986585 - FBO
Expected Result:
Date
July 2
July 3
Omitted Dates:
Date - Reason
July 1 - because there is an open order
July 4 - because there is an open order
I don't want an omitted table - just wanted to show what wouldn't show up.
could be this what you are looking for (i don't know your schema so for the join i have used a column named key
$sql = "SELECT *
FROM dates
LEFT JOIN orders on (dates.date = orders.date and orders.status not in ('PEN','BO', 'FBO'))
WHERE DATEDIFF(CURDATE(), dates.date) >= 30
AND `30day`='No'";
otherwise if you need that status is not PEN, BO and FBO then you can
..
$sql = "SELECT *
FROM dates
LEFT JOIN orders ON dates.date = orders.date
WHERE DATEDIFF(CURDATE(), dates.date) >= 30
AND `30day`='No'
AND orders.status NOT IN ('PEN','BO', 'FBO' ) ";
..
$sql = "SELECT *
FROM dates
INNER JOIN orders ON dates.date = orders.date
WHERE DATEDIFF(CURDATE(), dates.date) >= 30
AND `30day`='No'
AND orders.status IN ('PEN','BO', 'FBO' ) ";
based on the sample provided this should return the rows you need
$sql = " SELECT *
FROM dates
WHERE DATEDIFF(CURDATE(), date) >= 30
AND `30day`='No'
AND date not in (
select date
from orders
where order.status IN ('PEN','BO', 'FBO' )
) ";
Related
I am trying to get a COUNT of the last 12 months of appointments grouped by month for output into a chart. The following works fine but I need it to return 0 if no results for each month.
$query = "SELECT COUNT(id) as total_month FROM appointments WHERE created >= DATE(NOW()) - INTERVAL 365 DAY GROUP BY Month(created)";
$query = $mysqli->real_escape_string($query);
if($result = $mysqli->query($query)){
while($row = $result->fetch_array())
{
$month_total_appointments .= $row['total_month'].',';
}
}
echo $month_total_appointments;
================================================================
Simple table structure and example for appointments Table
id customer_name created
1 John 2020-05-01 08:00:00 <= stored as datetime
2 Mike 2020-04-01 09:00:00
3 Steve 2020-02-01 10:00:00
Output would be 0,0,0,0,0,0,0,0,1,0,1,1
======================================================
Current output is: 1,1,1
I've read some use a month table and LEFT JOIN but everything i've tried doesn't seem to work. Can anyone help please?
You won't get zeroes for rows that aren't there. Grouping combines rows that match particular criteria, but it can't fabricate them out of nothing.
That's why it's typical to include the grouping criteria in the results:
SELECT COUNT(id), MONTH(created) AS created_month
FROM appointments
WHERE created >= DATE(NOW()) - INTERVAL 365 DAY
GROUP BY created_month
Then you can expand that in your application code to fill in the missing values. The alternative is you need a fully populated list of all possible dates to JOIN against.
Keep in mind the MONTH() thing will wrap around and group January 2020 with January 2021. You may want to split this up:
SELECT COUNT(id), YEAR(created) AS created_year, MONTH(created) AS created_month
FROM appointments
WHERE created >= DATE(NOW()) - INTERVAL 365 DAY
GROUP BY created_year, created_month
i have this query that get all regsiterd data between this two dates
SELECT date as date_month, status FROM tbl_reports WHERE status != 0 AND (date BETWEEN '2017-01-01' AND '2017-12-31') GROUP BY MONTH(date)
result
date_month
2017-04-19
2017-05-24
2017-06-26
2017-07-01
but how i can get the result even if there is no regsitered input in the other months
want result
Date_month
2017-01-01
2017-02-01
2017-03-01 - it will give this value if there is no such data in this month
2017-04-19 - i will get the input date from database
'' '' ''
2017-12-31
is it possible ? or i need some php code to manipulate this data?
thank you in advance for my answering my question.. :)
Create an extra table, named datehelper. Provide it with 2 columns: year and month. And fill those with year: 2017 until 2027 and 1 / 12 for the months.
SELECT
datehelper.year,
datehelper.month,
r.status
FROM datehelper
LEFT JOIN tbl_reports r ON MONTH(r.date) = datehelper.month
AND YEAR(r.date) = datehelper.year AND status != 0
WHERE (datehelper.year = '2017')
ORDER BY datehelper.year, datehelper.month
I see you would get duplicates on the left joined part if there are more records there in a certain month.
What status would you expect? Let's think there are records with status 0 (excluded) status = 1, and status = 2
datehelper:
year month
2017 1
2017 2
2017 3
2017 4
...
2017 11
2017 12
2018 1
...
Need to insert into the table public_holidays, by the given date that matches holidays table date and it should match CLOCK table clock_in and clock_out time fields and fetch time difference in seconds for 28800 = 8hrs and also need to match designation table from users table user_id, sorry for a very messy code, greatly confused any help could be very helpful thank you.
Clock Table
id user_id date clock_in clock_out created_at updated_at
6 12 2016-06-10 2016-06-10 06:00:00 2016-06-10 14:00:00 2016-06-10 19:20:41 2016-06-10 00:15:51
Users
id first_name last_name designation_id email
1 crysis name 1 crysis#gmail.com
holidays
id date holiday_description created_at updated_at
1 2016-06-10 christmas 2016-06-11 15:23:54 2016-06-11 15:23:54
Designation
designation_id department_id designation created_at updated_at
1 1 employee 2016-01-30 21:03:24 2016-01-30 22:03:24
Department
department_id department_name department_description created_at updated_at
1 IT Info tech 2016-01-30 21:03:24 2016-01-30 21:03:24
Need to insert into this table Public_holidays in the below format by calculating time 8hrs from clock table
user_id department_id designation_id date_cur clock_in clock_out created_at updated_at
12 1 1 2016-06-13 2016-06-10 06:00:00 2016-06-10 14:00:00 2016-06-13 21:03:24 2016-06-13 21:03:24
Tried Messy code
INSERT INTO public_holidays (user_id, department_id,designation_id,clock_in,clock_out) SELECT(cl.user_id,
di.department_id,
di.designation_id,
cl.clock_in,
cl.clock_out)
FROM clock cl, designations di
where
(
select h1.date AS ph_date from holidays h1 WHERE ph_date = 2015-01-22
AND
select (TIMESTAMPDIFF(second,cl.clock_in, cl.clock_out)=28800) AS differ1
AND
INNER JOIN users AS ui ON ui.designation_id = di.id
);
I still don't get the idea, but at least see some relations. Query below will return dataset for all emplyees and all holidays for each of them (h.date column should go into cur_date table). The last JOIN has no relation thus it creates cartesian product - attaches each holiday for each already constructed row (total rows = nuber of empl x number of holidays)
SELECT cl.user_id, di.department_id, di.designation_id, h.date, cl.clock_in, cl.clock_out
FROM clock cl
INNER JOIN users u ON cl.user_id = u.user_id
INNER JOIN designation di ON u.designation_id = u.user_id
CROSS JOIN holidays h --cartesian product: all dates for each row
Don't know what clock table is - standard working hours (one row per employee - I assumed that this is true), daily timesheet (one row per date per employee) or sth else? If its standard working hours then why datetime and not time? If its daily timesheet how should I decide which 8 hrs from that table should you choose (and why 8 hrs)?
Dont know why you calculated/searched for these 8 hrs - do you want to insert holidays for empl. that work 8 hrs only (according to "clock"/in any day)
Don't know what cur_date is (assumed the holiday), but why use date next to datetime - shouldn't be either time or date or datetime only?
Finally this one works #shudder thanks for the help
INSERT INTO public_holidays (user_id, department_id,designation_id,date_cur,clock_in,clock_out)
SELECT cl.user_id, des.department_id , us.designation_id, cl.date,cl.clock_in, cl.clock_out
FROM clock cl
INNER JOIN holidays AS hol ON hol.date = cl.date
INNER JOIN users AS us ON cl.user_id = us.id
INNER JOIN designations AS des ON des.id = us.designation_id
WHERE date(cl.created_at) = '2016-06-13'
AND TIMESTAMPDIFF(second,cl.clock_in, cl.clock_out) = 28800;
I have 2 tables,
prices (pcode, date, priceperweek)
1 record covers 1 week
booked (pcode, date)
1 record = 1 day, because shortbreaks are available
And a form consists of 3 fields
startdate
flexibility (+/- 1/2/3 weeks)
duration (3 / 4 / 5 / 6 / 7 / 14)).
The below query should be selecting prices & dates and checking to see if the selected start date appears in the prices table and that each day from the startdate doesn't appear in the bookings table.
SELECT SUM(priceperweek) AS `ppw`, prices.date AS `startdate`
FROM `prices` LEFT JOIN `booked` ON prices.pcode=booked.pcode
WHERE prices.pcode='A2CD59GH'
AND (prices.date IN ('20131221', '20131228')
AND booked.date NOT IN ('20131221', '20131222', '20131223',
'20131224', '20131225', '20131226', '20131227', '20131228',
'20131229', '20131230', '20131231', '20140101', '20140102',
'20140103')
)
OR (prices.date IN ('20131214', '20131221')
AND booked.date NOT IN ('20131214', '20131215', '20131216',
'20131217',
'20131218', '20131219', '20131220', '20131221', '20131222',
'20131223', '20131224', '20131225', '20131226', '20131227')
)
OR (prices.date IN ('20131228', '20140104') AND booked.date NOT IN
('20131228', '20131229', '20131230', '20131231', '20140101',
'20140102', '20140103', '20140104', '20140105', '20140106',
'20140107', '20140108', '20140109', '20140110')
)
GROUP BY prices.date
ORDER BY prices.date ASC
VALUES GIVEN TO QUERY...
startdate = 20131221
duration = 14
property = A2CD59GH
plusminus = 1
My problem is that this query returns records even if some of the dates in a range appear in the "bookings" table AND the ppw value is alot more than i would have expected.
The reason for using SUM(ppw) is when a duration of 14 is specified the price will sum both weeks together.
Thanks for any help on this
The problem with your approach is that the startdate will only be filtered from the results if every single record in the booked table for the given pcode falls within the booking period. Obviously this won't be the case if the property has been booked on some other date.
I'd suggest performing an anti-join along the following lines:
SELECT t.date, SUM(prices.priceperweek) FROM prices JOIN (
SELECT prices.date
FROM prices LEFT JOIN booked
ON booked.pcode = prices.pcode
AND booked.date BETWEEN prices.date
AND prices.date + INTERVAL 14 DAY
WHERE booked.pcode IS NULL
AND prices.pcode = 'A2CD59GH'
AND prices.date BETWEEN '20131221' - INTERVAL 1 WEEK
AND '20131221' + INTERVAL 2 WEEK
) t ON prices.date BETWEEN t.date AND t.date + INTERVAL 13 DAY
GROUP BY t.date
Can anyone post a SQL query for Calculating Total No. of Orders per Day?
Here are the Columns along with their data in my Database.
order_id order_placed_date order_total
- 1 12/30/2008 12:06:24 AM 2499.99
- 2 2/3/2009 1:57:17 AM 199.99
- 3 2/3/2009 1:58:27 AM 449.99
- 4 5/3/2009 1:58:48 AM 299.99
- 5 6/3/2009 2:00:31 AM 359.94
- 6 6/3/2009 2:01:47 AM 279.97
- 7 6/3/2009 2:02:31 AM 1359.94
- 9 7/1/2009 2:21:18 PM 5099.98
- 10 7/1/2009 2:21:36 PM 2621.97
- 11 7/2/2009 2:22:18 PM 2169.95
- 12 7/3/2009 2:23:29 PM 2249.95
- 13 7/4/2009 2:24:24 PM 5509.95
- 14 7/5/2009 12:15:17 AM 449.99
- 15 7/5/2009 12:18:08 AM 2299.99
- 16 7/5/2009 12:18:28 AM 3999.99
- 17 7/5/2009 12:18:45 AM 1939.99
- 18 7/5/2009 11:58:07 PM 39.99
- 19 7/6/2009 12:00:42 AM 1899.99
- 20 7/6/2009 12:01:00 AM 3999.99
- 21 7/7/2009 12:06:38 AM 199.99
- 22 7/7/2009 12:08:31 AM 1143.97
- 23 7/7/2009 12:09:13 AM 449.99
- 26 7/15/2009 1:30:03 PM 5469
- 27 7/15/2009 2:14:24 PM 329.97
- 28 7/15/2009 6:18:47 PM 5469
- 29 7/15/2009 10:17:36 PM 39.99
For e.g. there are 2 orders in the month of Febuary 2009
- 2 2/3/2009 1:57:17 AM 199.99
- 3 2/3/2009 1:58:27 AM 449.99
I need a sql query which would calculate and show the total amount per day. So for 3rd of Feb 2009, the total would be 699.98
I need to display Total Order Amount per day in a Chart
If it would be easier to do it with PHP, do mention it as well.
UPDATE:
I would like to clarify that I needed the Total Amount Per Day in the Present Month. I forgot to mention that in my initial question.
So i udpated Peter's query to get Total No. of Orders + Total Amount per Day in this Month.
Please, let me know if it needs any correction or is there a better and shorter way of doing it.
SELECT date(order_placed_date), COUNT(order_id) AS num_orders, SUM(order_total) AS daily_total
FROM orders
WHERE order_placed_date>=date_sub(current_date, INTERVAL 31 DAY)
GROUP BY date(order_placed_date)
MySQL's date() function will return a DATEIME or TIMESTAMP value without any hour/minute/second info - which means you reduce the accuracy to the day of the value.
So all you need to do is group by that and then add your aggregate functions to the right columns.
SELECT date(order_placed_date)
, COUNT(id) AS num_orders
, SUM(order_total) AS daily_total
FROM [Table]
GROUP BY date(order_placed_date)
SELECT date(order_placed_date)
, COUNT(id) AS num_orders
, SUM(order_total) AS daily_total
FROM orders
GROUP BY 1
Just copied Peter's answer but altered it so it will work. Plus shortcuted the group by.
A group by is your friend here. It can aggregate on grouped rows. An example query would be:
SELECT order_placed_date, SUM(order_total)
FROM orders
GROUP BY order_placed_date
Of course, in your example you'd probably want to extract just the day/month/year part using the DATE() function, and group by that.
select YEAR(datename(year,ORDER_DATE)) as YEARS,
count(ORDER_nO) as [NO(ORDER)]
from ProductOrder
group by YEAR(datename(year,ORDER_DATE))
I don't know what it is in MySQL but in MSSQL it would be this,
select
datepart(yyyy, order_placed_date),
datepart(mm, order_placed_date),
datepart(dd, order_placed_date),
sum(order_total)
from orders
group by datepart(yyyy, order_placed_date), datepart(mm, order_placed_date), datepart (dd, order_placed_date)