1

I have two tables (tblBestsellers, tblAllsellers).

  • tblAllsellers includes all the books in the shop
  • tblBestsellers includes only the ones that have sold over their quota.

The only link between them is their productID.

Is it possible to add a column with the title Bestseller at runtime? Using a query in which will show if that book was a best seller or not such as

ProductID   - Product Name  - Bestseller   
----------------------------------------
324234        Harry Potter        Y
382932        LOTHR               Y
234292        SQL Guide           N

and so on.

I suspect there is an IF statement there somewhere??

SELECT *
FROM tblAllsellers, tblbestsellers
1
  • 1
    Which database system do you use (SQLServer, MySQL, Oracle, etc.)? Commented Dec 3, 2012 at 14:56

2 Answers 2

2

You should perform a LEFT JOIN of tblAllSellers with tblBestSellers on ProductID. If there is a record in the tblBestSellers for a given ProductID, it means that is a best seller and otherwise not. So, you can use this column in the CASE statement for that last column, BestSeller.

SELECT 
    A.*, 
    CASE 
        WHEN B.ProductId IS NULL THEN 'N' 
        ELSE 'Y' 
    END 'BestSeller'
FROM 
    tblAllSellers A
    LEFT JOIN tblBestSellers B
    ON A.ProductId = B.ProductID

You would use this in another query with JOINs as follows:

 SELECT A.* 
 FROM 
     AnotherTable X
     JOIN 
     (
        SELECT 
            A.*, 
            CASE 
                WHEN B.ProductId IS NULL THEN 'N' 
                ELSE 'Y' 
            END 'BestSeller'
        FROM 
            tblAllSellers A
            LEFT JOIN tblBestSellers B
            ON A.ProductId = B.ProductID
     ) AS Y
     ON X.ProductId = Y.ProductID

If you want to join the results of this query with those from another query, then you would do it as follows:

 SELECT A.ProductId, A.COlumn1, A.Column2 ...
 FROM 
     AnotherTable X

 UNION 
 SELECT Y.ProductId, Y.Column1, Y.Column2..
 FROM
 (
 SELECT 
      A.*, 
      CASE 
           WHEN B.ProductId IS NULL THEN 'N' 
           ELSE 'Y' 
      END 'BestSeller'
 FROM 
      tblAllSellers A
      LEFT JOIN tblBestSellers B
      ON A.ProductId = B.ProductID
 ) AS Y

For the UNION scenario, make sure the number, data type of the columns in both the SELECT statements are the same.

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

3 Comments

Thats great, so how can I combine this query to another query that has lots of inner joins? I tried UNION but I get the error message 'all queries combined using UNION, INTERSECT or EXCEPT must have an equal number of expressions in their target lines'
Enclose this query in parenthesis and give an alias like (<the above query) AS A and JOIN it with other tables appropriately. Then you can refer to the columns returned by the query as A.<columnName>. E.g. A.BestSeller.
see above update to the code (still getting an error). Thanks in advance.
0

You can better use a Case statement.

 SELECT A.*, CASE WHEN B.ProductId IS NULL THEN 'N' ELSE 'Y' END 'BestSeller'
 FROM 
tblAllSellers A, tblBestSellers B
where A.ProductId = B.ProductID

1 Comment

Many thanks for all your help but have noticed that the CASE statement is either giving all 'N's or all 'Y's and not correctly comparing the two table columns. Would it make a difference if the coloumns in different tables have different names at all?

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.