Solved

SQL Query select  where field1 = x and field1 = y

Posted on 2013-05-14
9
204 Views
Last Modified: 2013-10-04
I need to be able to select EmployeeID, Employee Name from a table where the same field will have a description Sales or Sales Stocker.   The employee has two entries.   If I use an and i don't get anything returned if I use or i get everything.  I just need the employees that have both.
0
Comment
Question by:smhorton
9 Comments
 
LVL 2

Expert Comment

by:Psihawk99
ID: 39164982
Can you post the query you are attempting to run?
0
 
LVL 25

Expert Comment

by:Ron M
ID: 39165004
Try this...

SELECT DISTINCT EmployeeID, Employee_Name from TableName
WHERE Description = 'Sales Stocker' AND Employee_Name IN(Select Employee_Name From TableName WHERE Description = 'Sales')
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39165089
SELECT
    EmployeeID, Employee_Name
FROM dbo.tablename
GROUP BY EmployeeID, Employee_Name
HAVING
    MAX(CASE WHEN Description = 'Sales' THEN 1 ELSE 0 END) = 1 AND
    MAX(CASE WHEN Description = 'Sales Stocker' THEN 1 ELSE 0 END) = 1
ORDER BY
    EmployeeID, Employee_Name
0
 

Author Comment

by:smhorton
ID: 39165150
I will post the exact query I am using but it will have to be this afternoon.  Sorry.  I don't have access to that machine from where I am at.  Will also try both of the examples above then as well.  Thanks!
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:smhorton
ID: 39165180
SELECT [employee_id]
      ,[first_name]
      ,[last_name]
      ,[dept_no]
      ,[salary]
  FROM [AdventureWorks2008].[dbo].[employee]
  where dept_no = '10' and dept_no ='20'

Above is an example I have made up using adventureworks 2008.  the and between the dept_no returns nothing but I have employees setup with both a 10 and a 20 dept number.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39165299
SELECT [employee_id]
      ,[first_name]
      ,[last_name]
      ,[dept_no]
      ,[salary]
      ,COUNT(*)
FROM [AdventureWorks2008].[dbo].[employee]
WHERE dept_no IN ('10','20')
GROUP BY [employee_id],[first_name],[last_name],[dept_no],[salary]
HAVING COUNT(*) = 2;
0
 

Author Comment

by:smhorton
ID: 39165586
The last example just above using adventureworks 2008 doesn't return any rows for me.  Sorry.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 250 total points
ID: 39165669
What does the query without the having clause produce?
SELECT [employee_id]
      ,[first_name]
      ,[last_name]
      ,[dept_no]
      ,[salary]
      ,COUNT(*)
FROM [AdventureWorks2008].[dbo].[employee]
WHERE dept_no IN ('10','20')
GROUP BY [employee_id],[first_name],[last_name],[dept_no],[salary]
ORDER BY [employee_id],[first_name],[last_name],[dept_no],[salary];
0
 
LVL 32

Expert Comment

by:awking00
ID: 39165678
If dept_no is a numeric datatype, adventureworks might not like doing an implicit conversion. Try removing the quotes -
where dept_no in (10,20)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now