user1393064
user1393064

Reputation: 411

Oracle query linking queries

My SQL query is spitting out 3000 queries when it should be spitting out 20, I'm using Oracle.

Here are the tables:

Item (itemNumber, itemName, itemDescription, itemValue, itemLocation, 
categoryID, sellerUsername)   

Auction (auctionNumber, currency, startDateTime, endDateTime, shippingTerms, 
startBidAmount, reserveAmount, bidIncrementAmount, noOfItems, itemSold, 
itemNumber feedbackDateAndTime, rating, comments, paymentDate, paymentid)

Bid (bidderUsername, auctionNumber, bidDateTime,bidAmount)

and my query

SELECT
   i.itemname,
   i.itemdescription,
   i.itemvalue,
   CASE
       WHEN i.itemnumber=a.itemnumber and a.itemSold='y' THEN 'Sold'
       WHEN a.auctionnumber != b.auctionnumber and TO_CHAR(sysdate,'DD-MON-YY')>endDateTime THEN 'No Bids on that closed auction'
       WHEN TO_CHAR(sysdate,'DD-MON-YY')<a.endDatetime and a.auctionnumber=b.auctionnumber 
                 and reserveamount>(
                 SELECT b.bidAmount
                 FROM dbf12.bid b, dbf12.auction a               
                 WHERE a.auctionnumber=b.auctionnumber 
                 GROUP BY b.bidAmount
                 HAVING b.bidAmount= max(b.bidAmount)) THEN 'No Bids that meets the reserve'
        ELSE 'Auction Still Open'
   END 
FROM 
   dbf12.item i, dbf12.auction a, dbf12.bid b;

Upvotes: 0

Views: 93

Answers (1)

Mark Roberts
Mark Roberts

Reputation: 460

It looks like you forgot the join criteria between dbf12.item, dbf12.auction, and dbf12.bid. This makes it essentially a cross product of the three tables, joining every row in each to every row in all the others.

Try something like this:

SELECT
   i.itemname,
   i.itemdescription,
   i.itemvalue,
   CASE
       WHEN i.itemnumber=a.itemnumber and a.itemSold='y' THEN 'Sold'
       WHEN a.auctionnumber != b.auctionnumber and TO_CHAR(sysdate,'DD-MON-YY')>endDateTime THEN 'No Bids on that closed auction'
       WHEN TO_CHAR(sysdate,'DD-MON-YY')<a.endDatetime and a.auctionnumber=b.auctionnumber 
                 and reserveamount>(
                 SELECT b.bidAmount
                 FROM dbf12.bid b, dbf12.auction a               
                 WHERE a.auctionnumber=b.auctionnumber 
                 GROUP BY b.bidAmount
                 HAVING b.bidAmount= max(b.bidAmount)) THEN 'No Bids that meets the reserve'
        ELSE 'Auction Still Open'
   END 
FROM 
   dbf12.item i, dbf12.auction a, dbf12.bid b
    WHERE i.itemnumber = a.itemnumber and b.actionnumber = a.auctionnumber

You can also say something like:

SELECT
   i.itemname,
   i.itemdescription,
   i.itemvalue,
   CASE
       WHEN i.itemnumber=a.itemnumber and a.itemSold='y' THEN 'Sold'
       WHEN a.auctionnumber != b.auctionnumber and TO_CHAR(sysdate,'DD-MON-YY')>endDateTime THEN 'No Bids on that closed auction'
       WHEN TO_CHAR(sysdate,'DD-MON-YY')<a.endDatetime and a.auctionnumber=b.auctionnumber 
                 and reserveamount>(
                 SELECT b.bidAmount
                 FROM dbf12.bid b, dbf12.auction a               
                 WHERE a.auctionnumber=b.auctionnumber 
                 GROUP BY b.bidAmount
                 HAVING b.bidAmount= max(b.bidAmount)) THEN 'No Bids that meets the reserve'
        ELSE 'Auction Still Open'
   END 
    from db12.item i
        inner join dbf12.auction a on a.itemnumber = i.itemnumber
        inner join dbf12.bid b on b.auctionnumber = a.auctionnumber

Upvotes: 2

Related Questions