MS SQL Server排查多列之間的值是否重復(fù)的功能實(shí)現(xiàn)
需求
在日常的應(yīng)用中,排查列重復(fù)記錄是經(jīng)常遇到的一個問題,但某些需求下,需要我們排查一組列之間是否有重復(fù)值的情況。比如我們有一組題庫數(shù)據(jù),主要包括題目和選項(xiàng)字段(如單選選擇項(xiàng)或多選選擇項(xiàng)) ,一個合理的數(shù)據(jù)存儲應(yīng)該保證這些選項(xiàng)列之間不應(yīng)該出現(xiàn)重復(fù)項(xiàng)目數(shù)據(jù),比如選項(xiàng)A不應(yīng)該和選項(xiàng)B的值重復(fù),選項(xiàng)B不應(yīng)該和選項(xiàng)C的值重復(fù),以此窮舉類推,以保證這些選項(xiàng)之間不會出現(xiàn)重復(fù)的值。本文將介紹如何利用 group by 、having 語句來實(shí)現(xiàn)這一需求,主要實(shí)現(xiàn)如下功能:
(1)上傳 EXCEL 版試題題庫到 MS SQL SERVER 數(shù)據(jù)庫進(jìn)行導(dǎo)入
(2)通過 union all 將各選項(xiàng)列的數(shù)據(jù)進(jìn)行 轉(zhuǎn)記錄行的合并
(3)通過 group by 語句 和 count 聚合函數(shù)統(tǒng)計(jì)重復(fù)情況
(4)通過 having 子句篩選出重復(fù)記錄
范例運(yùn)行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
數(shù)據(jù)庫:Microsoft SQL Server 2016
.netFramework 4.7.2
數(shù)據(jù)樣本設(shè)計(jì)
假設(shè)有 EXCEL 數(shù)據(jù)題庫如下:
如圖我們假設(shè)設(shè)計(jì)了錯誤的數(shù)據(jù)源,第4題的A選項(xiàng)與D選項(xiàng)重復(fù),第8題的A選項(xiàng)與C選項(xiàng)重復(fù)了。
題庫表 [exams] 設(shè)計(jì)如下:
序號 | 字段名 | 類型 | 說明 | 備注 |
---|---|---|---|---|
1 | sortid | int | 排序號 | 題號,唯一性 |
2 | etype | nvarchar | 試題類型 | 如多選、單選 |
3 | etitle | nvarchar | 題目 | |
4 | A | nvarchar | 選項(xiàng)A | |
5 | B | nvarchar | 選項(xiàng)B | |
6 | C | nvarchar | 選項(xiàng)C | |
7 | D | nvarchar | 選項(xiàng)D |
功能實(shí)現(xiàn)
上傳EXCEL文件到數(shù)據(jù)庫
導(dǎo)入功能請參閱我的文章C#實(shí)現(xiàn)Excel合并單元格數(shù)據(jù)導(dǎo)入數(shù)據(jù)集詳解_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 (選項(xiàng)) 字段進(jìn)行分組統(tǒng)計(jì),使用 count 聚合函數(shù)統(tǒng)計(jì)選項(xiàng)在 題號 中出現(xiàn)的個數(shù),如下封裝:
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 語句對結(jié)果集進(jìn)行過濾,排查出問題記錄,如下語句:
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
在查詢分析器運(yùn)行SQL語句,顯示如下圖:
由此可以看出,通過查詢可以排查出第4題和第8題出現(xiàn)選項(xiàng)重復(fù)問題。
小結(jié)
我們可以繼續(xù)完善對結(jié)果的分析,以標(biāo)注問題序號是哪幾個選項(xiàng)之間重復(fù),可通過如下語句實(shí)現(xiàn):
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
關(guān)鍵語句: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
這個用于對比每一個選項(xiàng)列,得到對應(yīng)的選項(xiàng)列名,運(yùn)行查詢分析器,結(jié)果顯示如下:
這樣我們可以更直觀的看到重復(fù)的選項(xiàng)列名是哪幾個,以更有效幫助我們改正問題。在實(shí)際的應(yīng)用中每一個環(huán)節(jié)我們都難免會出現(xiàn)一些失誤,因此不斷的根據(jù)實(shí)際的發(fā)生情況總結(jié)經(jīng)驗(yàn),通過計(jì)算來分析,將問題扼殺在搖籃里,以最大保證限度的保證項(xiàng)目運(yùn)行效果的質(zhì)量。
至此關(guān)于排查多列之間重復(fù)值的問題就介紹到這里,感謝您的閱讀,希望本文能夠?qū)δ兴鶐椭?/p>
以上就是MS SQL Server排查多列之間的值是否重復(fù)的功能實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于MS SQL Server排查值是都重復(fù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SQL?Server如何設(shè)置用戶只能訪問特定數(shù)據(jù)庫和訪問特定表或視圖
最近項(xiàng)目中需要限定特定的用戶只能查看并訪問特定的數(shù)據(jù)庫,防止多個用戶對數(shù)據(jù)庫操作時(shí)一些誤操作,所以這篇文章主要給大家介紹了關(guān)于SQL?Server如何設(shè)置用戶只能訪問特定數(shù)據(jù)庫和訪問特定表或視圖的相關(guān)資料,需要的朋友可以參考下2023-04-04SqlServer中查看當(dāng)前Open的Cursor方法
這篇文章主要介紹了SqlServer中查看當(dāng)前Open的Cursor方法,本文為了解決A cursor with thename ' ' already exists總結(jié)的幾個方法,需要的朋友可以參考下2015-07-07sql 查詢記錄數(shù)結(jié)果集某個區(qū)間內(nèi)記錄
sqlserver如何實(shí)現(xiàn)查詢記錄數(shù)某個區(qū)間內(nèi)記錄,本文將提供多種解決方法,需要了解的朋友可以參考下2012-11-11不同數(shù)據(jù)庫之間導(dǎo)入導(dǎo)出功能介紹
在SQL Server中使用最廣泛的就是通過SELECT INTO語句導(dǎo)出數(shù)據(jù),SELECT INTO語句同時(shí)具備兩個功能2010-12-12數(shù)據(jù)庫SQL中having和where的用法區(qū)別
這篇文章主要介紹了數(shù)據(jù)庫SQL中having和where的用法區(qū)別的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11