Oracle SQL Developer Query

I am trying to write a query where I pull values from one table where it cant find values in another table with certain criteria

so first table i want

select ordernumber, created_dt, customerID from tbl_customer
where (there isnt an entry in customer comments table with the same ordernumber, created on same date, and with the same customerID in tbl_customerComments as what is selected from tbl_customer)

so for each entry in this query select ordernumber, created_dt, customerID from tbl_customer i want to look in the tbl_customerComments table and see if there is a matching entry with same details, if not then I generate the list.
dkilbyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
This query takes all records in tbl_customer and matches those to records in tbl_customerComments, then the WHERE clause  only allows those without a match to be listed.
SELECT
      C.ordernumber
    , C.created_dt
    , C.customerID
FROM tbl_customer C
LEFT JOIN tbl_customerComments CC
      ON C.ordernumber = CC.ordernumber
      AND C.customerID = CC.customerID
      AND C.created_dt = CC.created_dt
WHERE CC.ordernumber IS NULL
;

Open in new window


HOWEVER, you have asked for a match involving a date column. This may be difficult if the information is precise to milliseconds and information that appears to be "the same date" are in fact very slightly different as far as the database is concerned.

e.g.
if a date in customers is                  2015-07-01 09:08:10 +12345
and then a date in comments is    2015-07-01 09:08:10 +23456

those 2 dates are NOT equal, but if you only "see" the date portion (2015-07-01) they look the same
dkilbyAuthor Commented:
is there a way to compare the created dates at the mm/dd/yyyy level as it makes sense not to explain at exact level as it will be different entries
PortletPaulEE Topic AdvisorCommented:
Yes there is, use TRUNC(),  but it makes the query less efficient.
SELECT
      C.ordernumber
    , C.created_dt
    , C.customerID
FROM tbl_customer C
LEFT JOIN tbl_customerComments CC
      ON C.ordernumber = CC.ordernumber
      AND C.customerID = CC.customerID
      AND trunc(C.created_dt) = trunc(CC.created_dt)
WHERE CC.ordernumber IS NULL
;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

dkilbyAuthor Commented:
thank you very much - works perfectly
PortletPaulEE Topic AdvisorCommented:
No problem, thanks for the quick resolution.

TRUNC() is a very handy function for date handling (amongst the other things it can do)
johnsoneSenior Oracle DBACommented:
Just to clarify.

If the column is of type DATE, then it only has seconds, not subseconds.

If the column is of type TIMESTAMP, then it can have subseconds.  The number of decimal places a TIMESTAMP holds is defined at create time.

The TRUNC function works with either datatype and does what has already been described.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.