sql - 聚合最大函数和分组依据

时间:2016-01-29 09:00:12

标签: mysql sql

select city, title, releasedate
from movies join shownat on shownat.movietitle = movies.title join theatres on theatres.theatrename = shownat.theatrename
group by city, title, releasedate
order by city, max(releasedate) desc;

以上是我的查询,这个问题要解决。

查找每个城市中显示的最新电影的标题。显示按城市名称和电影标题排序的城市名称和最新电影标题。

标准化数据有3个表格,分别是剧院,电影和电影。是的,我意识到发布日期可以在展示桌上更有效,对我来说它更有意义,因为并非总是视频总是在相同的地区或城市同时发布。然而,这是我最后的作业问题,我坚持。 我们需要的是该城市应该只有1个列表。我认为按城市分组会照顾这一点。对于每个城市,我需要在数据集中的每个特定城市(4)中具有最新发布日期的电影的标题。我不确定为什么我在这里获得重复项,因为我有这个功能组以及它用于处理聚合最大函数。最大功能应该给我最新版本是吗?

"CITY"          "TITLE"        "RELEASEDATE"
"Cincinnati"    "Interstellar"  07-NOV-14
"Cincinnati"    "Big Hero 6"    07-NOV-14
"Cincinnati"    "Nightcrawler"  31-OCT-14
"Cincinnati"    "Gone Girl"     03-OCT-14
"Cincinnati"    "The Pianist"   03-JAN-03
"Cincinnati"    "Fargo" 05-APR-96
"Cincinnati"    "Schindler's List"  04-FEB-94
"Florence"      "Big Hero 6"    07-NOV-14
"Florence"      "Interstellar"  07-NOV-14
"Florence"      "Nightcrawler"  31-OCT-14
"Florence"  "Gone Girl" 03-OCT-14
"Florence"  "District 9"    14-AUG-09
"Florence"  "A Perfect Getaway" 07-AUG-09
"Florence"  "Aliens in the Attic"   31-JUL-09
"Florence"  "Away We Go"    26-JUN-09
"Florence"  "Up"    29-MAY-09
"Florence"  "Star Trek" 08-MAY-09
"Florence"  "The Hurt Locker"   10-OCT-08
"Florence"  "The Dark Knight"   18-JUL-08
"Florence"  "The Departed"  06-OCT-06
"Florence"  "The Green Mile"    10-DEC-99
"Newport"   "Interstellar"  07-NOV-14
"Newport"   "Big Hero 6"    07-NOV-14
"Newport"   "Gone Girl" 03-OCT-14
"Newport"   "District 9"    14-AUG-09
"Newport"   "A Perfect Getaway" 07-AUG-09
"Newport"   "Away We Go"    26-JUN-09
"Newport"   "Up"    29-MAY-09
"Newport"   "The Departed"  06-OCT-06
"Wilder"    "Big Hero 6"    07-NOV-14
"Wilder"    "Interstellar"  07-NOV-14
"Wilder"    "Gone Girl" 03-OCT-14
"Wilder"    "Public Enemies"    01-JUL-09
"Wilder"    "The Departed"  06-OCT-06

2 个答案:

答案 0 :(得分:1)

这将是查询,但我不太确定max函数是否适用于日期变量:

SELECT city, title, max (releasedate) as max_dateRelease
FROM movies inner join shownat on shownat.movietitle = movies.title join theatres 
on theatres.theatrename = shownat.theatrename
GROUP BY city, title, releasedate
ORDER BY city, max_dateRelease desc

答案 1 :(得分:0)

select T1.city,T2.movietitle,T1.releasedate
from
(select city,max(releasedate) as maxreleasedate
from movies join shownat on shownat.movietitle = movies.title join theatres on theatres.theatrename = shownat.theatrename
group by city) as T1
inner join
(select shownat.movietitle,shownat.city,shownat.releasedate)
from shownat) as T2
 on T1.city=T2.city and T1.maxreleasedate=T2.releasedate

这是一种方式而不是最佳方式。