본문 바로가기
데이터 분석/SQL과 Python

[SQL] 데이터 정합성 검증하기(1) - 결손값

2023. 1. 2.

데이터를 수집하면서 데이터가 잘 쌓이는지는 우연에 맡긴 경향이 있는데 규모가 커질수록 영향도가 커진다는 생각이 들었다. 그래서 데이터 정합성을 검증하는 관점을 세 가지로 구분해보았다. (엄청 어려운 일은 아니지만 귀찮은 일에 속하는 것 같다)

목차
[SQL] 데이터 정합성 검증하기(1) - 결손값
[SQL] 데이터 정합성 검증하기(2) - 중복값
[SQL] 데이터 정합성 검증하기(3) - 이상값


결손값: 있어야 하는 게 없거나, 없어야 하는 게 있을 때

결손도 확인하기

액션의 종류에 따라 컬럼의 값이 not null이어야 하는지, null 허용인지 달라진다. 가령 purchase 액션은 products와 amount 컬럼의 값이 반드시 not null이어야 한다.

select
    action,
    round(avg(case when session is not null then 1.0
			else 0.0 end),2) as session,
    round(avg(case when user_id is not null then 1.0
			else 0.0 end),2) as user_id,
    -- action = view 일 때만 null
    round(avg(case when action = 'view' then case when category is null then 1.0
                                            else 0.0 end
		else case when category is not null then 1.0 
                    else 0.0 end end),2) as category,
    -- action = view 일 때만 null
    round(avg(case when action = 'view' then case when products is null then 1.0
                                            else 0.0 end
		else case when products is not null then 1.0 
                    else 0.0 end end),2) as products,
    -- action = purchase 일 때만 null
    round(avg(case when action = 'purchase' then case when amount is not null then 1.0
                                            else 0.0 end
		else case when amount is null then 1.0 
                    else 0.0 end end),2) as amount,
    round(avg(case when stamp is not null then 1.0
			else 0.0 end),2) as stamp
from invalid_action_log
group by action

✔ 각 컬럼 값이 null 인지 아닌지 액션별 조건에 부합하는지에 따라 점수를 부여
✔ 점수의 평균값을 계산하여 액션별 컬럼 값의 결손 정도를 판별(결손값이 없으면 1점)

결손도 확인하기


데이터 출처: 데이터 분석을 위한 SQL 레시피

이어보기
[SQL] 데이터 정합성 검증하기(2) - 중복값
[SQL] 데이터 정합성 검증하기(3) - 이상값

댓글