2016-10-31 59 views
0

我试图找到一个语句来选择唯一值。 不像独特/独特,导致这些只是删除重复。我想获得所有唯一值的列表,只有一个条目。SQL找到唯一值

例如:

值:1,2,3,4,4,5,5,6,

我想获得:1,2,3,6,

编辑:

问题是: 什么角色(电影的名称和数量)在超过199部电影中扮演角色与独特的角色名称?

这些都是我的表:

Table "public.filmparticipation" 
    Column | Type | Modifiers 
----------+---------+----------- 
partid | integer | 
personid | integer | not null 
filmid | integer | not null 
parttype | text | not null 


Table "public.filmcharacter" 
    Column  | Type | Modifiers 
---------------+---------+----------- 
partid  | integer | 
filmcharacter | text | 
billingpos | integer | 


Table "public.person" 
    Column |  Type  | Modifiers 
-----------+--------------+----------- 
personid | integer  | 
lastname | text   | not null 
firstname | text   | 
gender | character(1) | 

这是我迄今为止尝试过,虽然我不甚至接近一个解决方案,我认为:

SELECT p.firstname, COUNT(fp.filmid) 
FROM person p INNER JOIN filmparticipation fp 
ON p.personid = fp.personid 
INNER JOIN filmcharacter fc 
ON fc.partid = fp.partid 
GROUP BY p.firstname 
HAVING COUNT(fc.filmcharacter) = 1; 

谢谢。

+0

使用SELECT DISTINCT – user1929959

回答

4

一个简单的方法是使用group byhaving

select val 
from t 
group by val 
having count(*) = 1; 
+0

Okey,这有点帮助。但是,我想找到什么值(人)有超过199个唯一值(邮票)。我可以用另一个计数来找到超过199的人吗? – Br0dskive

+0

@ Br0dskive您能否提供更详细的样本和结果以反映这一新要求? – JohnHC

+0

@JohnHC我更新了更多细节问题:) – Br0dskive

0

你要计算每个演员和人物片,所以你必须通过这两个组。

select p.personid, p.firstname, p.lastname, fc.filmcharacter, count(distinct fp.filmid) 
from person p 
join filmparticipation fp on fp.personid = p.personid 
join filmcharacter fc on fc.partid = fp.partid 
group by p.personid, p.firstname, p.lastname, fc.filmcharacter 
having count(distinct fp.filmid) > 199; 

即使你只在至少200部电影起到了一些作用演员感兴趣的(即无论哪个角色,或者如果只有一个角色或一个以上),你要做的第一件相同,然后将每个演员的独特行烧开:

select distinct p.personid, p.firstname, p.lastname 
from person p 
join filmparticipation fp on fp.personid = p.personid 
join filmcharacter fc on fc.partid = fp.partid 
group by p.personid, p.firstname, p.lastname, fc.filmcharacter 
having count(distinct fp.filmid) > 199;