Linq to SQL Left Outer Join Not - sql

I have two Tables "Employees" and "EmployeesCompanies", Employees contains a list of all employees, and employeescompanies contains a list of all companies associated with an employee:
Table 1 (Employees)
EmployeeID
1
2
3
Table 2 (EmployeesCompanies)
EmployeeID
1
2
I want to return
3 which is the missing record from EmployeesCompanies, here is the linq code I'm using:
var queryOrphanedEmployees = (from a in db.Employees
join b in db.EmployeesCompanies
on a.EmployeeID equals b.EmployeeID
into outer
from c in outer.DefaultIfEmpty()
select new { a.EmployeeID}).ToList();
However this returns:
1
2
Which is exactly opposite to what I want.

You should be able to do something like this if you've set up the foreign keys properly
from e in Employees
where !e.EmployeesCompanies.Any()
select e

Related

Find the names of projects where all employees currently working on that project work in the same department?

I am new to SQL, and am trying to figure our how to "find the names of projects where all employees currently working on the project work in the same department." For this problem, we have three tables. This problem could be simplified if this data was stored in fewer tables. But lets assume we can't modify our tables, and have to make a query.
"employeeproject" - our project table that we can refer to with our empid
empid
projectid
1
2
2
2
3
1
4
1
"project" - Project table, that we can refer to with projectid
projectid
name
1
ring
2
lord
"department" - our department table which we can refer to with empid
empid
department
1
1
2
1
3
2
4
3
I've been attempting to make a single query that can do this. Right now I have a found a way to output the number of departments working on a project, but I still need to get the name of the project to output. Any suggestions would help.
SELECT COUNT(DISTINCT d.department)
FROM employeeproject ep
LEFT JOIN project p ON p.projid = ep.projid
LEFT JOIN department d ON ep.empid = d.empid
GROUP BY p.projid;
Expected result :
project name
Lord
You can use an aggregation on the project name while using count of different departments equal to 1 as condition in an HAVING clause.
SELECT name
FROM project p
INNER JOIN employeeproject ep
ON p.projectid = ep.projectid
INNER JOIN department d
ON ep.empid = d.empid
GROUP BY name
HAVING COUNT(DISTINCT d.deparment) = 1
Here's a demo in MySQL, though this should work on most DBMS'.

Need Simple SQL direction

I'm trying to write a Ado SQL statement for my Access table and I'm getting the wrong results.
Employee Table
ID Name DriverID
1 Alex 1
2 Tom 2
3 Trevor 3
4 PHIL 0
5 Gina 4
Vehicle Table
ID PLATE EMPLOYEEID INSERVICE
1 123XYZ 1 N
2 456GFR 2 Y
3 TFV4FG 3 Y
4 F6GK7D 4 Y
5 GEY7GH 1 Y
I want result of All employes and to display the Vehcicle info if they are assigned to it.
Result should be
Name Plate
Alex GEY7GH
Tom 456GFR
Trevor TFV4FG
PHIL
Gina F6GK7D
SELECT Employee.ID, Employee.FirstName, Vehicles.Plate, Vehicles.InService
FROM Employee LEFT JOIN Vehicles ON Employee.ID = Vehicles.DriverID
WHERE (((Vehicles.InService)=True));
Does not display PHIL who is not assigned to a vehicle.
Just add the condition inside the join, making sure to use parentheses to avoid problems when joining with constants or anything but simple equals:
SELECT Employee.ID, Employee.FirstName, Vehicles.Plate, Vehicles.InService
FROM Employee LEFT JOIN Vehicles ON (Employee.ID = Vehicles.DriverID AND Vehicles.InService = True)
From the above tables, it looks like the DriverID Column in employee table aligns with the EmployeeID column in the vehicles table and the issue is the on clause in the join.
SELECT
Employee.ID
,Employee.FirstName
,Vehicles.Plate
,Vehicles.InService
FROM
Employee
LEFT JOIN Vehicles ON Employee.DRIVERID = Vehicles.EMPLOYEEID
WHERE
(((Vehicles.InService)=True));
Well, in a normal database, you would move the WHERE condition to the ON clause. But I don't think that MS Access supports this:
SELECT e.ID, e.FirstName, v.Plate, v.InService
FROM Employee as e LEFT JOIN
Vehicles as v
ON e.ID = v.DriverID AND v.InService = True;
An alternative is a subquery:
SELECT e.ID, e.FirstName, v.Plate, v.InService
FROM Employee as e LEFT JOIN
(SELECT v.*
FROM Vehicles as v
WHERE v.InService = True
) as v
ON e.ID = v.DriverID ;

How to get the unmatched records from two tables using Joins

I have two tables one is teacher and another is Department which is mentioned below.
Teacher Table
Id Name
1 xyz
2. Gjd
3. Dftr
4 dhdk
Department Table
Id Name EMPID
1 SQL. 2
2. PHP. 4
3. JAVA. 1
4 PEARL. 5
QUESTION
i want those records of teacher which are not link with any Department.
you can use following statement using left join then filter Teacher that not matched
SELECT t.*
FROM Teacher t
left join Department d on d.EMPID = t.Id
where d.id is null
SELECT * FROM teachers WHERE
id NOT IN (SELECT DISTINCT EMPID FROM departments) ;
Hope this helps.!!
you can do it by inner query..
select * from teacher where id not in (select empid from department);

How to join my tables?

I'm using a Sqlite3 database and I have 3 tables which I want to join. My program basically has documents. And you can select multiple employees that belong to the document. But employees can also belong to other documents.
Below are my tables:
Table Employee:
id:Integer primary key
Table Document:
id:Integer primary key
Table DocEmp:
id:Integer primary key
docId:Integer
empId:Integer
state:Integer
The DocEmp table can be empty only the Employee and Document table is always filled. Most important is that i always want to load all employees!
So for example:
I have 3 employees with id's: 1,2 and 3. Have 2 documents with id 1 and 2.
The DocEmp has the following records:
Row1: 1,1,1,0
Row2: 2,1,2,1
So this means only document 1 has 2 employees with id 1 and 2. So when i'm in document 1 i want to load all 3 employees, and also want to know the state of the 2 employees that are filled in for that document in the DocEmp table. And because document 2 has no employees i need just all the employees.
If I understand your question correctly, I think you might be looking for a left join:
select e.id as employeeid,
d.id as documentid,
de.state as state
from employee e
--this could be an inner join but your example is confusing
left join document d on e.id = d.id
left join docemp de on e.id = de.empid
and d.id = de.docid;

NHibernate + join to derived table

In a table that stores multiple rows per employee, I want to pull one row per employee that represents the most recent entry for each employee. Here's where I am with hand-written SQL:
SELECT [all the selected columns here]
FROM Nominations t
inner join
(select max(NominationId) mostRecentNominationId,
EmployeeId from Nominations group by EmployeeId) n
on n.mostRecentNominationId = t_.NominationId
From source data like this:
nomination_id employee_id
-------------------------------
1 5
2 5
4 10
7 10
That'll give me something like this:
nomination_id employee_id
-------------------------------
2 5
7 10
I haven't been able to figure out how to accomplish that type of query via NHibernate ICriteria. Any thoughts?
Here is what you need to do:
DetachedCriteria dCriteria = DetachedCriteria.For<Nomination>("nomination")
.SetProjection(Projections.Max("nomination.Id"))
.Add(Restrictions.EqProperty("nomination.EmployeeId", "employee.Id"));
var nominations = Session.CreateCriteria<Nomination>("nom")
.CreateCriteria("Employee", "employee")
.Add(Subqueries.PropertyEq("nom.Id", dCriteria)).List<Nomination>();
This is not equilevant to the SQL query providfed in the question but it does exactly the same thing.
The SQL query that is generated by the above criteria query is:
SELECT *
FROM Nomination nom
inner join Employee employee on nom.EmployeeId=employee.EmployeeId
WHERE nom.NominationId =
(SELECT max(nomination.NominationId) as maxID
FROM Nomination nomination
WHERE nomination.EmployeeId = employee.EmployeeId)