1. [MySQL] 피벗

    테이블과 결과

    Input: 
    Department table:
    +------+---------+-------+
    | id   | revenue | month |
    +------+---------+-------+
    | 1    | 8000    | Jan   |
    | 2    | 9000    | Jan   |
    | 3    | 10000   | Feb   |
    | 1    | 7000    | Feb   |
    | 1    | 6000    | Mar   |
    +------+---------+-------+
    Output: 
    +------+-------------+-------------+-------------+-----+-------------+
    | id   | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
    +------+-------------+-------------+-------------+-----+-------------+
    | 1    | 8000        | 7000        | 6000        | ... | null        |
    | 2    | 9000        | null        | null        | ... | null        |
    | 3    | null        | 10000       | null        | ... | null        |
    +------+-------------+-------------+-------------+-----+-------------+
    Explanation: The …
    read more
  2. [MySQL] 평균 구하기

    테이블과 결과

    Input: 
    Signups table:
    +---------+---------------------+
    | user_id | time_stamp          |
    +---------+---------------------+
    | 3       | 2020-03-21 10:16:13 |
    | 7       | 2020-01-04 13:57:59 |
    | 2       | 2020-07-29 23:09:44 |
    | 6       | 2020-12-09 10:39:37 |
    +---------+---------------------+
    Confirmations table:
    +---------+---------------------+-----------+
    | user_id | time_stamp          | action    |
    +---------+---------------------+-----------+
    | 3       | 2021-01-06 03:30 …
    read more
  3. [MySQL] REGEXP_LIKE

    테이블과 결과

    Input: 
    Patients table:
    +------------+--------------+--------------+
    | patient_id | patient_name | conditions   |
    +------------+--------------+--------------+
    | 1          | Daniel       | YFEV COUGH   |
    | 2          | Alice        |              |
    | 3          | Bob          | DIAB100 MYOP |
    | 4          | George       | ACNE DIAB100 |
    | 5          | Alain        | DIAB201      |
    +------------+--------------+--------------+
    Output: 
    +------------+--------------+--------------+
    | patient_id | patient_name | conditions   |
    +------------+--------------+--------------+
    | 3          | Bob          | DIAB100 MYOP |
    | 4          | George       | ACNE DIAB100 | 
    +------------+--------------+--------------+
    Explanation: Bob and George both have a condition that …
    read more
  4. [MySQL] SELECT 존재여부 체크

    SELECT 존재여부 체크

    • COUNT(*)로 세는 것이 더 느릴 수 있다. 그냥 1로 해라
    SELECT EXISTS 
    (
        SELECT 1
        FROM CVVE_POINT.CPO_POINT_EARN cpe, CVVE_CUSTOMER.CR_CUSTOMER cc
        WHERE cpe.CR_ID = cc.CR_ID
        AND cpe.CUH_ID = cc.CUH_ID
        AND cpe.CPE_ISSUE_DT BETWEEN DATE_ADD …
    read more
  5. [MySQL] SELF JOIN

    테이블

    Employee 테이블

    id name salary managerId
    1 Joe 70000 3
    2 Henry 80000 4
    3 Sam 60000
    4 Max 90000

    문제

    • 자신의 매니저보다 많이 salary를 받는 인원을 뽑으시오

    SELECT e1 …
    read more
  6. [MySQL] 특정 기간에만 존재하는 데이터 뽑기

    테이블과 결과

    Input: 
    Product table:
    +------------+--------------+------------+
    | product_id | product_name | unit_price |
    +------------+--------------+------------+
    | 1          | S8           | 1000       |
    | 2          | G4           | 800        |
    | 3          | iPhone       | 1400       |
    +------------+--------------+------------+
    Sales table:
    +-----------+------------+----------+------------+----------+-------+
    | seller_id | product_id | buyer_id | sale_date  | quantity | price |
    +-----------+------------+----------+------------+----------+-------+
    | 1         | 1          | 1        | 2019-01-21 | 2        | 2000  |
    | 1         | 2          | 2        | 2019-02-17 | 1        | 800   |
    | 2         | 2          | 3        | 2019-06 …
    read more
  7. [MySQL] UNION ALL

    테이블과 결과

    Input: 
    Teams table:
    +---------+-----------+
    | team_id | team_name |
    +---------+-----------+
    | 1       | Ajax      |
    | 4       | Dortmund  |
    | 6       | Arsenal   |
    +---------+-----------+
    Matches table:
    +--------------+--------------+-----------------+-----------------+
    | home_team_id | away_team_id | home_team_goals | away_team_goals |
    +--------------+--------------+-----------------+-----------------+
    | 1            | 4            | 0               | 1               |
    | 1            | 6            | 3               | 3               |
    | 4            | 1            | 5               | 2               |
    | 6            | 1            | 0               | 0               |
    +--------------+--------------+-----------------+-----------------+
    Output: 
    +-----------+----------------+--------+----------+--------------+-----------+
    | team_name | matches_played | points | goal_for | goal_against | goal_diff |
    +-----------+----------------+--------+----------+--------------+-----------+
    | Dortmund  | 2              | 6      | 6 …
    read more

links

social