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

打印 上一主题 下一主题

主题 507|帖子 507|积分 1521

 
目次
需求
范例运行环境
数据样本设计
功能实现
上传EXCEL文件到数据库
SQL语句

小结


需求

在日常的应用中,排查列重复记录是经常碰到的一个题目,但某些需求下,需要我们排查一组列之间是否有重复值的环境。比如我们有一组题库数据,重要包罗题目和选项字段(如单选选择项或多选选择项) ,一个合理的数据存储应该包管这些选项列之间不应该出现重复项目数据,比如选项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选项A5Bnvarchar选项B6Cnvarchar选项C7Dnvarchar选项D
功能实现


上传EXCEL文件到数据库

导入功能请参阅我的文章《C#实现Excel归并单位格数据导入数据集》这里不再赘述。

SQL语句

首先通过 UNION ALL 将A到D的各列的值给组合成记录集 a,代码如下:
  1.         select A as item,sortid from exams  
  2.          union all
  3.         select B as item,sortid from exams  
  4.          union all
  5.         select C as item,sortid from exams  
  6.          union all
  7.         select D as item,sortid from exams  
复制代码
其次,通过 group by 对 sortid (题号) 和 item (选项) 字段举行分组统计,利用 count 聚合函数统计选项在 题号 中出现的个数,如下封装:
  1. select item,count(item) counts,sortid from (        select A as item,sortid from exams  
  2.          union all
  3.         select B as item,sortid from exams  
  4.          union all
  5.         select C as item,sortid from exams  
  6.          union all
  7.         select D as item,sortid from exams  ) a group by sortid,item order by sortid
复制代码

最后利用 having 语句对结果集举行过滤,排查出题目记录,如下语句:
  1. select item,count(item) counts,sortid from (        select A as item,sortid from exams  
  2.          union all
  3.         select B as item,sortid from exams  
  4.          union all
  5.         select C as item,sortid from exams  
  6.          union all
  7.         select D as item,sortid from exams  ) a group by sortid,item   having count(item)>1 order by sortid
复制代码
在查询分析器运行SQL语句,显示如下图:

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


小结

我们可以继承美满对结果的分析,以标注题目序号是哪几个选项之间重复,可通过如下语句实现:
  1. 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  
  2.          union all
  3.         select B as item,sortid from exams  
  4.          union all
  5.         select C as item,sortid from exams  
  6.          union all
  7.         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
这个用于对比每一个选项列,得到对应的选项列名,运行查询分析器,结果显示如下:


这样我们可以更直观的看到重复的选项列名是哪几个,以更有效帮助我们改正题目。在现实的应用中每一个环节我们都难免会出现一些失误,因此不绝的根据现实的发生环境总结履历,通过盘算来分析,将题目扼杀在摇篮里,以最大包管限度的包管项目运行结果的质量。
至此关于排查多列之间重复值的题目就介绍到这里,感谢您的阅读,希望本文能够对您有所帮助。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表