DISTINCT
특정 column에 대해 중복을 제거하는 sql문이다.
Apple stock prices dataset의 "month" column에서 unique values를 선택하기 위한 쿼리는 아래와 같다.
(result)
month |
1 |
2 |
... |
만약 column 두 개를 select 한다면 두 열의 쌍으로 unique한 pair들을 모두 출력할 것이다.
(result)
year | month |
2014 | 2 |
2014 | 5 |
2015 | 1 |
... | ... |
+ 위에 result는 모두 month 또는 year에 대해 오름차순이 되도록 setting 해놨는데,
만약 결과가 원하는 순서로 정렬되어 있지 않다면
마지막 줄에
order by (column 명) (desc) 를 덧붙여 주면 된다.
# Using DISTINCT in aggregations
aggregation 할 때 같이 이용할 수 있음.
특히 COUNT 함수랑 자주 쓰임.
예를 들어 주어진 table에서 "month" 열에 대하여 unique value가 몇 개(종류)가 있는지 찾기 위한 쿼리를 작성한다면?
** DISTINCT 절은 aggregate function안에 들어가야 함을 기억하자...
++ 예제
Q. Write a query that counts the number of unique values in the month column for each year.
select year ,
count (distinct month) as unique_months_count
from tutorial.aapl_historical_stock_price
group by year
order by year
Q. Write a query that seperately counts the number of unique values in the month column and the number of unique values in the 'year' column.
select year,
count (distinct month) as unique_months_count ,
count (distinct year) as unique_years_count
from tutorial.aapl_historical_stock_price
[참고자료]
'Study > SQL' 카테고리의 다른 글
SQL : Case (0) | 2023.07.14 |
---|---|
SQL : Having (0) | 2023.07.14 |
SQL : Group by (0) | 2023.07.10 |
SQL : Aggregate functions : Count, Sum, Min/Max, Avg (0) | 2023.07.06 |
SQL : not, order by (0) | 2023.07.01 |