3

So I have joined a few tables together and I am having some issues. Here is my sql:

SELECT co.orderid,
       orderdate,
       emailaddr,
       city,
       weight,
       partdescription
FROM customer c
INNER JOIN custorder co ON c.customerid = co.customerid
INNER JOIN custorderline cl ON co.orderid = cl.orderid
INNER JOIN inventorypart i ON cl.partnumber = i.partnumber
WHERE orderdate BETWEEN to_date('2015/11/01', 'yyyy/mm/dd') AND to_date('2015/11/30', 'yyyy/mm/dd')
  AND emailaddr LIKE '%gmail%'
  OR emailaddr LIKE '%outlook%'
  AND categoryid IS NOT NULL
  AND weight > 30;

here are some results:

ORDERID    ORDERDATE EMAILADDR                                          CITY                     WEIGHT PARTDESCRIPTION                                  
---------- --------- -------------------------------------------------- -------------------- ---------- --------------------------------------------------
2000000351 30-NOV-15 vacation@gmail.com                                 Walla Walla                  .5 MINI TOWER CASE                                   
2000000351 30-NOV-15 vacation@gmail.com                                 Walla Walla               34.44 EAGLE Z240 XEON E3                                
2000000354 01-DEC-15 jmuench@outlook.com                                Miami                     34.44 EAGLE Z240 XEON E3                                
2000000224 07-OCT-15 findwork@outlook.com                               Asheville                 34.44 EAGLE Z240 XEON E3                                
2000000279 01-NOV-15 analyzeh20@gmail.com                               Sumter                     .438 BOARD GAMES                                       
2000000279 01-NOV-15 analyzeh20@gmail.com                               Sumter                     .375 INTERNET SECURITY                                 
2000000298 08-NOV-15 mswatson@gmail.com                                 Ogden                     27.32 EAGLE PRETIEGE 9000                               
2000000311 12-NOV-15 syarian@gmail.com                                  Whales                       .5 2GB RADEON R7 350 VIDEO CARD                      
2000000316 16-NOV-15 mouse@gmail.com                                    Charleston                 .375 FLASH WING OPTICAL GAMING MOUSE                   
2000000320 17-NOV-15 jjones@gmail.com                                   Burns                      .188 HDMI MALE 180 DEG SWIVEL                          
2000000346 29-NOV-15 sign3@gmail.com                   

here is what I am trying to accomplish. all orders in november, emails contain gmail or outlook, part numbers must have a categoryid, parts must weight more than 30, and orderid only shows up once.

my issues are that parts weighing less than 30 are still showing up and i somehow need to make it so "If an order appears more than once;list it only once."

This is a hw question that I am stumped on... I've played with it for a long time now, keep coming back to it and think I am just thinking about it wrong. Any help or insight would be appreciated!

1
  • Why does the same order ID show up with different city and weight? Which row is correct?
    – kjmerf
    Oct 18, 2017 at 3:48

1 Answer 1

0

You need to add brackets to your WHERE clause to process "OR" logic correctly.

select co.orderid, orderdate, emailaddr, city, weight, partdescription
  from customer c
 inner join custorder co on c.customerid = co.customerid
 inner join custorderline cl on co.orderid = cl.orderid
 inner join inventorypart i on cl.partnumber = i.partnumber
 where orderdate between to_date('2015/11/01', 'yyyy/mm/dd')
   and to_date('2015/11/30', 'yyyy/mm/dd')
   and ( emailaddr like '%gmail%' or emailaddr like '%outlook%' )
   and categoryid is not null
   and weight > 30;

Also this query will not get orders later than "2015/11/30 00:00", for example it will not catch "2015/11/30 00:01". So you need to add time to format mask:

...AND to_date('2015/11/30 23:59:59', 'yyyy/mm/dd hh24:mi:ss') 

or instead of BETWEEN use

    orderdate >= to_date('2015/11/01', 'yyyy/mm/dd')
and orderdate < to_date('2015/12/01', 'yyyy/mm/dd')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.