Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Multiple join query help needed

Author  Topic 

dkekesi
Starting Member

38 Posts

Posted - 2011-03-28 : 06:35:51
Hi All,

I am turning to you SQL experts as I feel a little lost with the following scenario.
I have 4 tables:
1. Department (contains Dept. ID, Name, misc. data)
2. Employee (contains Employee ID, Dept. ID, name, misc. data)
3. Task (contains Task ID, Department ID, task name, misc. data)
4. EmployeeSelectedForTask (contains the IDs of tasks and the IDs of employees who are selected for the task)

On the GUI of the software I select a task from a task list first. This defines the Task ID we need to work within the SQL query. In the Task window I select a department from a drop down list. This defines the Department ID we need to work within the query. Now I need to create a query which returns 2 columns in a DataGridView and this is where I need your help. Column 1 should contain the employee names for the selected department (all of them) and Column 2 should contain TRUE if the Employee ID is in the EmployeeSelectedForTask table with the Task ID selected in the first step, or FALSE if the Employee ID is NOT in the EmployeeSelectedForTask table with the Task ID selected in the first step.

How do I accomplish all this?

Thank you for your kind help in advance.

Best Regards,
Daniel

Best Regards,
Daniel

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-28 : 21:03:46
You need a stored procedure something like this:
create procedure dbo.EmployeesForTask
@task_id int,
@dept_id int
as
select
e.Name,
case
when est.employee_id is null then 'FALSE'
else 'TRUE'
end as SelectedForTask
from
Employee e
left join EmployeeSelectedForTask est
on e.employee_id = est.employee_id
where
e.dept_id = @dept_id
and est.task_id = @task_id
Once you have this stored procedure, run it in SSMS to make sure you are getting the right answers. Then you will need to invoke this stored proc in your GUI/middle-tier software.

Hope that helps. I made up column names etc. So it may not work exactly as it is.
Go to Top of Page

dkekesi
Starting Member

38 Posts

Posted - 2011-03-30 : 12:00:04
Thank you for your help. I will give it a try.

Best Regards,
Daniel
Go to Top of Page

dkekesi
Starting Member

38 Posts

Posted - 2011-03-31 : 04:06:51
Hi,

The query was missing an additional join, but it was more than enough to get me started. It is working now as it should.
Thank you for your help once again.

Best Regards,
Daniel

Best Regards,
Daniel
Go to Top of Page

dkekesi
Starting Member

38 Posts

Posted - 2011-04-01 : 12:18:41
OK, I thought I had it going but not. If I select employee1 at task1 and select employee2 at task2 and execute the query, I'll get both employees selected for both tasks. I've been thinking about this for a while now but I simply cannot wrap my head around the problem.
Please help as I am going nuts.

Best Regards,
Daniel
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-01 : 16:05:09
quote:
Originally posted by dkekesi

OK, I thought I had it going but not. If I select employee1 at task1 and select employee2 at task2 and execute the query, I'll get both employees selected for both tasks. I've been thinking about this for a while now but I simply cannot wrap my head around the problem.
Please help as I am going nuts.

Best Regards,
Daniel

I thought you select a Task followed by a department. So ONE task, ONE department and then you wanted to get the list of employees in that department selected for that task.

So I didn't quite follow what you meant by selecting task1 and employee1 and task2 and employee2. Can you explain your process a little bit more? Where are you selecting employees?
Go to Top of Page

dkekesi
Starting Member

38 Posts

Posted - 2011-04-04 : 13:32:35
Hi Sunitabeck,

Sorry for the misunderstanding and my poor phrasing of the situation. What I meant if the database contains employee1 selected for task1 and employee2 selected for task2 then executing the sp will return that both employee1 and employee2 are selected for task1 and task2.

I hope I could clarify the problem.

Thanks again for your valuable time.

Best Regards,
Daniel
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-04 : 14:26:04
I am afraid I am still not seeing the problem.

So I created the test script below. Run this script (in a test environment) to see what it does. Then, change the insert statements as you see fit to demonstrate the problem. Once you have the problem, copy the modified script and post it to the forum.

--- CREATE TABLES
create table dbo.Department
(
DepartmentId int not null primary key clustered,
DepartmentName varchar(255) not null
);

create table dbo.Employee
(
EmployeeId int not null primary key clustered,
DepartmentId int not null references Department(DepartmentId),
EmployeeName varchar(255) not null
);

create table dbo.Task
(
TaskId int not null primary key clustered,
DepartmentId int not null references Department(DepartmentId),
TaskName varchar(255) not null
);

create table dbo.EmployeeSelectedForTask
(
TaskId int not null references Task(TaskId),
EmployeeId int not null references Employee(EmployeeId),
primary key clustered(TaskId, EmployeeId)
);

-- INSERT TEST DATA
insert into Department values (1, 'Development');
insert into Department values (2, 'QA');
insert into Department values (3, 'Configuration');

insert into Employee values (1,1,'Sunita Beck');
insert into Employee values (2,1, 'D. Kekesi');
insert into Employee values (3,1, 'William Gates');

insert into Task values (1, 1, 'Create Windows 8');
insert into Task values (2, 1, 'Create SQL Server 2014');
insert into Task values (3, 1, 'Create IE 11');

insert into EmployeeSelectedForTask values(1,1);
insert into EmployeeSelectedForTask values(2,2);

GO

-- CREATE STORED PROC (I CHANGED THE JOIN CONDITIN ON THE STORED PROC FROM THE ORIGINAL)

create procedure dbo.EmployeesForTask
@task_id int,
@dept_id int
as
select
e.EmployeeName,
case
when est.EmployeeId is null then 'FALSE'
else 'TRUE'
end as SelectedForTask
from
Employee e
left join EmployeeSelectedForTask est
on e.EmployeeId = est.EmployeeId and est.TaskId = @task_id
where
e.DepartmentId = @dept_id
GO

-- TEST THE STORED PROC
exec dbo.EmployeesForTask 1, 1
exec dbo.EmployeesForTask 2, 1

GO


-- CLEANUP
drop procedure dbo.EmployeesForTask;
drop table dbo.EmployeeSelectedForTask;
drop table dbo.Task ;
drop table dbo.Employee;
drop table dbo.Department;
Go to Top of Page

dkekesi
Starting Member

38 Posts

Posted - 2011-04-05 : 04:28:11
Hi Sunitabeck,

Despite my lousy phrasing you hit the nail right on the head. Thank you so much for your valuable help. The SP is now working perfectly.

Best Regards,
Daniel
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-05 : 07:10:43
That's great. Glad it worked out for you :--)
Go to Top of Page
   

- Advertisement -