【问题标题】:SQL Count a number of rowsSQL 计算行数
【发布时间】:2013-03-05 20:37:18
【问题描述】:

我有以下:

SELECT nominees.personname, awards.name as awards, nominees.filmname 
FROM nominees, awards WHERE nominees.aid = awards.aid and nominees.personname is not 
NULL GROUP BY nominees.personname, awards.name, nominees.filmname 
ORDER BY nominees.personname;

这会产生下表:

"personname"    "awards"        "filmname"

"Ang Lee"       "director"      "Life of Pi"
"Anglelina J."  "actress"       "The Tourist"
"Ann Dowd"   "supporting actress"   "Compliance"
"Anne Hathaway" "actress"      "Love and Other Drugs"
"Anne Hathaway" "supporting actress"  "Les Misrables"
"Annette Bening"    "actress"   "The Kids Are All Right"
"Another Year"  "screenplay"    "Mike Leigh"
"A.R. Rahman"     "score"       "127 Hours"
"AR Rahman"       "score"       "127 Hours"
"Barbara Hershey"   "supporting actress"    "Black Swan"
"Ben Affleck"   "actor"         "Argo"
"Ben Affleck"   "director"      "Argo"

我正在尝试获取仅包含因同一部电影获得两个单独奖项提名的人的集合。在这张只有“Ben Affleck”的特殊表格中。

结果应该是

    "personname"    "awards"        "filmname"

    "Ben Affleck"   "actor"         "Argo"
    "Ben Affleck"   "director"      "Argo"

但我似乎无法理解,我尝试使用 HAVING 和某种计数方法,但无法使其正常工作。任何帮助表示赞赏。

【问题讨论】:

  • 你能提供更多关于提名和奖项的表格结构的信息吗?

标签: sql postgresql count


【解决方案1】:

这是一个简单的聚合查询。要让这个人这样做:

select personname, filmname
from (SELECT nominees.personname, awards.name as awards, nominees.filmname 
      FROM nominees, awards WHERE nominees.aid = awards.aid and nominees.personname is not NULL
      GROUP BY nominees.personname, awards.name, nominees.filmname 
     ) t
 group by personname, filmname
 having count(*) > 1

您可以根据原始数据来表达这一点,并在一行中获取列表:

SELECT nominees.personname, nominees.filmname, array_agg(awards.name) as listOfaward
FROM nominees join
     awards
     on nominees.aid = awards.aid
where nominees.personname is not NULL
GROUP BY nominees.personname, nominees.filmname 
having COUNT(distinct  awards.name) > 1

要为每个奖项获取单独的行,您可以连接回原始数据:

select personname, filmname, a.name
from (SELECT nominees.personname, nominees.filmname, array_agg(awards.name) as listOfaward
      FROM nominees join
           awards
           on nominees.aid = awards.aid
      where nominees.personname is not NULL
      GROUP BY nominees.personname, nominees.filmname 
      having COUNT(distinct  awards.name) > 1
     ) l join
     nominees n
     on n.personname = l.personname and n.filmname = l.filmname join
     awards a
     on n.aid = a.aid

【讨论】:

  • 另一种方式 - COUNT(distinct awards.name) 作为窗口函数
  • 第三个建议正是我想要的,我已经完成了第一个建议,但找不到将每个奖项放在单独一行中的好方法。谢谢。
【解决方案2】:

我认为它应该像这样工作:

SELECT nominees.personname, awards.name as awards, nominees.filmname, count(distinct  awards.name) as number_awards
FROM nominees, awards WHERE nominees.aid = awards.aid and nominees.personname is not 
NULL 
GROUP BY nominees.personname, awards.name, nominees.filmname 
HAVING number_awards > 1
ORDER BY nominees.personname;

【讨论】:

  • 它不会工作。 count(distinct awards.name) 将始终为 1,因为您按 awards.name 分组
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多