Sql Query

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 7
Reputation: Moody1 is an unknown quantity at this point 
Solved Threads: 0
Moody1 Moody1 is offline Offline
Newbie Poster

Sql Query

 
0
  #1
34 Days Ago
Hello,
Ihope to find some help in here, i have the following diagram

http://img143.imageshack.us/img143/6704/13503390.th.jpg

and i want a query which return Emp_ID, Pro_ID

note;
i can get these values but by 2 queries as the following;
  1. SELECT Pro_ID FROM Projects WHERE Dep_ID IN
  2. (SELECT Dep_ID FROM Departments WHERE name = 'test')
  3.  
  4. SELECT Emp_ID FROM Employees WHERE Dep_ID IN
  5.  
  6. (SELECT Dep_ID FROM Departments WHERE name = 'test')

the goal of having the result through one query is that i want to insert the value into another table
insert into testTable (query)

Hope to see reply ASAP
Last edited by peter_budo; 33 Days Ago at 10:01 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
34 Days Ago
If the tables have a 1:1 relationship:
  1. SELECT
  2. (
  3. SELECT Top 1 Pro_ID
  4. FROM Projects
  5. WHERE Dep_ID IN
  6. (
  7. SELECT Dep_ID
  8. FROM Departments
  9. WHERE name = 'test'
  10. )
  11. ) AS PRO_ID,
  12. (
  13. SELECT Top 1 Emp_ID
  14. FROM Employees
  15. WHERE Dep_ID IN
  16. (
  17. SELECT Dep_ID
  18. FROM Departments
  19. WHERE name = 'test'
  20. )
  21. ) AS EMP_ID
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: Moody1 is an unknown quantity at this point 
Solved Threads: 0
Moody1 Moody1 is offline Offline
Newbie Poster
 
0
  #3
34 Days Ago
Hello,
Thanks for ur quick response.
the relationship is 1:m
so i'm getting this error message when i'm trying this query
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

i think that i need to use JOIN but i don't know how to join these tables to get the desire results.

so any other suggestions?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
34 Days Ago
You haven't provided enough information to answer that question. Please provide table structures for both tables, sample data, and desired results from your sample data.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: Moody1 is an unknown quantity at this point 
Solved Threads: 0
Moody1 Moody1 is offline Offline
Newbie Poster
 
0
  #5
34 Days Ago
the structure is as the follow;
Departments
Dep_ID (int), Name (Varchar)
Employee
Emp_ID (int), Name (Varchar), Dep_ID (int)
Projects
Pro_ID (int), Name (Varchar), Dep_ID (int)
Pro_Emp
ID (int), Pro_ID (int), Emp_ID (int)

sample data
Departments
1 ------- Administration
2 ------- Markiting
Employee
1 ------- Mike ------ 2
2 ------- jhone ----- 1
Projects
1 ------- test ------ 2
2 ------- test1 ----- 1

Now i want to get Emp_ID, Pro_ID which have the same Dep_ID depending on the Dep name and insert them into temp table and then insert them back to Pro_Emp table

the relationships are clarified in the attached pic up in the first post.

i hope that the situation is clear now
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #6
34 Days Ago
>>the relationships are clarified in the attached pic up in the first post.
That picture is a 1x1" square. There is no way you could read it..

Try something like this. I still don't understand what you're asking but maybe this will work:
  1. IF OBJECT_ID('tempdb..#Department', 'U') IS NOT NULL DROP TABLE #Department
  2. IF OBJECT_ID('tempdb..#Employee', 'U') IS NOT NULL DROP TABLE #Employee
  3. IF OBJECT_ID('tempdb..#Projects', 'U') IS NOT NULL DROP TABLE #Projects
  4.  
  5. CREATE TABLE #Department
  6. (
  7. Dep_ID int PRIMARY KEY,
  8. Name varchar(50)
  9. )
  10. INSERT INTO #Department (Dep_ID, Name) Values (1, 'Administration')
  11. INSERT INTO #Department (Dep_ID, Name) Values (2, 'Marketing')
  12.  
  13. CREATE TABLE #Employee
  14. (
  15. Emp_ID int PRIMARY KEY,
  16. Name varchar(50),
  17. Dep_ID int
  18. )
  19. INSERT INTO #Employee (Emp_ID, Name, Dep_ID) Values (1, 'Mike', 2)
  20. INSERT INTO #Employee (Emp_ID, Name, Dep_ID) Values (2, 'Jhone', 1)
  21.  
  22. CREATE TABLE #Projects
  23. (
  24. Pro_ID int PRIMARY KEY,
  25. Name varchar(50),
  26. Dep_ID int
  27. )
  28. INSERT INTO #Projects (Pro_ID, Name, Dep_ID) Values (1, 'test', 2)
  29. INSERT INTO #Projects (Pro_ID, Name, Dep_ID) Values (2, 'tes1', 1)
  30.  
  31. SELECT *
  32. FROM #Department Inner Join #Projects On (#Projects.Name = 'Test' and #Department.Dep_ID = #Projects.Dep_ID)
  33. INNER JOIN #Employee On (#Projects.Dep_ID = #Employee.Dep_ID)
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: Moody1 is an unknown quantity at this point 
Solved Threads: 0
Moody1 Moody1 is offline Offline
Newbie Poster
 
0
  #7
34 Days Ago
about the pic i've attached another one, i hope it's clear now.
i will describe the process which will get me to the desired result
- i have projects on the waiting list in this table
Waiting_List
ID (int)
Name (varchar)
Dep_ID (int)

now after moving the projects from the waiting_list into their table Projects i want to fill Pro_Emp table which used to broke many to many relationship between Projects and Employee table.
so the main goal is to fill Pro_Emp table

so your first query is giving me the desired results but in the case of 1:1 relationship.

the insert query should be like this
  1. INSERT INTO Pro_Emp (Emp_ID, Pro_ID) SELECT Emp_ID, Pro_ID FROM Employee, Projects WHERE Dep_ID IN (SELECT Dep_ID FROM departments WHERE name = 'test')
(this query isn't right but i've put it as sample to the query which i want)
as i said before i can get Emp_ID individually by a query but i want to get these two values togethr (Emp_ID, Pro_ID)

i hope this clear now, if not please let me know the point which making u confused.

thanks for ur help
Last edited by peter_budo; 33 Days Ago at 10:02 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Attached Thumbnails
test1.JPG  
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #8
33 Days Ago
Take the query I updated, add more test data, and report back what you want. The test data you provided only had a 1:1 relationship in the database.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: Moody1 is an unknown quantity at this point 
Solved Threads: 0
Moody1 Moody1 is offline Offline
Newbie Poster
 
0
  #9
33 Days Ago
Hello,
i just wanna say many thanks for ur help
i've solved it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC