python - Group by column to get array results in Postgresql -
i have table called moviegenre
looks like:
moviegenre: - movie (fk movie.id) - genre (fk genre.id)
i have query (orm generated) returns movie.imdb
, genre.id
's have genre.id
's in common given movie.imdb_id
.
select "movie"."imdb_id", "moviegenre"."genre_id" "moviegenre" inner join "movie" on ( "moviegenre"."movie_id" = "movie"."id" ) ( "movie"."imdb_id" in (select u0."imdb_id" "movie" u0 inner join "moviegenre" u1 on ( u0."id" = u1."movie_id" ) ( u0."last_ingested_on" not null , not ( u0."imdb_id" in ( 'tt0169547' ) ) , not ( u0."imdb_id" in ( 'tt0169547' ) ) , u1."genre_id" in ( 2, 10 ) )) , "moviegenre"."genre_id" in ( 2, 10 ) )
the problem i'll results in format:
[ ('imdbid22`, 'genreid1'), ('imdbid22`, 'genreid2'), ('imdbid44`, 'genreid1'), ('imdbid55`, 'genreid8'), ]
is there way within query can group of genre ids list under movie.imdb_id
's? i'd grouping in query.
doing in web app code (python) extremely slow when 50k+ rows returned.
[ ('imdbid22`, ['genreid1', 'genreid2']), ('imdbid44`, 'genreid1'), ('imdbid55`, 'genreid8'), ]
thanks in advance!
edit:
here's python code runs against current results
results_list = []
for item in movies_and_genres: genres_in_common = len(set([ i['genre__id'] in movies_and_genres if i['movie__imdb_id'] == item['movie__imdb_id'] ])) imdb_id = item['movie__imdb_id'] if genres_in_common >= min_in_comon: result_item = { 'movie.imdb_id': imdb_id, 'count': genres_in_common } if result_item not in results_list: results_list.append(result_item) return results_list
select m.imdb_id, array_agg(g.genre_id) genre_id moviegenre g inner join movie m on g.movie_id = m.id m.last_ingested_on not null , not m.imdb_id in ('tt0169547') , not m.imdb_id in ('tt0169547') , g.genre_id in (2, 10) group m.imdb_id
array_agg
create array of genre_ids
of imdb_id
:
Comments
Post a Comment