how can i delete a record in sql using more than 1 table - sql-server-2000

I'm having a problem in sql, can anyone help me
I have 3 tables
sublocation, postingdetail and employee
I have to delete sublocid from sublocation table
but first i have to check that is there any employee working on that sublocation from postingdetail table, if yes then the record shouldn't be deleted.
table sublocation
sublocid
sublocname
mainlocid
table postingdetail
empid
subloc
mainloc
table employee
empid
empname
sublocid
mainlocid

Something like
DELETE
FROM [SUBLOCATION]
LEFT JOIN [EMPLOYEE] ON [SUBLOCATION].[sublocid] = [EMPLOYEE].[sublocid]
LEFT JOIN [POSTINGDETAIL] ON [POSTINGDETAIL].[empid] = [EMPLOYEE].[empid]
WHERE empid IS NULL
That might work.

Well I am not familiar with sql-server, I am a MySQL user myself, but I know sql-server does have stored procedures... and that's what stored procedures were made to do...
Here is a little tutorial on stored procedures.

I don't understand the meaning of the postingdetail table, and why we are taking the sublocid from table employee rather than postingdetail, but you query should look something like this:
delete sublocation
where sublocid not in
(
select e.sublocid
from employee e
join postingdetail p on e.empid = p.emid
)

Related

ORACLE SQL Query IN CLAUSE or other solution

I have a existing big query that has a subquery deep in the trenches that gets an employee based on a ID.
SELECT BIG QUERY.....................................
( Select mananger_id from Employee_table where employee_id=123)
)
now I have a newer requirement to address multiple employee Ids. max number of employees in a group is 200
SELECT BIG QUERY.....................................
( Select mananger_id from Employee_table where employee_id IN (?,?,?... 200X?))
)
Is this the best method? If not what should I do. What the best option
Would there be any performance implications on the IN clause
How exactly are you using this sub query?
For example if you code actually looks like this:
SELECT BIG QUERY.....................................
FROM table
WHERE table.manager_id =
( Select mananger_id from Employee_table where employee_id=123)
)
Then it is better to do this:
SELECT BIG QUERY.....................................
FROM table
JOIN Employee_table E on e.employee_id = 123 AND table.manager_id = e.manager_ID
Since you don't give much details how you get the 200 entries I can't add more detail but you can see how doing a join might be extended depending on your use

Update Employee Script

I have a provider table which has our employees information in it. It is set up like this.
Provider_Data
PID
PROFIRST
PROMI
PROLAST
When I typically use the data from this table I will use a case statement(this might be the wrong technique I am thinking).
Select ln.Location_ID,
ln.Note_Author,
lc.Customdata AS Residents
FROM Location_Notes_Data LN
JOIN Location_Notes_Custom_Data lc
ON ln.Noteid = lc.Noteid
I changed up my explanation here a little. I use this code to get the location ID, Note_Author, and then Customdata from another table. But when the location_Id and the Note_author is populated it is like this: 491-12 and 122083, its not listing the name of them. This is why I was using a CASE statement and manually typing them in.
The Note_Author = PID. My problem that I am running into is when a new employee comes on or when an employee leaves my script will not reflect this unless I manually go in and add/remove them.
I am guessing you just need a join. Your question doesn't have much detail, but something like this:
select n.*, pd.*
from notes n left join
provider_data pd
on n.note_author = pd.pid
You can update a table from joined tables. I gather you are trying to update employee records as they move in or out of your company? I'm not entirely sure, but if there exists a mapping between tables, you can code the update statement something like this.
UPDATE e
SET e.Location_ID = ln.Location_ID
,e.Residents = lc.CustomData
FROM Employee e
INNER JOIN Location_Notes_Data AS ln ON ln.Note_Author = e.PID
INNER JOIN Location_Notes_Custom_Data lc ON ln.Noteid = lc.Noteid

SQL Query Involving Data From Different Tables

I have two different tables with records I need to join together in a way I can't quite figure out how to make work. My data looks like this.
Table A
Columns: Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager ... many other columns
Sample Values:
12345 Department1 Team1 67890 Yes/No
.
.
.
One employee per row, several thousand rows comprising the entire company
Table B
Employee_ID, Manager_ID ... other columns
The exact same data set as Table A
Currently I'm combining those two tables (and three others) with a simple join on Employee_ID, which I'm then using as a data source in Tableau to visualize the data.
What I'd like to do with a SQL script is as follows:
Check to see whether an employee in Table A is a manager or not based on the Is_a_Manager column
If they are, find an employee in Table B who is one of their direct reports by matching the employee ID in Table A to the Manager ID in Table B.
Lookup that direct report's department and team in Table A by matching the Employee_ID in Table B to Employee_ID in Table A and displaying the Employee_Department and Employee_Team columns.
Add the direct report's department and team to two new columns in the original manager's Table A row
I'd like the final output in Table A to be something like
Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager? ... Direct_Report_Department, Direct_Report_Team
Also, an important point is that some managers will have employees who are on different teams, so values in the Direct_Report_Department and Direct_Report_Team are not distinct. I only actually need any one employee's Department and Team to display, it doesn't matter which employee's it is.
Finally, I am able to do step 1 fairly easily in Tableau, so if the SQL script could do steps 2-4 and simply return a null value if the employee was not a manager, that would work for me as well.
Any ideas on how to accomplish this would be greatly appreciated. Thank you!
This should work based on the requirement provided. You don’t have to do any of the steps in Tableau and can simply export the output from the SQL as your data source
Select Tb1.Employee_ID, Tb1.Employee_Department, Tb1.Employee_Team, Tb1.Manager_ID, Tb1.Is_a_Manager, Tb3. Direct_Report_Department, Tb3. Direct_Report_Team
from Table_A Tb1
join (Select Manager_id, max(Employee_id) as emp_id from Table_B group by Manager_id) Tb2
on Tb1.Employee_id = Tb2.Manager_id
left join (Select Employee_ID, Employee_Department as Direct_Report_Department, Employee_Team as Direct_Report_Team from Table_A group by Employee_ID, Employee_Department, Employee_Team) Tb3
on Tb2.emp_id = Tb3.Employee_ID
where Tb1.Is_a_Manager = 'Yes';

Combining SQL statements into one

I have the task to read data of two tables which requires several SQL statements, with only 1 query.
I know that this is possible and i have done some research on that matter but this sadly did not help me as i had no clue how to apply this to my problem.
I believe it is not too complex but because of my lack of SQL experience it's quite a challenge for me.
For understanding purposes i would like to know how to combine these three statements into one.
I have two tables. "PEmpl" and "Department" and i have the value ID.
PEmpl has the columns: "DepartmentID" and "PEmplID"
Department has the columns: "DepartmentID" "DepartmentName" and "FatherDepID"
I have the EmployerID and want to find out to what departments he has permissions so i came up with these 3 statements.
(I already programmed it working in C# with these querys, but sadly the task was to do it in only 1 query and no c#)*
SELECT DepartmentID
FROM PEmpl
WHERE PEmplID = #ID <-- the ID i have
SELECT DepartmentName
FROM Department
WHERE DepartmentID = #DepartmentID <- The DepID i got from the last query
SELECT DepartmentName
FROM Department
WHERE FatherDepID = #DepartmentID <- same ID as last querys
But i have no idea how to combine these into 1. I hope that someone can give me some clue about this. I dont expect to get the finished answer but a nudge into the right direction would be appreciated.
This is what i programmed in C#. I have the Person and it's ID and there you see that he has permission for "Musterfirma Gmbh" and because this is the father department of the others, he has also permission for those.
PS: I hope i explained it understandable. If not let me know and i will try to rephrase it :)
I am not sure I have understood correctly your question (pls post some sample data). I think you could try something like following query.
I use only one join with a OR condition, and add a column to identify if father, direct or both deps.
I used "aliases" of tables (A for PEmpl and B for Department to simplify reading the query)
SELECT B.DepartmentName
, CASE WHEN A.DepartmentID=B.DepartmentID AND A.DepartmentID=B.FatherDepID THEN 'B' /* both */
WHEN A.DepartmentID=B.DepartmentID THEN 'D' /* "direct" dep */
ELSE 'F' /* father */
END AS GR /* case updated thanks to Caleth suggestion */
FROM PEmpl A
INNER JOIN Department B ON A.DepartmentID=B.DepartmentID OR A.DepartmentID=B.FatherDepID
WHERE A.PEmplID = #ID
If you need only records without (possible) "repetitions" of departments, you can remove GR column and use DISTINCT after SELECT.
Like this maybe
SELECT
DepartmentID as dep0
FROM PEmpl
LEFT JOIN Department as dep1 on (dep0.DepartmentID = dep1.DepartmentID)
LEFT JOIN Department as dep2 on (dep0.DepartmentID = dep2.DepartmentID)
WHERE PEmplID = #ID
You might be looking for this
SELECT
A.DepartmentID,
A.PEmplID,
B.DepartmentID,
B.DepartmentName,
B.FatherDepID
FROM PEmpl A
INNER JOIN Department B
ON A.DepartmentID = B.DepartmentID
AND A.PEmplID = #ID;
Try this out, if it isn't working, fix joining columns, and check out joins that's what you need
SELECT d2.DepartmentId, d2.DepartmentName
FROM PEmpl e
JOIN Department d1 on e.DepartmentID = d1.DepartmentID
JOIN Department d2 on d2.FatherDepID = d1.DepartmentID
WHERE e.PEmplID = #ID <-- your ID
You need a JOIN, or possibly many.
WITH Deps AS (
-- This finds the parent departments. We have two copies of the same DepartmentID to have the same shape results as below
SELECT DepartmentName, DepartmentID AS Link, DepartmentID
FROM Departments
-- Combine results from two SELECT clauses (without duplicates)
UNION
-- This will find all the child departments, all the child's children, etc
SELECT child.DepartmentName, child.DepartmentID AS Link, parent.DepartmentId
FROM Departments AS child
-- We can join on the results we are selecting, to follow links
JOIN Deps AS parent ON child.FatherDepID = parent.Link )
SELECT DepartmentName
-- To add any other columns from Departments, be sure to include them in both SELECT clauses above
FROM PEmpl
JOIN Deps ON PEmpl.DepartmentID = Deps.DepartmentID

SQL - Why Does This Happen?

These are the tables that I'm working with.
With that in mind, I want to showcase the Employees that are both a supervisor and a manager.
But when I used this
select e1.fname,e1.lname
from employee e1,employee e2,department
where e1.ssn=e2.super_ssn and e1.ssn = Mgr_ssn
This was the output
I know I can solve the problem with 'distinct', but I'm more interested to know why the output turned out like it did.
How about exists?
select e.*
from employee e
where exists (select 1 from employee e2 where e2.mgr_ssn = e.ssn) and
exists (select 1 from employee e2 where e2.super_ssn = e.ssn) ;
Your query returns duplicates for two reasons. First, presumably managers and supervisors have multiple employees below them. You end up with rows for each such employee. Second, you have a cartesian product with department, which further multiplies the rows. The department table is not used in the query.
Using select distinct is not a good solution in this case. The database just ends up having to do a lot more work than necessary -- first to create the duplicate rows and then to remove them.
add department matching clause in where like
select e1.fname,e1.lname
from employee e1,employee e2,department d
where e1.ssn=e2.super_ssn and e1.ssn = Mgr_ssn and
d.Dnumber=e1.Dno