데이터를 사용하기 위해 테이블을 조작하기 전에 항상 테이블의 데이터가 올바른 데이터인지 확인해봐야 한다.
다음은 테이블의 데이터의 품질을 확인하는 4가지 방법과 sql 쿼리다.
모든 레코드를 카운트하고, 중복을 제거한 뒤 카운트해 값을 비교한다.
select count(1) from adhoc.session_summary;
select count(1) from (
select distinct userId, sessionId, channel, ts
from adhoc.session_summary
);
with ds as (
select distinct userId, sessionId, ts, channel
from adhoc.session_summary
)
select count(1) from ds;
select min(ts), max(ts) from adhoc.session_summary;
select sessionId, count(1)
from adhoc.session_summary
group by 1
order by 2 desc
limit 1;
가장 큰 값이 1이라면 중복이 없다는 의미
select
count(case when sessionId is null then 1 end) sessionid_null_count,
count(case when userId is null then 1 end) userid_null_count,
count(case when ts is null then 1 end) ts_null_count,
count(case when channel is null then 1 end) channel_null_count
from adhoc.session_summary;
case when then else 문에서 else 값을 넣어주지 않는다면 Null을 리턴한다. 즉 count에 더해지지 않는다.