SQL - Oracle

PeckersPeckers Sweden
edited January 2004 in Internet & Media
The query I'm trying to construct: What's the least popular product(s)?
SELECT FURNITURENAME, FURNITURENO, 'NO ORDERS' AS STATUS 
FROM MY_FURNITURE 
WHERE FURNITURENO NOT IN 
(SELECT FURNITURENO FROM MY_FURNITUREORDER) 
UNION 
SELECT FURNITURENAME, FURNITURENO, 'LEAST ORDERED' AS STATUS 
FROM MY_FURNITURE WHERE FURNITURENO IN 
(SELECT FURNITURENO 
FROM MY_FURNITUREORDER 
GROUP BY FURNITURENO HAVING COUNT(FURNITURENO) = 
(SELECT MIN(COUNT(FURNITURENO)) 
FROM MY_FURNITUREORDER 
GROUP BY FURNITURENO)) 
ORDER BY FURNITURENO;

First Select will return the furnitureno of furniture(s) that have never been ordered.
Second Select will return the furnitureno of the furniture(s) with the least number of orders.

This all works fine, but I only want the second Select to run if there is no furnitures with no(NULL) orders.
Any ideas?

Comments

  • a2jfreaka2jfreak Houston, TX Member
    edited January 2004
    Doesn't Oracle allow you to embed logic (forget the actual terminology for this) in the database? If so, create a conditional (if() perhaps?) and only have the second part run if there are no furnitures with NULL orders.
  • PeckersPeckers Sweden
    edited January 2004
    yeah thats what I want to do, just haven't found a way to use IF-THEN-ELSE in a SELECT statement.
    I'll have a look at CASE, that might work.
Sign In or Register to comment.