이번주는 금요일이 아닌 목요일에 진행되었다. 그래서 왠지 오늘이 금욜 같잖아!!
결과는... 문제 풀이 마지막에서...
SHOW DATEABASE;
USE products;
SELECT *
FROM stores s;
# STORE_ID, REGION_NAME, SALES, EMPLOYEES, OPEN_DATE, TYPE
SELECT *
FROM stores s
GROUP BY REGION_NAME
HAVING COUNT(DISTINCT STORE_ID) >= 2 ;
SELECT *
FROM stores s
GROUP BY REGION_NAME
HAVING COUNT(DISTINCT STORE_ID) >= 2
ORDER BY REGION_NAME;
SELECT REGION_NAME AS region_name,
MAX(SALES) AS highest_sales
FROM stores s
GROUP BY REGION_NAME
HAVING COUNT(DISTINCT STORE_ID) >= 2
ORDER BY REGION_NAME;
SELECT
region_name,
MAX(SALES) AS highest_sales
FROM stores
GROUP BY REGION_NAME
HAVING COUNT(DISTINCT STORE_ID) > 1
ORDER BY REGION_NAME;
SELECT *
FROM payments p ;
# ID, USER_ID, AMOUNT, PAY_DATE, PAYMENT_TYPE(0: CASH, 1: CARD)
SELECT *
FROM orders o ;
# ID, USER_ID, ORDER_DATE, ITEM
SELECT count(DISTINCT o.USER_ID) AS cnt_nopay
FROM orders o LEFT JOIN payments p ON o.USER_ID = p.USER_ID
WHERE p.PAY_DATE IS NULL;
SELECT COUNT(DISTINCT o.USER_ID) AS cnt_preord
FROM orders o LEFT JOIN
(SELECT USER_ID,
MIN(p.PAY_DATE) AS first_pay_date
FROM payments p
GROUP BY USER_ID) fp
ON o.USER_ID = fp.USER_ID
WHERE (o.ORDER_DATE < fp.first_pay_date);
SELECT COUNT(DISTINCT o.USER_ID) AS cnt
FROM orders o LEFT JOIN
(SELECT USER_ID,
MIN(p.PAY_DATE) AS first_pay_date
FROM payments p
GROUP BY USER_ID) fp
ON o.USER_ID = fp.USER_ID
WHERE (fp.USER_ID IS NULL) OR (o.ORDER_DATE < fp.first_pay_date);
WITH first_payment AS (
SELECT
USER_ID,
MIN(PAY_DATE) AS FIRST_PAY_DATE
FROM payments
GROUP BY USER_ID
)
SELECT
COUNT(DISTINCT o.USER_ID) cnt
FROM orders o
LEFT JOIN first_payment fp
ON o.USER_ID = fp.USER_ID
WHERE fp.USER_ID IS NULL
OR o.ORDER_DATE < fp.FIRST_PAY_DAT;
SELECT *
FROM cart_products cp ;
# ID, CART_ID, NAME, PRICE
SELECT A.CART_ID,
A.NAME AS name_x,
B.NAME AS name_y
FROM cart_products A
JOIN cart_products B
ON A.CART_ID = B.CART_ID AND A.NAME <> B.NAME
GROUP BY A.NAME, B.NAME;
SELECT
A.NAME AS name_x,
B.NAME AS name_y,
COUNT(DISTINCT A.CART_ID) as orders
FROM cart_products A
JOIN cart_products B
ON A.CART_ID = B.CART_ID AND A.NAME <> B.NAME
GROUP BY A.NAME, B.NAME
ORDER BY A.NAME, B.NAME;
SELECT
A.NAME AS name_x,
B.NAME AS name_y,
COUNT(DISTINCT A.CART_ID) as orders
FROM cart_products A
JOIN cart_products B
ON A.CART_ID = B.CART_ID AND A.NAME <> B.NAME
GROUP BY A.NAME, B.NAME
ORDER BY A.NAME, B.NAME;
완전 다 틀렸당!
오늘 좀 정신이 없어서 넋놓고 풀었더니 저모양..ㅎ