Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8585637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:06:56+00:00 2026-06-11T22:06:56+00:00

In the UNION query below, subqueries #2 and #4 are identical, and I would

  • 0

In the UNION query below, subqueries #2 and #4 are identical, and I would like to avoid fetching the same data twice. From what I know, some databases allow you to write a WITH clause at the beginning of the query, but SQLite does not support it. So the question is how to optimize the query below.

 SELECT disliked, recommended, weight
 FROM
   (SELECT movie_id * 10 AS disliked
      FROM ratings
     WHERE rating IN (1,2)
       AND user_id = #{id}
   ) AS input,
   (SELECT movie_id AS recommended, rating AS weight
      FROM ratings
     WHERE rating IN (4,5)
       AND user_id = #{id}
   ) AS output
 UNION
 SELECT liked, recommended, weight
 FROM
   (SELECT movie_id, movie_id * 10 + 1 AS liked
      FROM ratings
     WHERE rating IN (4,5)
       AND user_id = #{id}
   ) AS input,
   (SELECT movie_id AS recommended, rating AS weight
      FROM ratings
     WHERE rating IN (4,5)
       AND user_id = #{id}
   ) AS output
 WHERE input.movie_id != recommended
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:06:58+00:00Added an answer on June 11, 2026 at 10:06 pm

    I’m not entirely sure I’ve read you right this fast, but maybe something like this:

     SELECT
         10*r1.movie_id+(r1.rating IN (4,5)) AS likedisliked,
         r2.movie_id AS recommended,
         r2.rating AS weight
     FROM ratings AS r1, ratings AS r2 
     WHERE r1.user_id=#{id} AND r2.user_id=#{id}
           AND r1.rating IN (1,2,4,5) AND r2.rating IN (4,5)
           AND r1.movie_id!=r2.movie_id; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the query below which uses a union to pull data from three
I have a query like this: select author1 from books_group where category='cse' union select
The below query is giving error: SELECT DISTINCT dealer_state FROM A WHERE country='SS' UNION
We have a Union Query. Here's a basic (similar) example: SELECT a.Name, b.Info FROM
I have an sqlite union query: SELECT term,abstract, termid, nterm FROM (SELECT term, subterm,
Why this query doesn’t working: SELECT name FROM ( SELECT name FROM table1 UNION
Take the following query: select * from ( select a, b from c UNION
I have a query like below that has groups (COL1) and that group's values
I'm using SQL Server 2005. With the query below (simplified from my real query):
Below is my query to get a bulk of attributes of some objects. Are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.