1

I have the following query

SELECT P.PRICE, P.PRODUCT_ID FROM PRODUCT P WHERE P.PRODUCT_ID IN (41,41)

I need it to return the product record twice due to limitations with the application code.

I am not too familiar with SQL so really stuck, I don't want to run single queries for each product

4 Answers 4

3

To make it appear twice, you can do:

SELECT P.PRICE, P.PRODUCT_ID FROM PRODUCT P WHERE P.PRODUCT_ID IN (41)
UNION ALL
SELECT P.PRICE, P.PRODUCT_ID FROM PRODUCT P WHERE P.PRODUCT_ID IN (41)

What the UNION operator does is it essentially "tacks" the result of one query onto the result of another... so in your case, we are "tacking" on the same exact selection so that all rows are duplicated twice in your final result.


However, what you'll probably want to do for more complex queries is use a CROSS JOIN:

SELECT P.PRICE, P.PRODUCT_ID 
FROM PRODUCT P 
INNER JOIN ...
INNER JOIN ...
CROSS JOIN
(
    SELECT NULL UNION ALL 
    SELECT NULL
) cj
WHERE P.PRODUCT_ID IN (41) AND ... AND ... AND ..

This way, rows will be duplicated as many times as there are SELECT NULL's within the CROSS JOIN subselect, and the JOIN and WHERE conditions of the main query only need to be executed once.

The nice thing about using CROSS JOIN is that you don't need to evaluate a join condition, it simply makes a Cartesian product between two sets of data.

Simply tack on the CROSS JOIN at the end of all of your main JOIN operations to manually duplicate your data.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, that worked. The query I posted was a simplified one, the actual one has joins with three tables and 5 clauses in the where statement. If I do a union the whole query needs to be duplicated as many times as number of products. Is there much overhead of doing the union?
3

Try this::

SELECT P.PRICE, P.PRODUCT_ID FROM PRODUCT P WHERE P.PRODUCT_ID=41
UNION ALL
SELECT P.PRICE, P.PRODUCT_ID FROM PRODUCT P WHERE P.PRODUCT_ID=41

1 Comment

thanks, that worked. The query I posted was a simplified one, the actual one has joins with three tables and 5 clauses in the where statement. If I do a union the whole query needs to be duplicated as many times as number of products. Is there much overhead of doing the union?
1

Another way (using UNION ALL in an internal level). Easier to combine with complex queries (as you mention in the comments):

SELECT p.price, p.product_id
FROM 
        PRODUCT AS p 
    JOIN 
        ( SELECT 41 AS product_id
        UNION ALL
          SELECT 41
        ) AS selection
      ON p.product_id = selection.product_id ;

1 Comment

You are technically still using UNION ALL ;)
0

A cartesian product (vary similar to a cross-join):

SELECT P.PRICE, P.PRODUCT_ID
FROM FROM PRODUCT P
JOIN (values(1),(2) ) AS two ON 1=1
WHERE P.PRODUCT_ID IN (42)
        ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.