当前位置: 代码网 > it编程>数据库>MsSqlserver > MS SQL Server排查多列之间的值是否重复的功能实现

MS SQL Server排查多列之间的值是否重复的功能实现

2024年09月18日 MsSqlserver 我要评论
需求在日常的应用中,排查列重复记录是经常遇到的一个问题,但某些需求下,需要我们排查一组列之间是否有重复值的情况。比如我们有一组题库数据,主要包括题目和选项字段(如单选选择项或多选选择项),一个合理的数

需求

在日常的应用中,排查列重复记录是经常遇到的一个问题,但某些需求下,需要我们排查一组列之间是否有重复值的情况。比如我们有一组题库数据,主要包括题目和选项字段(如单选选择项或多选选择项) ,一个合理的数据存储应该保证这些选项列之间不应该出现重复项目数据,比如选项a不应该和选项b的值重复,选项b不应该和选项c的值重复,以此穷举类推,以保证这些选项之间不会出现重复的值。本文将介绍如何利用 group by  、having 语句来实现这一需求,主要实现如下功能:

(1)上传 excel 版试题题库到 ms sql server 数据库进行导入

(2)通过 union all  将各选项列的数据进行 转记录行的合并

(3)通过 group by 语句 和 count 聚合函数统计重复情况

(4)通过 having 子句筛选出重复记录

范例运行环境

操作系统: windows server 2019 datacenter

数据库:microsoft sql server 2016

.netframework 4.7.2

数据样本设计

假设有 excel 数据题库如下:

如图我们假设设计了错误的数据源,第4题的a选项与d选项重复,第8题的a选项与c选项重复了。

题库表 [exams] 设计如下:

序号字段名类型说明备注
1sortidint排序号题号,唯一性
2etypenvarchar试题类型如多选、单选
3etitlenvarchar题目
4anvarchar选项a
5bnvarchar选项b
6cnvarchar选项c
7dnvarchar选项d

功能实现

上传excel文件到数据库

导入功能请参阅我的文章c#实现excel合并单元格数据导入数据集详解_c#教程_代码网 (jb51.net)这里不再赘述。

sql语句

首先通过 union all 将a到d的各列的值给组合成记录集 a,代码如下:

	select a as item,sortid from exams  
	 union all
	select b as item,sortid from exams  
	 union all
	select c as item,sortid from exams  
	 union all
	select d as item,sortid from exams  

其次,通过 group by 对 sortid (题号) 和 item (选项) 字段进行分组统计,使用 count 聚合函数统计选项在 题号 中出现的个数,如下封装:

select item,count(item) counts,sortid from (
	select a as item,sortid from exams  
	 union all
	select b as item,sortid from exams  
	 union all
	select c as item,sortid from exams  
	 union all
	select d as item,sortid from exams  
) a group by sortid,item order by sortid

最后使用 having 语句对结果集进行过滤,排查出问题记录,如下语句:

select item,count(item) counts,sortid from (
	select a as item,sortid from exams  
	 union all
	select b as item,sortid from exams  
	 union all
	select c as item,sortid from exams  
	 union all
	select d as item,sortid from exams  
) a group by sortid,item   having count(item)>1 order by sortid

在查询分析器运行sql语句,显示如下图:

由此可以看出,通过查询可以排查出第4题和第8题出现选项重复问题。 

小结

我们可以继续完善对结果的分析,以标注问题序号是哪几个选项之间重复,可通过如下语句实现:

 
select case when a=item then 'a' else ''end+
case when b=item then 'b' else '' end +
case when c=item then 'c' else '' end +
case when d=item then 'd' else '' end tip
,b.* from  
(select item,count(item) counts,sortid from (
	select a as item,sortid from exams  
	 union all
	select b as item,sortid from exams  
	 union all
	select c as item,sortid from exams  
	 union all
	select d as item,sortid from exams  
) a group by sortid,item   having count(item)>1 ) b,exams c where b.sortid=c.sortid

关键语句:case when a=item then 'a' else ''end+
case when b=item then 'b' else '' end +
case when c=item then 'c' else '' end +
case when d=item then 'd' else '' end tip

这个用于对比每一个选项列,得到对应的选项列名,运行查询分析器,结果显示如下:

这样我们可以更直观的看到重复的选项列名是哪几个,以更有效帮助我们改正问题。在实际的应用中每一个环节我们都难免会出现一些失误,因此不断的根据实际的发生情况总结经验,通过计算来分析,将问题扼杀在摇篮里,以最大保证限度的保证项目运行效果的质量。

至此关于排查多列之间重复值的问题就介绍到这里,感谢您的阅读,希望本文能够对您有所帮助。

以上就是ms sql server排查多列之间的值是否重复的功能实现的详细内容,更多关于ms sql server排查值是都重复的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com