数据质量六要素: 完整性、有效性、一致性、准确性、唯一性、及时性
备注:
1,示例SQL只供参考思路,不一定正确。 对于长度格式等校验可使用正则表达式。
2,根据实际业务选择合适的测试点,不求齐全但求适用。各个测试点不一定要归到六要素里面,本身就有一定的交叉联系。
一、完整性:
- 整条数据信息是否完整;统计全表的数据条数(如应该有100条数据实际只有90条则不完整);统计表中各业务每日的数据增量,计算数据量的同比环比情况;预测数据量的未来趋势,预测软硬件资源扩容和下一年的资金预算等。
// 抽样查看整体数据信息情况 SELECT iot_code,time,humidity,temperature FROM iot_weather where 1=1 LIMIT 20; // 统计数据量情况 SELECT count(*) FROM iot_weather where 1=1 and iot_code='1200020002'; // 统计昨天的数据量 SELECT count(*) FROM iot_weather where 1=1 and iot_code='1200020002' and time >DATE_SUB(curdate(),INTERVAL 1 DAY) and time <DATE_SUB(curdate(),INTERVAL 0 DAY);
- 数据组成字段是否完整;统计数据中某个字段的缺失和空值情况,对于“必填、关键 、重要”的字段肯定不能为空,否则就是有问题,即统计空值数量是0;对非必填字段根据实际情况统计空值占比情况,如果空值占比波动变大,则可能出现了问题。
// 统计某个字段的空值情况 SELECT temperature FROM iot_weather where 1=1 and iot_code='1200020002' and (temperature is null or trim(temperature) = '' ); // 统计某个字段空值占比情况。temperature is not null select (SELECT count(*) FROM iot_weather where 1=1 and iot_code='1200020002' and (temperature is null or trim(temperature) = '' ))/(SELECT count(*) FROM iot_weather where 1=1 and iot_code='1200020002'); // 统计某个字段情况的占比。count(temperature)只会统计temperature有值的行数。 SELECT count(temperature)/count(*) as a FROM iot_weather where 1=1 and iot_code='1200020002';