Reprint:
Disclaimer: If I violated anyone's rights, please contact me and I will delete it.
Welcome experts to spray me
Article Directory
1398. Customers who bought product A and product B but did not buy product C
Link: https://leetcode-cn.com/problems/customers-who-bought-products-a-and-b-but-not-c/solution/liang-chong-fang-shi-xi-huan-jiu- xing-by-rosieisme/
Title description
customer_id is the primary key of this table.
customer_name is the name of the customer.
order_id is the primary key of this table.
customer_id is the id of the customer who bought the product named "product_name".
The query result is shown in the following example.
answer
select a.customer_id, a.customer_name
from Customers a join Orders b on a.customer_id = b.customer_id
group by a.customer_id
having
sum(b.product_name ='A') *sum(b.product_name='B') >0
and sum(b.product_name = 'C') = 0
SELECT
customer_id, customer_name
FROM
Customers
WHERE
customer_id NOT IN (
SELECT customer_id
FROM Orders
WHERE product_name = 'C'
) AND Customer_id IN (
SELECT customer_id
FROM Orders
WHERE product_name = 'A'
) AND Customer_id IN (
SELECT customer_id
FROM Orders
WHERE product_name = 'B'
)
ORDER BY customer_id
1596. The most frequently ordered product by each customer
Link: https://leetcode-cn.com/problems/the-most-frequently-ordered-products-for-each-customer
Title description
customer_id is the primary key of
the table. The table contains all customer information
order_id is the primary key of
the table. This table contains the order information of customer customer_id.
No customer will order the same product more than once in a day
Write a SQL statement to find the most frequently ordered products for each customer.
The result form should have customer_id of every customer who has placed an order at least once, and the product_id and product_name of the product he ordered most frequently.
There is no order requirement for the returned results.
The query result format is shown in the following example:
Alice (customer 1) ordered the mouse three times and the keyboard once, so the mouse is the most frequently ordered item by Alice.
Bob (customer 2) ordered the keyboard once, ordered the mouse once, and ordered the monitor once, so these are the most frequently ordered items by Bob .
Tom (the Customer 3) only twice ordered display, so the display is the most frequently ordered items Tom.
Jerry (the Customer 4) once ordered only the keyboard, so the keyboard is the most frequently ordered items Jerry.
John (the Customer 5) did not order too Commodity, so we did not include John in the result table.
answer
select b.customer_id, c.product_id,p.product_name
from
(
select a.customer_id, a.product_id, max(a.cnt)as maxcnt
from
(
select customer_id, product_id, count(order_id)as cnt
from orders
group by customer_id, product_id
)a
group by a.customer_id
)b
join
(
select customer_id, product_id, count(order_id) as cnt
from orders
group by customer_id, product_id
)c
on b.customer_id = c.customer_id and b.maxcnt = c.cnt
join products p on p.product_id = c.product_id
order by b.customer_id, c.product_id
1421. Net present value query
Title description
Write a SQL to find the net present value of each query in the Queries table.
There is no order requirement for the result table.
The format of the query result is as follows:
The net present value of (7, 2018) is not in the NPV table, we treat it as 0.
The net present value of all other queries can be found in the NPV table.
answer
select q.id, q.year, ifnull(npv,0)as npv
from queries q left join npv n on q.id = n.id and q.year = n.year
order by q.id, q.year
# Write your MySQL query statement below
select
q.id,
q.year,
ifnull(npv,0) npv
from
Queries q
left join
NPV n using(id,year);