Find customers that re-ordered the same purchase, MySql - mysql

I have 3 tables: users, orders and order_prices with the latter containing the analysis of each order and I need to find the customers that re-ordered in a specific month, ( both tables have user_id, order_id, order_date, good_id(only order_prices) etc) my script is the following:
select o.system_id, o.user_id, o.date, o.id
from orders o
where o.date >= '2018-11-01' and o.date <= '2018-11-30' -- in the date range
AND o.user_id in
( -- has made the same order (content)
select op.user_id
from order_prices op join users u on u.id = op.user_id
where op.good_id in
(
select good_id
from order_prices
where user_id = o.user_id
)
And u.number_of_orders > 0 -- has ordered before
)
group by o.user_id
Well i am not sure if this even works logically, also I guess I need to find those users that their previous order was the same in content with their last order the above script doesn't take into consideration.... any suggestions MORE than welcome!

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).

Select accounts where last order placed was older than X date

I am trying to identify shoppers who last placed an order on or before a specific date.
I want to do this so that I can identify stale accounts and unsubscribe them from mailings.
So far I have this query, but it seems to be retrieving accounts that have ordered more recently than the specified date - which is not what I want!
SELECT
s.id,s.first_name,s.last_name,s.email, latest_orders.last_order_date, au.verified, au.unh
FROM
(SELECT
shopper_id, MAX(order_date) AS last_order_date
FROM
order_list WHERE order_date <= ?
GROUP BY
shopper_id) AS latest_orders
INNER JOIN
shoppers s
ON
s.id = latest_orders.shopper_id
JOIN auth_users au ON au.shopper_id=s.id
WHERE s.mail_outs='Y' LIMIT 50
Use having:
SELECT shopper_id, MAX(order_date) AS last_order_date
FROM order_list
GROUP BY shopper_id
HAVING MAX(order_date) <= ?;
If you want all information about the shoppers, then join back to shoppers:
SELECT s.*, os.last_order_date
FROM shoppers s JOIN
(SELECT shopper_id, MAX(order_date) AS last_order_date
FROM order_list
GROUP BY shopper_id
HAVING MAX(order_date) <= ?
) os
ON os.shopper_id = s.id;

Can you tell me what's wrong in the query I'm using to retrieve info on when a customer last placed an order?

I'm trying to write a query on MySQL workbench that shows me when a customer last ordered from my client's website. I'm using two tables for this purpose. One is called "orders", which is updated with order time whenever a new order is placed and assigns a unique id to each new order. The other table is called "orders_customers_details", which is also updated whenever a new order is placed and contains the unique id (based on a combination of customer's email address and phone number) of the customer placing the order, as well as a key which corresponds to the orders table's id.
My problem is that the query I'm using is not returning every customer's most recent order. In the case of some customers, I'm being given the date of their fourth or fifth most recent order.
I'm left joining orders on orders_customer_details:
left join orders o
on ocd.id = o.customer_details_id
I've also tried using a left outer join, and the following join:
from orders o, orders_customers_details ocd
where o.customer_details_id = ocd.id
In order to retrieve the most recent order only, I'm grouping by customer_id HAVING max(order.id). FYI, order.id increases with the placement of each new order, so the order with the highest id is the most recent order.
I've also tried
SELECT customer_id, MAX(order.id)
and then grouped by customer_id, but to no avail.
Here's the entire code:
select customer_id, o.id as id_of_last_order, date(order_date) as
last_ordered, timestampdiff(day, order_date, now()) as
days_since_last_ordered
from orders o, orders_customers_details ocd
where o.customer_details_id = ocd.id
group by customer_id having max(o.id)
order by customer_id;
A typical method in MySQL is to use a correlated subquery to get the most recent order:
select ocd.customer_id, o.id as id_of_last_order,
date(o.order_date) as last_ordered,
timestampdiff(day, o.order_date, now()) as days_since_last_ordered
from orders o join
orders_customers_details ocd
on o.customer_details_id = ocd.id
where o.order_date = (select max(o2.order_date)
from orders o2 join
orders_customers_details ocd2
on o2.customer_details_id = ocd2.id
where oc2.customer_id = ocd.customer_id
)
order by ocd.customer_id;
Of course, if o.id is an auto-incrementing column, then the largest value is from the most recent date. If this is the case, then you can just use aggregation:
select ocd.customer_id,
max(o.id) as id_of_last_order,
date(max(o.order_date)) as last_ordered,
timestampdiff(day, max(o.order_date), now()) as days_since_last_ordered
from orders o join
orders_customers_details ocd
on o.customer_details_id = ocd.id
group by ocd.customer_id
order by ocd.customer_id;
In MySQL 8+, you would simply use window functions:
select *
from (select ocd.customer_id, o.id as id_of_last_order,
date(o.order_date) as last_ordered,
timestampdiff(day, o.order_date, now()) as days_since_last_ordered,
row_number() over (partition by ocd.customer_id order by o.order_date desc) as seqnum
from orders o join
orders_customers_details ocd
on o.customer_details_id = ocd.id
) ocd
where seqnum = 1
order by ocd.customer_id;
Using a correlated sub query in the where clause to find the most recent order by customer and tidying up the code then something like this
select customer_id, o.id as id_of_last_order, date(order_date) as last_ordered,
timestampdiff(day, order_date, now()) as days_since_last_ordered
from orders_customers_details ocd
join orders o on o.customer_details_id = ocd.id
where ocd_id = (select max(ocd_id) from orders_customers_details ocd1 where ocd1.customer_id = ocd_customer_id);
Though I cannot be certain without table definitions sample data etc..
Try this-
SELECT
customer_details_id customer_id,
MAX(o.id) AS id_of_last_order,
MAX(order_date) AS last_ordered,
TIMESTAMPDIFF(DAY, MAX(order_date), NOW()) AS days_since_last_ordered
FROM orders O
INNER JOIN orders_customers_details OCD
ON O.customer_details_id = OCD.id
GROUP BY customer_details_id
ORDER BY customer_details_id;

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

MySQL Query not displaying correctly

I am having to set up a query that retrieves the last comment made on a customer, if no one has commented on them for more than 4 weeks. I can make it work using the query below, but for some reason the comment column won't display the latest record. Instead it displays the oldest, however the date shows the newest. It may just be because I'm a noob at SQL, but what exactly am I doing wrong here?
SELECT DISTINCT
customerid, id, customername, user, MAX(date) AS 'maxdate', comment
FROM comments
WHERE customerid IN
(SELECT DISTINCT id FROM customers WHERE pastdue='1' AND hubarea='1')
AND customerid NOT IN
(SELECT DISTINCT customerid FROM comments WHERE DATEDIFF(NOW(), date) <= 27)
GROUP BY customerid
ORDER BY maxdate
The first "WHERE" clause is just ensuring that it shows only customers from a specific area, and that they are "past due enabled". The second makes sure that the customer has not been commented on within the last 27 days. It's grouped by customerid, because that is the number that is associated with each individual customer. When I get the results, everything is right except for the comment column...any ideas?
Join much better to nested query so you use the join instead of nested query
Join increase your speed
this query resolve your problem.
SELECT DISTINCT
customerid,id, customername, user, MAX(date) AS 'maxdate', comment
FROM comments inner join customers on comments.customerid = customers.id
WHERE comments.pastdue='1' AND comments.hubarea='1' AND DATEDIFF(NOW(), comments.date) <= 27
GROUP BY customerid
ORDER BY maxdate
I think this might probably do what you are trying to achieve. If you can execute it and maybe report back if it does or not, i can probably tweak it if needed. Logically, it ' should' work - IF i have understood ur problem correctly :)
SELECT X.customerid, X.maxdate, co.id, c.customername, co.user, co.comment
FROM
(SELECT customerid, MAX(date) AS 'maxdate'
FROM comments cm
INNER JOIN customers cu ON cu.id = cm.customerid
WHERE cu.pastdue='1'
AND cu.hubarea='1'
AND DATEDIFF(NOW(), cm.date) <= 27)
GROUP BY customerid) X
INNER JOIN comments co ON X.customerid = co.customerid and X.maxdate = co.date
INNER JOIN customer c ON X.customerid = c.id
ORDER BY X.maxdate
You need to have subquery for each case.
SELECT a.*
FROM comments a
INNER JOIN
(
SELECT customerID, max(`date`) maxDate
FROM comments
GROUP BY customerID
) b ON a.customerID = b.customerID AND
a.`date` = b.maxDate
INNER JOIN
(
SELECT DISTINCT ID
FROM customers
WHERE pastdue = 1 AND hubarea = 1
) c ON c.ID = a.customerID
LEFT JOIN
(
SELECT DISTINCT customerid
FROM comments
WHERE DATEDIFF(NOW(), date) <= 27
) d ON a.customerID = d.customerID
WHERE d.customerID IS NULL
The first join gets the latest record for each customer.
The second join shows only customers from a specific area, and that they are "past due enabled".
The third join, which uses LEFT JOIN, select all customers that has not been commented on within the last 27 days. In this case,only records without on the list are selected because of the condition d.customerID IS NULL.
But tomake your query shorter, if the customers table has already unique records for customer, then you don't need to have subquery on it.Directly join the table and put the condition on the WHERE clause.
SELECT a.*
FROM comments a
INNER JOIN
(
SELECT customerID, max(`date`) maxDate
FROM comments
GROUP BY customerID
) b ON a.customerID = b.customerID AND
a.`date` = b.maxDate
INNER JOIN customers c
ON c.ID = a.customerID
LEFT JOIN
(
SELECT DISTINCT customerid
FROM comments
WHERE DATEDIFF(NOW(), date) <= 27
) d ON a.customerID = d.customerID
WHERE d.customerID IS NULL AND
c.pastdue = 1 AND
c.hubarea = 1
Two of your table columns are not contained in either an aggregate function or the GROUP BY clause. for example suppose that you have two data rows with the same customer id and same date, but with different comment data. how SQL should aggregate these two rows? :( it will generate an error...
try this
select customerid, id, customername, user,date, comment from(
select customerid, id, customername, user,date, comment,
#rank := IF(#current_customer = id, #rank+ 1, 1),
#current_customer := id
from comments
where customerid IN
(SELECT DISTINCT id FROM customers WHERE pastdue='1' AND hubarea='1')
AND customerid NOT IN
(SELECT DISTINCT customerid FROM comments WHERE DATEDIFF(NOW(), date) <= 27)
order by customerid, maxdate desc
) where rank <= 1