1

I have a problem with my SQL command. I want to select 2 movies which 2 movies sum of durations less than 150 I wrote this SQL command:

Select
   movie_title,Sum(movie_time) as sum_movie
From
  movie_movie
Group By
  movie_title
Having 
  Sum(movie_time)<100
Order By
  sum_movie DESC
  • 1
    What does your query do wrongly other than the typo of using 100 instead of 150? – drescherjm May 20 '22 at 15:58
  • For the limit of 2 rows you may want: [https://stackoverflow.com/questions/13674031/how-to-get-the-top-10-values-in-postgresql](https://stackoverflow.com/questions/13674031/how-to-get-the-top-10-values-in-postgresql) – drescherjm May 20 '22 at 16:00

1 Answers1

0

You can get two movies with minimum movie_time values ​​with order by movie_time ASC limit 2 in CTE, and then use that in the condition.

with  two_min_movie as (
  select * 
  from movie_movie 
  order by movie_time ASC limit 2
)
select * 
from two_min_movie 
where (select sum(movie_time) from two_min_movie) < 150

Demo in DBfiddle

emrdev
  • 2,155
  • 3
  • 9
  • 15