0

I have this query there I want get all empids' who didn't not handle orders on February 12 , 2008

Select Distinct E.empid, lastname, firstname 
    From HR.Employees as E
    INNER JOIN Sales.Orders as O
    ON E.empid = O.empid 
    Where O.orderdate <> Convert(datetime,'02/12/2008',101)

using NOT IN, EXISTS or set operators is not applicable can somebody get me a genuine solution and explanation also

7
  • 1
    Does orderdate include the time that the order was made? Commented Jan 23, 2013 at 17:56
  • 1
    What is O.orderdate? is it a datetime? If so, you will have to convert it as well, and you should be aware that will probably kill your ability to use any index on that column Commented Jan 23, 2013 at 17:57
  • @StefanH time is 00:00:00 for every entry Commented Jan 23, 2013 at 17:59
  • @user1207217 O.orderdate is column name, there Date on which order was made and yes it's type is datetime Commented Jan 23, 2013 at 18:01
  • Is it fair to assume that the order table won't have records for a specific date where an employee didn't handle the orders? Commented Jan 23, 2013 at 18:12

4 Answers 4

1

One way to do this is to use a left outer join:

Select Distinct E.empid, lastname, firstname 
From HR.Employees as E left outer join
     Sales.Orders as O
     ON E.empid = O.empid and O.orderdate = Convert(datetime,'02/12/2008',101)
where o.orderdate is NULL

If there is a match between employees and orders on the date, then the where clause ignores those records.

This query is rephrasing your logic. You say "I want get all empids' who didn't not handle orders on February 12 , 2008". The way this works is it actually matches the employees to orders on that date. However, because it uses a left outer join, the match leaves a NULL when there is no match. These are the ones that you want.

In a sense, this query answers the equivalent question, phrased as "I want all empids that fail to match an order on such-and-such a date."

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

1 Comment

can you please explain it to me more precisely even though I've understood the concept but I can't get the whole picture why didn't my query work and this did thank you in advance
0

For Sql-Server:

I think the problem is your date format. Try using ISO format (yyyymmdd) instead. Also since you are not having any time on the right, your o.orderdate should be a date type field otherwise convert it as convert(date,O.orderdate) <> Convert(date,'20081202');

Select Distinct E.empid, lastname, firstname 
From HR.Employees as E
INNER JOIN Sales.Orders as O
ON E.empid = O.empid 
Where convert(date,O.orderdate) <> Convert(date,'20081202')

Comments

0
Select E.empid, lastname, firstname 
    From HR.Employees as E
WHERE NOT EXISTS (SELECT O.Empid FROM Sales.Orders as O
    WHERE O.empid = E.EmpID
    AND O.orderdate = '02/12/2008')

EDIT: The assumption is that the Orders table will not have records of a given date for employees who didn't handle it.

Select E.empid, lastname, firstname, MAX(O.OrderDate) 
    From HR.Employees as E LEFT JOIN Sales.Orders as O
    ON O.empid = E.EmpID
    WHERE O.orderdate = '02/12/2008'
GROUP BY E.empid, lastname, firstname
HAVING MAX(O.OrderDate) IS NULL

EDIT2: I haven't tried this. I hope you get the idea of what is being done using LEFT JOIN, MAX and HAVING.

1 Comment

I don't want to use EXISTS I want to use joins only
0

While doing only date comparisons you should use trunc. Try this

Select Distinct E.empid, lastname, firstname 
    From HR.Employees as E
    INNER JOIN Sales.Orders as O
    ON E.empid = O.empid 
    Where trunc(O.orderdate) <> trunc(TO_DATE('02/12/2008','MM/DD/YYYY'))

2 Comments

There is no need to trunc() a date that has been create with to_date() without a time part (but it doesn't hurt either). But you should specify a format mask for the to_date() call, the way you have specified it, it might fail depending on NLS settings.
@a_horse_with_no_name-thanks. Updated my answer for the NLS format.

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.