无法通过查询从组中获取结果
Posted
技术标签:
【中文标题】无法通过查询从组中获取结果【英文标题】:couldn't get result from group by- having query 【发布时间】:2021-07-23 11:28:06 【问题描述】:我正在尝试找到最大数量的电影发布的年份,其中类型为“神秘”的电影总数。此数据库的正确答案是 2001 和 2。
下面是带有记录的数据库脚本:(要使用的表:movie、movie_geners、geners)
CREATE TABLE [dbo].[actor$](
[act_id] [float] NULL,
[act_fname] [nvarchar](255) NULL,
[act_lname] [nvarchar](255) NULL,
[act_gender] [nvarchar](255) NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[director$] Script Date: 4/30/2021 10:26:41 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[director$](
[dir_id] [float] NULL,
[dir_fname] [nvarchar](255) NULL,
[dir_lname] [nvarchar](255) NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[genres$] Script Date: 4/30/2021 10:26:41 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[genres$](
[gen_id] [float] NULL,
[gen_title] [nvarchar](255) NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[movie$] Script Date: 4/30/2021 10:26:41 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[movie$](
[mov_id] [float] NULL,
[mov_title] [nvarchar](255) NULL,
[mov_year] [float] NULL,
[mov_time] [float] NULL,
[mov_lang] [nvarchar](255) NULL,
[mov_dt_rel] [datetime] NULL,
[mov_rel_country] [nvarchar](255) NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[movie_cast$] Script Date: 4/30/2021 10:26:41 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[movie_cast$](
[act_id] [float] NULL,
[mov_id] [float] NULL,
[role] [nvarchar](255) NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[movie_direction$] Script Date: 4/30/2021 10:26:41 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[movie_direction$](
[dir_id] [float] NULL,
[mov_id] [float] NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[movie_genres$] Script Date: 4/30/2021 10:26:41 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[movie_genres$](
[mov_id] [float] NULL,
[gen_id] [float] NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[rating$] Script Date: 4/30/2021 10:26:41 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[rating$](
[mov_id] [float] NULL,
[rev_id] [float] NULL,
[rev_stars] [float] NULL,
[num_o_ratings] [float] NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[reviewer$] Script Date: 4/30/2021 10:26:41 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[reviewer$](
[rev_id] [float] NULL,
[rev_name] [nvarchar](255) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (101, N'James', N'Stewart', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (102, N'Deborah', N'Kerr', N'F')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (103, N'Peter', N'OToole', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (104, N'Robert', N'De Niro', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (105, N'F. Murray', N'Abraham', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (106, N'Harrison', N'Ford', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (107, N'Nicole', N'Kidman', N'F')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (108, N'Stephen', N'Baldwin', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (109, N'Jack', N'Nicholson', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (110, N'Mark', N'Wahlberg', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (111, N'Woody', N'Allen', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (112, N'Claire', N'Danes', N'F')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (113, N'Tim', N'Robbins', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (114, N'Kevin', N'Spacey', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (115, N'Kate', N'Winslet', N'F')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (116, N'Robin', N'Williams', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (117, N'Jon', N'Voight', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (118, N'Ewan', N'McGregor', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (119, N'Christian', N'Bale', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (120, N'Maggie', N'Gyllenhaal', N'F')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (121, N'Dev', N'Patel', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (122, N'Sigourney', N'Weaver', N'F')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (123, N'David', N'Aston', N'M')
INSERT [dbo].[actor$] ([act_id], [act_fname], [act_lname], [act_gender]) VALUES (124, N'Ali', N'Astin', N'F')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (201, N'Alfred', N'Hitchcock')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (202, N'Jack', N'Clayton')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (203, N'David', N'Lean')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (204, N'Michael', N'Cimino')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (205, N'Milos', N'Forman')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (206, N'Ridley', N'Scott')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (207, N'Stanley', N'Kubrick')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (208, N'Bryan', N'Singer')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (209, N'Roman', N'Polanski')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (210, N'Paul', N'Thomas Anderson')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (211, N'Woody', N'Allen')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (212, N'Hayao', N'Miyazaki')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (213, N'Frank', N'Darabont')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (214, N'Sam', N'Mendes')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (215, N'James', N'Cameron')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (216, N'Gus', N'Van Sant')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (217, N'John', N'Boorman')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (218, N'Danny', N'Boyle')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (219, N'Christopher', N'Nolan')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (220, N'Richard', N'Kelly')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (221, N'Kevin', N'Spacey')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (222, N'Andrei', N'Tarkovsky')
INSERT [dbo].[director$] ([dir_id], [dir_fname], [dir_lname]) VALUES (223, N'Peter', N'Jackson')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1001, N'Action')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1002, N'Adventure')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1003, N'Animation')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1004, N'Biography')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1005, N'Comedy')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1006, N'Crime')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1007, N'Drama')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1008, N'Horror')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1009, N'Music')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1010, N'Mystery')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1011, N'Romance')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1012, N'Thriller')
INSERT [dbo].[genres$] ([gen_id], [gen_title]) VALUES (1013, N'War')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (901, N'Vertigo', 1958, 128, N'English', CAST(N'1958-08-24T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (902, N'The Innocents', 1961, 100, N'English', CAST(N'1962-02-19T00:00:00.000' AS DateTime), N'SW')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (903, N'Lawrence of Arabia', 1962, 216, N'English', CAST(N'1962-12-11T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (904, N'The Deer Hunter', 1978, 183, N'English', CAST(N'1979-03-08T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (905, N'Amadeus', 1984, 160, N'English', CAST(N'1985-01-07T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (906, N'Blade Runner', 1982, 117, N'English', CAST(N'1982-09-09T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (907, N'Eyes Wide Shut', 1999, 159, N'English', NULL, N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (908, N'The Usual Suspects', 1995, 106, N'English', CAST(N'1995-08-25T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (909, N'Chinatown', 1974, 130, N'English', CAST(N'1974-08-09T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (910, N'Boogie Nights', 1997, 155, N'English', CAST(N'1998-02-16T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (911, N'Annie Hall', 1977, 93, N'English', CAST(N'1977-04-20T00:00:00.000' AS DateTime), N'USA')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (912, N'Princess Mononoke', 1997, 134, N'Japanese', CAST(N'2001-10-19T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (913, N'The Shawshank Redemption', 1994, 142, N'English', CAST(N'1995-02-17T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (914, N'American Beauty', 1999, 122, N'English', NULL, N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (915, N'Titanic', 1997, 194, N'English', CAST(N'1998-01-23T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (916, N'Good Will Hunting', 1997, 126, N'English', CAST(N'1998-06-03T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (917, N'Deliverance', 1972, 109, N'English', CAST(N'1982-10-05T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (918, N'Trainspotting', 1996, 94, N'English', CAST(N'1996-02-23T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (919, N'The Prestige', 2006, 130, N'English', CAST(N'2006-11-10T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (920, N'Donnie Darko', 2001, 113, N'English', NULL, N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (921, N'Slumdog Millionaire', 2008, 120, N'English', CAST(N'2009-01-09T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (922, N'Aliens', 1986, 137, N'English', CAST(N'1986-08-29T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (923, N'Beyond the Sea', 2004, 118, N'English', CAST(N'2004-11-26T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (924, N'Avatar', 2009, 162, N'English', CAST(N'2009-12-17T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (926, N'Seven Samurai', 1954, 207, N'Japanese', CAST(N'1954-04-26T00:00:00.000' AS DateTime), N'JP')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (927, N'Spirited Away', 2001, 125, N'Japanese', CAST(N'2003-09-12T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (928, N'Back to the Future', 1985, 116, N'English', CAST(N'1985-12-04T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (925, N'Braveheart', 1995, 178, N'English', CAST(N'1995-09-08T00:00:00.000' AS DateTime), N'UK')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (100, N'Bollywood', 1993, 93, N'Hindi', CAST(N'2021-01-01T00:00:00.000' AS DateTime), N'India')
INSERT [dbo].[movie$] ([mov_id], [mov_title], [mov_year], [mov_time], [mov_lang], [mov_dt_rel], [mov_rel_country]) VALUES (101, N'A 2001 Movie', 2001, 150, N'English', CAST(N'2001-09-01T00:00:00.000' AS DateTime), N'USA')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (101, 901, N'John Scottie Ferguson')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (102, 902, N'Miss Giddens')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (103, 903, N'T.E. Lawrence')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (104, 904, N'Michael')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (105, 905, N'Antonio Salieri')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (106, 906, N'Rick Deckard')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (107, 907, N'Alice Harford')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (108, 908, N'McManus')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (110, 910, N'Eddie Adams')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (111, 911, N'Alvy Singer')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (112, 912, N'San')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (113, 913, N'Andy Dufresne')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (114, 914, N'Lester Burnham')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (115, 915, N'Rose DeWitt Bukater')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (116, 916, N'Sean Maguire')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (117, 917, N'Ed')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (118, 918, N'Renton')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (120, 920, N'Elizabeth Darko')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (121, 921, N'Older Jamal')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (122, 922, N'Ripley')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (114, 923, N'Bobby Darin')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (109, 909, N'J.J. Gittes')
INSERT [dbo].[movie_cast$] ([act_id], [mov_id], [role]) VALUES (119, 919, N'Alfred Borden')
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (201, 901)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (202, 902)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (203, 903)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (204, 904)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (205, 905)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (206, 906)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (207, 907)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (208, 908)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (209, 909)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (210, 910)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (211, 911)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (212, 912)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (213, 913)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (214, 914)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (215, 915)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (216, 916)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (217, 917)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (218, 918)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (219, 919)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (220, 920)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (218, 921)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (215, 922)
INSERT [dbo].[movie_direction$] ([dir_id], [mov_id]) VALUES (221, 923)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (922, 1001)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (917, 1002)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (903, 1002)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (912, 1003)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (911, 1005)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (908, 1006)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (913, 1006)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (926, 1007)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (928, 1007)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (918, 1007)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (921, 1007)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (902, 1008)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (923, 1009)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (907, 1010)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (927, 1010)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (901, 1010)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (914, 1011)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (906, 1012)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (904, 1013)
INSERT [dbo].[movie_genres$] ([mov_id], [gen_id]) VALUES (101, 1010)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (901, 9001, 8.4, 263575)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (902, 9002, 7.9, 20207)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (903, 9003, 8.3, 202778)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (906, 9005, 8.2, 484746)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (924, 9006, 7.3, NULL)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (908, 9007, 8.6, 779489)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (909, 9008, NULL, 227235)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (910, 9009, 3, 195961)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (911, 9010, 8.1, 203875)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (912, 9011, 8.4, NULL)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (914, 9013, 7, 862618)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (915, 9001, 7.7, 830095)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (916, 9014, 4, 642132)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (925, 9015, 7.7, 81328)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (918, 9016, NULL, 580301)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (920, 9017, 8.1, 609451)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (921, 9018, 8, 667758)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (922, 9019, 8.4, 511613)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (923, 9020, 6.7, 13091)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (101, 9001, 9, 50000)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (927, 9001, 8, 45000)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (920, 9001, 7, 46000)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (907, 9001, 9, 50000)
INSERT [dbo].[rating$] ([mov_id], [rev_id], [rev_stars], [num_o_ratings]) VALUES (914, 9001, 7, 45000)
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9001, N'Righty Sock')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9002, N'Jack Malvern')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9003, N'Flagrant Baronessa')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9004, N'Alec Shaw')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9005, NULL)
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9006, N'Victor Woeltjen')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9007, N'Simon Wright')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9008, N'Neal Wruck')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9009, N'Paul Monks')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9010, N'Mike Salvati')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9011, NULL)
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9012, N'Wesley S. Walker')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9013, N'Sasha Goldshtein')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9014, N'Josh Cates')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9015, N'Krug Stillo')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9016, N'Scott LeBrun')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9017, N'Hannah Steele')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9018, N'Vincent Cadena')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9019, N'Brandt Sponseller')
INSERT [dbo].[reviewer$] ([rev_id], [rev_name]) VALUES (9020, N'Richard Adams')
USE [master]
GO
ALTER DATABASE [movie] SET READ_WRITE
GO
下面是我尝试运行的查询:
SELECT
movie.mov_year, COUNT(movie.mov_year) AS 'total_movies'
FROM
movie$ movie
JOIN
movie_genres$ m_geners ON m_geners.mov_id = movie.mov_id
JOIN
genres$ geners ON geners.gen_id = m_geners.gen_id
AND geners.gen_title LIKE '%mystery%'
GROUP BY
movie.mov_year
HAVING
movie.mov_year IN
(SELECT
mov_year mov_yr
FROM
movie$ m
GROUP BY
mov_year
HAVING
COUNT([mov_year]) =
(SELECT
MAX(a.movie_count) maxcount
FROM
(SELECT
COUNT([dbo].[movie$].[mov_year]) movie_count
FROM
[dbo].[movie$]
GROUP BY
movie$.mov_year
) AS a
)
)
我没有得到任何结果。 请建议我应该在查询中进行哪些更新。
【问题讨论】:
我建议使用较短的表别名,它们旨在使您的查询更清晰。就我个人而言,我会将 Movies 别名为 M,将 Genres 别名为 G,将 MovieGenres 别名为 MG。此外,如果您可以确保提供minimal reproducible example 并强调最小化。您提供了大量不必要的数据,这使得查看实际问题变得更加困难。如果结果只需要 3 个表,则只显示 3 个表。 感谢@DaleK 我非常感谢您的指导。 TOP WITH TIES 工作。 【参考方案1】:这样的东西可以完成这项工作吗?
SELECT TOP(1) movie.mov_year, COUNT(movie.mov_year) AS 'total_movies'
FROM
movie$ movie
JOIN movie_genres$ m_geners ON m_geners.mov_id = movie.mov_id
JOIN genres$ geners ON geners.gen_id = m_geners.gen_id
WHERE geners.gen_title LIKE '%mystery%'
GROUP BY movie.mov_year
ORDER BY COUNT(movie.mov_year) desc
【讨论】:
感谢您抽出宝贵时间,沃伦。当电影数量最多只有一年时,此查询有效。如果有超过一年的最大电影数量相同,我可能不会使用 TOP,因为我希望所有年份都具有相同类型的最大电影数量。请问还有什么建议吗? @Deva 除了在这里寻求帮助之外,我鼓励您花一些时间进行自己的研究。在这种特殊情况下,读取documentation for TOP 表明有一个参数WITH TIES
,顾名思义,当它们具有相同的值时显示多行。
@Deva 你可以随时更新这个答案,因为它让你走上了正确的轨道。【参考方案2】:
我能够按照 Dale K 的建议使用 TOP WITH TIES 检索结果。 以下是查询:
SELECT TOP(1) WITH TIES mov_count,mov_year,rev_stars AS avg_rev_stars
FROM
(
SELECT COUNT(m.mov_year) mov_count, m.mov_year
,AVG(ISNULL(r.rev_stars,0)) rev_stars
FROM movie$ m
INNER JOIN movie_genres$ mg on m.mov_id=mg.mov_id
INNER JOIN genres$ g on mg.gen_id=g.gen_id
LEFT JOIN dbo.rating$ r on m.mov_id = r.mov_id
GROUP BY m.mov_year, gen_title
HAVING gen_title='Mystery'
) AS t1
ORDER BY t1.mov_count desc
结果:
【讨论】:
以上是关于无法通过查询从组中获取结果的主要内容,如果未能解决你的问题,请参考以下文章