我在名为 musics 的表中有 4 列 -'artist','genre', 'writer' , 'producer'.
我需要编写一个查询,如果没有与列名对应的值重复,它返回一个值 0 ;如果有重复的值,它应该返回一个值 1,对应于该列名。
任何帮助深表感谢
我在名为 musics 的表中有 4 列 -'artist','genre', 'writer' , 'producer'.
我需要编写一个查询,如果没有与列名对应的值重复,它返回一个值 0 ;如果有重复的值,它应该返回一个值 1,对应于该列名。
任何帮助深表感谢
为了Artist
select convert(bit,(count(1)-1))
from table_name
group by artist -- <-- Replace artist with column name for which duplicate
SELECT (COUNT(artist) <> COUNT(DISTINCT artist)) artist,
(COUNT(genre) <> COUNT(DISTINCT genre)) genre,
(COUNT(writer) <> COUNT(DISTINCT writer)) writer,
(COUNT(producer) <> COUNT(DISTINCT producer)) producer
FROM musics
另一个版本
SELECT
( SELECT (COUNT(*) > 0)
FROM (SELECT 1 FROM musics GROUP BY artist HAVING COUNT(*) > 1) a
) artist,
( SELECT (COUNT(*) > 0)
FROM (SELECT 1 FROM musics GROUP BY genre HAVING COUNT(*) > 1) g
) genre,
( SELECT (COUNT(*) > 0)
FROM (SELECT 1 FROM musics GROUP BY writer HAVING COUNT(*) > 1) w
) writer,
( SELECT (COUNT(*) > 0)
FROM (SELECT 1 FROM musics GROUP BY producer HAVING COUNT(*) > 1) p
) producer
样本数据
| artist | genre | writer | producer |
------------------------------------------
| artist1 | genre1 | writer1 | producer1 |
| artist2 | genre2 | writer1 | producer2 |
| artist1 | genre3 | writer3 | producer3 |
样本输出:
| artist | genre | writer | producer |
--------------------------------------
| 1 | 0 | 1 | 0 |
使用具有指定列的 distinct 和另一个没有 distinct 的 select count 编写一个 select count 语句,并根据您的要求比较它们
您可以将 4 个不同的查询与 union 一起使用,每个查询必须包含 count(column name) + group by 子句