SQL:计数为1时显示值

时间:2017-04-21 15:19:50

标签: sql

我有一个查询来显示只有1种语言版本的项目:

  SELECT COUNT(Language), 
         Name
    FROM Fields
GROUP BY Name
  HAVING COUNT(Language) = 1
ORDER BY Name

因为在这种情况下语言是唯一的,我希望将语言作为输出中的列。向GROUP BY添加语言不能做我想要的:( 其他方法仅显示来自特定语言的项目,但也显示COUNT(语言)= 1。

2 个答案:

答案 0 :(得分:2)

使用其他聚合函数(可以是MINMAX):

  SELECT MIN(Language) Language, 
         Name
    FROM Fields
GROUP BY Name
  HAVING COUNT(DISTINCT Language) = 1
ORDER BY Name;

答案 1 :(得分:0)

其他选择:

select f.Language, f.Name
from Fields f
where (select count(1) from Fields f2 where f2.Name = f.Name) = 1
order by f.Name

SELECT  (select Language from fields f2 where f2.Name = f.Name) Language,
         f.Name
FROM Fields f
GROUP BY f.Name
HAVING COUNT(f.Language) = 1
ORDER BY f.Name