avoid repetitive sub query in postgres

I have this postgres query running fine.But need to avoid repetitive sub query after NOT IN and IN clause.=>(select video_id from "PersonalInfo_history" where "PersonalInfo_history".user_id=5)

Postgres Query: (select a.user_id,b.channel_id,b.id as unseenvideo_id from "Channel_subscribe" a INNER JOIN "Videos_videos" b ON a.channel_id=b.channel_id and b.id NOT IN(select video_id from "PersonalInfo_history" where "PersonalInfo_history".user_id=5) where a.user_id=5 GROUP BY a.user_id,b.channel_id,b.id order by b.date desc) UNION ALL (select a.user_id,b.channel_id,b.id as unseenvideo_id from "Channel_subscribe" a INNER JOIN "Videos_videos" b ON a.channel_id=b.channel_id and b.id IN(select video_id from "PersonalInfo_history" where "PersonalInfo_history".user_id=5) where a.user_id=5 GROUP BY a.user_id,b.channel_id,b.id order by b.date desc)



Read more here: https://stackoverflow.com/questions/67412966/avoid-repetitive-sub-query-in-postgres

Content Attribution

This content was originally published by shruti kulkarni at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: