테이블 1 : Visits
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| visit_id | int |
| customer_id | int |
+-------------+---------+
visit_id is the column with unique values for this table.
This table contains information about the customers who visited the mall.테이블 2 : Transactions
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| transaction_id | int |
| visit_id | int |
| amount | int |
+----------------+---------+
transaction_id is column with unique values for this table.
This table contains information about the transactions made during the visit_id.문제
거래 없이 방문한 사용자의 ID와 이러한 유형의 방문 횟수를 찾는 솔루션을 작성하세요.
임의의 순서 로 정렬된 결과 테이블을 반환합니다 .Output
+---------------+---+
| 고객_ID | count_no_trans |
+---------------+---+
| 54 | 2 |
| 30 | 1 |
| 96 | 1 |
+---------------+---+
[조건]
select customer_id,
count(*) as count_no_trans
from visits vis left join transactions tra on vis.visit_id = tra.visit_id
where tra.visit_id is null
group by vis.customer_id
테이블 : Weather
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| recordDate | date |
| temperature | int |
+---------------+---------+
id is the column with unique values for this table.
This table contains information about the temperature on a certain day.문제
Id이전 날짜(어제)에 비해 온도가 더 높은 모든 날짜를 찾는 솔루션을 작성하세요 .
어떤 순서로든 결과 테이블을 반환합니다 .Output
+----+
| id |
+----+
| 2 |
| 4 |
+----+
[조건]
select id
from
(
select *,
lag(temperature, 1) over(order by recordDate) as pre_tem
from weather
) a
where temperature > pre_tem
또는 테이블을 두개로 지정하는 방법도 있다.
SELECT w1.id
FROM Weather w1, Weather w2
WHERE w1.Temperature > w2.Temperature AND DATEDIFF(w1.recordDate , w2.recordDate) = 1
-- lag 함수로 불러온 값 확인해보면 pre_tem에 temperatured의 이전행 값이 불러와진다.
select *,
lag(temperature, 1) over(order by recordDate) as pre_tem
from weather
| id | recordDate | temperature | pre_tem |
|---|---|---|---|
| 1 | 2015-01-01 | 10 | null |
| 2 | 2015-01-02 | 25 | 10 |
| 3 | 2015-01-03 | 20 | 25 |
| 4 | 2015-01-04 | 30 | 20 |