MYSQL adding OR clause in left join slowing up the query - mysql

I have two database tables: orders and customers.
I'm running a SQL to get all orders in June month.
If the Ship To and Bill To email are different, we are inserting two different records with both emails to customers table.
select o.order_id
, o.total
, c.customer_email
from orders o
left
join customers c
ON o.bill_email = c.customer_email
OR o.ship_email = c.customer_email
where DATE(o.order_date) >= '2020-06-01'
But this SQL is taking too much time to load because of the condition,
ON o.bill_email=c.customer_email
OR o.ship_email=c.customer_email
How can I add both conditions in ON clause?
Any help would be appreciated.

Use two left joins and put the results in separate columns rather than rows:
select o.order_id, o.total, cb.customer_email, so.customer_email
from orders o left join
customers cb
on o.bill_email = cb.customer_email left join
customers cs
o.ship_email = cs.customer_email
where o.order_date >= '2020-06-01';
Note that the date() function is not needed.
That said, this seems more easily expressed as:
select o.order_id, o.total, o.bill_email
from orders o
where o.order_date >= '2020-06-01'
union all
select o.order_id, o.total, o.ship_email
from orders o
where o.order_date >= '2020-06-01' and s.ship_email <> o.bill_email;

Related

Finding first order in a single year

I'm trying to determine how many new people made an order in 2018. This looks straight forward enough but there is an error with putting calculated fields in the WHERE statement.
SELECT DISTINCT COUNT(c.customer_id)
FROM Customer c
LEFT JOIN
Orders o ON c.customer_id=o.customer_id
WHERE MIN(order_date) > '2017-12-31'
AND MIN(order_date) < '2019-01-01';
You can achieve this by putting a sequence number to the orders and then selecting the first row for each customer. Although, I'm not really sure why you're performing a count of the orders when you just want to consider the first orders. Nevertheless the below should work just fine.
SELECT count(res.customer_id) FROM (
SELECT c.customer_id,
ROW_NUMBER() OVER (PARTITION BY c.customer_id ORDER BY o.order_date ASC) row_num
FROM Customer c
LEFT JOIN Orders o ON c.customer_id=o.customer_id
WHERE o.order_date > '2017-12-31'
AND o.order_date < '2019-01-01'
) res WHERE res.row_num=1
Join with a subquery that finds the customers that were new in 2018.
SELECT COUNT(DISTINCT o.customer_id)
FROM Orders o
JOIN (
SELECT DISTINCT customer_id
FROM Orders
GROUP BY customer_id
HAVING MIN(order_date) > '2017-12-31'
) o1 ON o1.customer_id = o.customer_id
WHERE o.order_date < '2019-01-01';
There's also no need to join with Customers, since the customer ID is in Orders.
And the correct way to get the distinct count is COUNT(DISTINCT o.customer_id), not DISTINCT COUNT(o.customer_id).

How would I create a multiple table subquery from a JOIN?

So I know how to create the query using a JOIN but how do I turn this JOIN into a subquery? I have been trying all sorts of things but I just can't figure it out.
My join looks like this:
SELECT
c.email_address, o.order_id, o.order_date
FROM
customer c
JOIN orders o ON (c.customer_id = o.customer_id)
ORDER BY
order_date;
My attempt at creating the multiple table:
SELECT
c.email_address, o.order_id, o.order_date
FROM
customer c
WHERE
c.customer_id IN (SELECT o.customer_id FROM orders o)
ORDER BY
o.order_date;
I keep getting an error of unknown column for both order_id and order_date

Using IFNULL function mysql to set null to zero

I have tables on my database with the following schema:
customers (customerID: integer, fName: string, lName: string)
items (itemID: integer, description: string, price: integer)
orders (orderID: integer, itemID: integer, aID: integer, customerID:integer, date: date)
and the following code:
SELECT c.customerID, COUNT(DISTINCT o.orderID) AS number_of_orders,
ROUND(SUM(i.price) / COUNT(DISTINCT o.orderID),2) AS average
FROM customers c
LEFT JOIN orders o
ON o.customerID = c.customerID
AND o.date >= '2013-03-01'
AND o.date < '2013-04-01'
LEFT JOIN items i
ON o.itemID = i.itemID
GROUP BY c.customerID
which returns three values: customer ID, number of orders per customer, and average spending per customer.
With the code as it is now, the average spending per customer is returned as blank (null).
I am having trouble using the IFNULL function to set the average spending per customer to 0.00 if the customer did not order anything in march 2013 (i.e., if number of orders per customer in march is zero).
Any help will be very much appreciated!
Without seeing your IFNULL attempts, this logic works for me:
SELECT c.customerID,
COUNT(DISTINCT o.orderID) AS number_of_orders,
ROUND(IFNULL( (SUM(i.price) / COUNT(DISTINCT o.orderID)), 0.00),2) AS average,
FORMAT(IFNULL(ROUND( (SUM(i.price) / COUNT(DISTINCT o.orderID)),2), 0), 2) AS averageWithFormat
FROM customers c
LEFT JOIN orders o ON o.customerID = c.customerID AND o.date >= '2013-03-01' AND o.date < '2013-04-01'
LEFT JOIN items i ON o.itemID = i.itemID
GROUP BY c.customerID
Returns: 0.00
I have two entries for average in my statement because in my local MySQL Workbench, the first one returns with two decimal precision but doesn't in SQLFiddle. I added the second entry with the FORMAT syntax to force the two decimal precision, if needed.
You use case maybe better off using the IF() function.
SELECT c.customerID, COUNT(DISTINCT o.orderID) AS number_of_orders,
IF(COUNT(o.orderID) > 0,ROUND(SUM(i.price) / COUNT(DISTINCT o.orderID),2),0) AS average
FROM customers c
LEFT JOIN orders o
ON o.customerID = c.customerID
AND o.date >= '2013-03-01'
AND o.date < '2013-04-01'
LEFT JOIN items i
ON o.itemID = i.itemID
GROUP BY c.customerID

JOIN VS SUBQUERY

I need to do this but with a subquery, not a join. My problem is, how can I use a subquery to display another column? I could grab the info from there, but I'll be missing the order_date column from the orders table. Can I use a subquery to display it?
SELECT CONCAT(c.customer_first_name, ' ' , c.customer_last_name) AS customer_name, MAX(o.order_date) AS recent_order_date
FROM customers AS c
JOIN orders AS o
ON c.customer_id = o.customer_id
GROUP BY customer_name
ORDER BY MAX(o.order_date) DESC
It's not at all clear what resultset you are trying to return, but it looks an awful like the like the ubiquitous "latest row" problem.
The normative pattern for the solution to that problem is to use a JOIN to the inline view. If there's not a unique constraint, you run the possibility of returning more than one matching row.
To get the latest order (the row in the orders table with the maximum order_date for each customer, assuming that the (customer_id, order_date) tuple is unique, you can do something like this:
SELECT o.*
FROM ( SELECT n.customer_id
, MAX(n.order_date) AS latest_order_date
FROM orders n
GROUP BY n.customer_id
) m
JOIN orders o
ON o.customer_id = m.customer_id
AND o.order_date = m.latest_order_date
If you want to also retrieve columns from the customers table based on the customer_id returned from orders, you'd use a JOIN (not a subquery)
SELECT CONCAT(c.customer_first_name,' ',c.customer_last_name) AS customer_name
, c.whatever
, o.order_date AS recent_order_date
, o.whatever
FROM ( SELECT n.customer_id
, MAX(n.order_date) AS latest_order_date
FROM orders n
GROUP BY n.customer_id
) m
JOIN orders o
ON o.customer_id = m.customer_id
AND o.order_date = m.latest_order_date
JOIN customers c
ON c.customer_id = o.customer_id
ORDER BY o.order_date DESC, o.customer_id DESC
As I mentioned before, if a given customer can have two orders with the exact same value for order_date, there's potential to return more than one order for each customer_id.
To rectify that, we can return a unique key from the inline view, and use that in the join predicate to guarantee only a single row returned from orders.
(NOTE: this approach is specific to MySQL, with this syntax, other RDBMS will throw an error that essentially says "the GROUP BY must include all non-aggregates". But MySQL allows it.)
SELECT CONCAT(c.customer_first_name,' ',c.customer_last_name) AS customer_name
, c.whatever
, o.order_date AS recent_order_date
, o.whatever
FROM ( SELECT n.customer_id
, MAX(n.order_date) AS latest_order_date
, n.order_id
FROM orders n
GROUP BY n.customer_id
) m
JOIN orders o
AND o.customer_id = m.customer_id
AND o.order_date = m.latest_order_date
AND o.order_id = n.order_id
JOIN customers c
ON c.customer_id = o.customer_id
ORDER BY o.order_date DESC, o.customer_id DESC
I am not really sure i understand your question, but i think this works... (not tested though...)
SELECT
(
SELECT
CONCAT(c.customer_first_name, ' ' , c.customer_last_name)
FROM
customers c
WHERE
c.customer_id = o.customer_id
LIMIT 1
) AS customer_name,
MAX(o.order_date) AS recent_order_date
FROM
orders o
GROUP BY
customer_name
ORDER BY
MAX(o.order_date) DESC

left join with at least one row with condition from right mysql

I have 2 tables, restaurants and orders, orders table have restaurant_id, status and date fields, each day is a separate line in orders table.
I need to filter the restaurants and show only the restaurants that have at least one reservation for some date range, but I have to show it on a calendar, it should look like a list of restaurants with information.
Smth like this
So, it means that if at least one day with reserved status condition is satisfied in that date range, all the orders for that date range for that restaurant should be fetched as well.
I can not make a usual inner join,
SELECT r.`id`, r.`name`, o.`date`, o.`status`
FROM restaurants r
INNER JOIN orders o ON r.id = o.restaurant_id AND o.date BETWEEN '2013-08-10' AND '2013-08-31'
AND status = 'reserved'
because, in this case, for example, I will not have the order information for August 31, for restaurant2, because though it is in date rage from 10 - 31, but its status is 'closed'.
So, I am thinking to make a left join like this
SELECT r.`id`, r.`name`, o.`date`, o.`status`, o.`id` order_id
FROM restaurants r
LEFT JOIN orders o ON r.id = o.restaurant_id AND
o.date BETWEEN '2013-08-10' AND '2013-08-31'
WHERE o.`id` IS NOT NULL
But I need to also add one condition to ensure that there is at least one row with order's status = 'reserved', I tried to add where clause like COUNT(o.status = 1) > 0 but it gives an error.
THanks
I think the best way to achieve this (assuming things like 'N/A' and 'reserved' are stored in the orders table is to join to all orders in the first instance, and then use a second join to limit it to only restaurants that have a reservation within that period:
SELECT r.`id`, r.`name`, o.`date`, o.`status`, o.`id` order_id
FROM restaurants r
INNER JOIN orders o
ON r.id = o.restaurant_id
AND o.date BETWEEN '2013-08-10' AND '2013-08-31'
INNER JOIN
( SELECT DISTINCT o2.Restaurant_ID
FROM orders o2
WHERE o2.date BETWEEN '2013-08-10' AND '2013-08-31'
AND o2.Status = 'Reserved'
) o2
ON r.id = o2.restaurant_id;
Example on SQL Fiddle