-1

I have a query with two where clauses. Example code:

   SELECT CONCAT(E.Firstname, ' ', 
            E.Middlename, ' ', 
            E.Lastname, ' ', 
            E.Maidenname, 
            ' (', E.Abbreviation, ')') AS Technician,
       'White' AS Backcolor
FROM Employee AS E
WHERE E.Empkey NOT IN (
                          SELECT Empkey
                          FROM Taskassignation AS Ta
                            INNER JOIN Task AS T ON T.Taskassignationid = Ta.Taskassignationid
                          WHERE T.Taskstatusid != '09E02513-00AD-49E3-B442-A9ED2833FB25'
                                AND T.Taskstatusid != '60F68A00-A212-4B7A-B0A3-620B5887136C'
                                AND Empkey IS NOT NULL
                                AND Terminationdate IS NULL
                          GROUP BY Empkey
                                         )
                          AND Departmentkey = 3
                          AND Terminationdate IS NULL

-- Now at the end of my second where I add two conditions:

                          WHERE T.Taskstatusid != '09E02513-00AD-49E3-B442-A9ED2833FB25' 
                          AND T.Taskstatusid != '60F68A00-A212-4B7A-B0A3-620B5887136C'
                          AND Empkey IS NOT NULL
                          AND Terminationdate IS NULL
                          AND E.Title = 'Project Technician'
                          AND E.Title = 'Project Mgr';

For some reason query don't care my last conditions, it just get all values with all titles instead get only titles that already declared. I need to specify that filter different if I have two where clauses? Any suggestions?

Kind Regards.

3
  • Hey, Pepe, welcome to Stack Overflow! I would suggest that you edit your question to include sample data, the output that results from that sample data, and the desired output for that sample data. See How to Ask and minimal reproducible example for further details.
    – Brian
    Commented Sep 3, 2018 at 21:38
  • I don't understand the question well. But, did you try adding the where clause AND [E].[Title] = 'Project Technician' AND [E].[Title] = 'Project Mgr after the end of the outer query where clause
    – Nachi
    Commented Sep 3, 2018 at 22:05
  • It is an internal query, not a join, it does not know [E]
    – DanielM
    Commented Sep 3, 2018 at 23:46

1 Answer 1

1

Let's look just at your != comparisons. The inner select returns all the records that are not equal to '09E...' and not equal to '60F...'

Then you ask for all the records that are NOT in that list. So every record in this result must have either '09E...' or '60F...'

Then you add a WHERE clause at the end that excludes all the records that are equal to '09E...' and '60F...'. This leaves you with nothing.

Consider this example:

create table Emp ( ID varchar(30), Name varchar(30) )

insert into Emp ( ID, Name ) values
('314', 'Peter'),
('09E', 'Fred' ),
('ELD', 'Wally'),
('60F', 'Mary' ),
('AOK', 'Alice')

-- List rows other than IDs 09E and 60F
select * from Emp where ID != '09E' and ID !='60F'

-- Result is:
-- ID   Name
-- 314  Peter
-- ELD  Wally
-- AOK  Alice

-- List rows that are NOT in the result set shown above

select * from Emp 
  where ID not in ( select ID from Emp where ID != '09E' and ID !='60F' )

-- Result is:
-- ID   Name
-- 09E  Fred
-- 60F  Mary

-- List all the rows above, but exclude 09E and 60F

select * from Emp 
  where ID not in ( select ID from Emp where ID != '09E' and ID !='60F' )
    and ID != '09E' and ID != '60F'

-- Result is empty
-- You've excluded all the records from the last results. 

Also you need to be careful adding two terms of the form X=A and X=B

create table Job ( Position varchar(30) )

insert into Job ( Position ) values
( 'CEO'         ),
( 'Technician'  ),
( 'Electrician' ),
( 'Manager'     ),
( 'Secretary'   )

select * from Job 
where Position = 'Technician'
  and Position = 'Manager'

-- Result set is empty

You might think the result should have two rows. But consider the second row. Position = 'Technician' is true, but Position = 'Manager' is false. But you ask only for records where the first is true, and the second is also true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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