MovieID Title WatchCountIs there any way to simplify this convoluted query?
1 Gone with the Wind 10
3 Green Hornet 6
5 Superman 15
create table #Movie (
MovieID int primary key,
Format varchar(10),
WatchCount int
);
create table #Title (
MovieID int,
Title varchar(50)
);
insert #Movie values (1,'DVD',10);
insert #Movie values (2,'DVD',5);
insert #Movie values (3,'Blu-ray',6);
insert #Movie values (4,'DVD',15);
insert #Movie values (5,'Blu-ray',15);
insert #Title values (1,'Gone with the Wind');
insert #Title values (2,'Green Hornet');
insert #Title values (3,'Green Hornet');
insert #Title values (4,'Superman');
insert #Title values (5,'Superman');
select Max(m.MovieID) as MovieID,t.Title,max(m.WatchCount) as WatchCount from (
select t.Title, max(m.WatchCount) as MaxWatch
from #Movie m
inner join #Title t on t.MovieID=m.MovieID
group by t.Title
) MoviesWithMaxWatchCount
inner join #Movie m on m.WatchCount=MoviesWithMaxWatchCount.MaxWatch
inner join #Title t on t.MovieID=m.MovieID and t.Title=MoviesWithMaxWatchCount.Title
group by t.Title
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.