데이터 품질 확인 방법 4가지

Minseok Kim·2023년 5월 11일
0

데이터를 사용하기 위해 테이블을 조작하기 전에 항상 테이블의 데이터가 올바른 데이터인지 확인해봐야 한다.
다음은 테이블의 데이터의 품질을 확인하는 4가지 방법과 sql 쿼리다.

중복된 레코드 체크

모든 레코드를 카운트하고, 중복을 제거한 뒤 카운트해 값을 비교한다.

select count(1) from adhoc.session_summary;

select count(1) from (
	select distinct userId, sessionId, channel, ts 
	from adhoc.session_summary
);
  • CTE를 사용해서 중복 제거 후 카운트 해보기
    with ds as (
    	select distinct userId, sessionId, ts, channel
    	from adhoc.session_summary
    )
    
    select count(1) from ds;

최신 데이터의 존재 여부 체크 (freshness)

select min(ts), max(ts) from adhoc.session_summary;

pk uniqueness가 지켜지는지 체크 ⭐️

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에 더해지지 않는다.

0개의 댓글