473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Selecting the same column twice in the same where clause

Hi :
From a crystal report i get a list of employee firstnames as a string
into my store procedure. Why is it comming this way ? hmmmmmm it's a
question for me too.
ex: "e1,e2,e3"

here are my tables

tblProjects
ProjectId
1
2
3

tblEmployee
employeeId FirstName
1 e1
2 e2
3 e3

tblProjectsToEmployee
ProjectId employeeId
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
4 1
4 3

i need to find out the project ids all 3 of these employees worked on.
so the out put i need is

projectId
1
3
How can i get it ????????????

now i can use replace command to format it to a OR clause or AND
clause
SET @string= 'employeeId =' + '''' + REPLACE('e1,e2,e3',',',''' OR
employeeId = ''') + ''''
some thing like this.

OR clause will give me all 4 projects.
in('e1','e2','e3') will give me all 4 projects.
of cause AND command will not give me any.

other method i tried was adding the employee table 3 times into the
same SQL string and doing some thing like

WHERE (empTable1.Firstname ='e1' AND (empTable1.Firstname
in('e2','e3'))
AND (empTable2.Firstname ='e2' AND (empTable1.Firstname in('e1','e3'))
AND ...

and goes alone. this gives me some what i needed. but it's a very
messy way of doing it, because i get a comma seperated string
parameter i have to construct the sql string on the fly.

any help or direction on this matter would greatly appreciated.

thanks
eric
Jul 23 '05 #1
3 3932
On 7 Feb 2005 16:04:36 -0800, Eric wrote:
From a crystal report i get a list of employee firstnames as a string
into my store procedure. Why is it comming this way ? hmmmmmm it's a
question for me too.
ex: "e1,e2,e3" (snip)

Hi Eric,

The first step would be to convert the comma-delimited list of employees
to a temp table. SQL Server MVP Erland Sommarskog has a whole web page
devoted to various techniques to accomplish this. Check it out at
http://www.sommarskog.se/arrays-in-sql.html, pick one of the techniques
and implement it. For the rest of this message, I'll assume that the
employees you are looking for are not in a comma-delimited list anymore,
but in the temp table #Employees, column EmployeeID.

i need to find out the project ids all 3 of these employees worked on.
so the out put i need is

projectId
1
3
How can i get it ????????????

(snip)

This is called "relational division". The standard solution is

SELECT p.ProjectID
FROM Projects AS p
INNER JOIN ProjectsToEmployee AS pe
ON pe.ProjectID = p.ProjectID
INNER JOIN #Employees AS e
ON e.EmployeeID = pe.EmployeeID
GROUP BY p.ProjectID
HAVING COUNT(*) = (SELECT COUNT(*) FROM #Employees)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Eric wrote:
Hi :
From a crystal report i get a list of employee firstnames as a string
into my store procedure. Why is it comming this way ? hmmmmmm it's a
question for me too.
ex: "e1,e2,e3"

here are my tables

tblProjects
ProjectId
1
2
3

tblEmployee
employeeId FirstName
1 e1
2 e2
3 e3

tblProjectsToEmployee
ProjectId employeeId
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
4 1
4 3

i need to find out the project ids all 3 of these employees worked on.
so the out put i need is

projectId
1
3
How can i get it ????????????

now i can use replace command to format it to a OR clause or AND
clause
SET @string= 'employeeId =' + '''' + REPLACE('e1,e2,e3',',',''' OR
employeeId = ''') + ''''
some thing like this.

OR clause will give me all 4 projects.
in('e1','e2','e3') will give me all 4 projects.
of cause AND command will not give me any.

other method i tried was adding the employee table 3 times into the
same SQL string and doing some thing like

WHERE (empTable1.Firstname ='e1' AND (empTable1.Firstname
in('e2','e3'))
AND (empTable2.Firstname ='e2' AND (empTable1.Firstname in('e1','e3'))
AND ...

and goes alone. this gives me some what i needed. but it's a very
messy way of doing it, because i get a comma seperated string
parameter i have to construct the sql string on the fly.

any help or direction on this matter would greatly appreciated.

thanks
eric


You can try this, I know database experts won't like this solution but
it works :)

SELECT ProjectId
FROM (SELECT tblProjects.ProjectId,SUM(tblEmployee.sadrzajid) as sum_all
FROM tblProjects
INNER JOIN tblProjectsToEmployee
ON tblProjects.ProjectId = tblProjectsToEmployee.ProjectId
INNER JOIN tblEmployee
ON tblProjectsToEmployee.employeeId = tblEmployee.employeeId
GROUP BY tblProjects.ProjectId) AS complet
WHERE (complet.sum_all = (SELECT SUM(employeeId)
FROM tblEmployee))

Josko

Jul 23 '05 #3
Joško Šugar wrote:


You can try this, I know database experts won't like this solution but
it works :)

SELECT ProjectId
FROM (SELECT tblProjects.ProjectId,SUM(tblEmployee.sadrzajid) as sum_all
FROM (SELECT tblProjects.ProjectId,SUM(tblEmployee.employeeId) as sum_all

lapsus, sorry
FROM tblProjects
INNER JOIN tblProjectsToEmployee
ON tblProjects.ProjectId = tblProjectsToEmployee.ProjectId
INNER JOIN tblEmployee
ON tblProjectsToEmployee.employeeId = tblEmployee.employeeId
GROUP BY tblProjects.ProjectId) AS complet
WHERE (complet.sum_all = (SELECT SUM(employeeId)
FROM tblEmployee))

Josko

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nimesh | last post by:
I need to find customer's names that repeat / occur more than once in the same table. I have treid many options and I have tried comparing the column to itself but Oracle gives me an error. ...
5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
4
by: remote89 | last post by:
Hi experts, I have been trying to limit the table rows in the following situation, any suggestions will be appreciated. we have table called tempTb has columns id, c_id, c_name, rating, date...
5
by: uthuras | last post by:
Machine : AIX 5.2 Product : UDB DB2 Release 8.1 FP4a I have problem loading data into destination table. The data file is huge with more than 6 Million records. This what i have done 1....
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
6
by: Steve | last post by:
I realize that this probably isn't a best practice, but I'm working with legacy code that has a query stored in one column of a table. Because the queries vary, the JSP page that selects a query...
5
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset....
10
by: pbd22 | last post by:
Hi. Like the title says - how do i do this? I was given the following example: INSERT INTO TABLE2 SELECT * FROM TABLE1 WHERE COL1 = 'A' The above statement threw the following error:
9
by: niz182 | last post by:
Hey all...first time to the site so excuse me for bein some what of a noob to how to properly ask questions. Im looking to create a table which selects a cloumn from another table twice. so i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.