Good day
I am trying to query the names of employees who work on every project.
My code is as follows:
SELECT CONCAT (fname,' ', minit,'. ' , lname) AS NAME
FROM employee a, works_on b, project c
WHERE pno IN (Select pnumber //WHAT COULD I SUBSTITUTE W/ IN
FROM projet)
AND a.ssn = b.essn
AND b.pno = c.pnumber
The problem with IN is that it is that the values inside are like evaluated as 'OR'... what is the equivalent of IN that makes the value of my subquery evaluated like 'AND'
Thank you in advance.
EDIT:
As reqeusted..
SELECT *
FROM employee e
WHERE NOT EXISTS
(
SELECT NULL
FROM employee ei
CROSS JOIN
project p
LEFT JOIN
works_on wo
ON wo.pno = p.pnumber
AND wo.essn = ei.ssn
WHERE ei.ssn = e.ssn
)
select CONCAT (fname,' ', minit,'. ' , lname) AS NAME
from employee
left join works_on
on works_on.essn=employee.ssn
group by employee.ssn
having count(works_on.essn) = (select count(*) from project);
Simplified example:
create table emp_work_on
(
emp_name varchar(50),
work_on varchar(30)
);
create table works
(
work_on varchar(30)
);
insert into works(work_on) values('apple'),('microsoft'),('google'),('facebook')
insert into emp_work_on values
('john','apple'),('john','microsoft'),('john','google'),('john','facebook'),
('paul','microsoft'),('paul','google'),
('george','apple'),('george','microsoft'),('george','google'),('george','facebook'),
('ringo','apple'),('ringo','facebook');
select e.emp_name
from works w
left join emp_work_on e on e.work_on = w.work_on
group by e.emp_name
having count(e.work_on) = (select count(*) from works)
order by e.emp_name
Output:
emp_name
----------
george
john
(2 rows)
On your table structure, you could use this:
SELECT * FROM employee
WHERE ssn IN
(
SELECT w.essn
FROM project c
LEFT JOIN works_on w ON w.pno = c.pnumber
GROUP BY w.essn
HAVING COUNT(w.pno) = (SELECT COUNT(*) FROM project)
)
Hmm.. but I think this could be the simplest, granting there's no repeating pno on employee's works_on, i.e. there's no pno in works_on that doesn't exists on project, i.e. referential integrity is maintained
SELECT * FROM employee
WHERE ssn IN
(
SELECT essn
FROM works_on
GROUP BY essn
HAVING COUNT(pno) = (SELECT COUNT(*) FROM project)
)
Related
I have written the code shown below and I got the expected output. but,need the same output without use the same tables in inline view (such as tables join for UserPhoneDetail_JSON). thanks in advance
Code:
BEGIN
DROP TABLE #USERMASTER;
DROP TABLE #USERPHONE;
CREATE TABLE #USERMASTER (ID INT, NAME VARCHAR(100));
CREATE TABLE #USERPHONE (ID INT, PHONENUMBER NUMERIC,PHONETYPE CHAR(1));
INSERT INTO #USERMASTER VALUES(1,'JOHN');
INSERT INTO #USERMASTER VALUES(2,'VICTOR');
INSERT INTO #USERPHONE VALUES(1,1356487965,'W');
INSERT INTO #USERPHONE VALUES(1,9841007493,'M');
INSERT INTO #USERPHONE VALUES(1,7255952105,'O');
INSERT INTO #USERPHONE VALUES(2,9874563212,'M');
WITH E AS (SELECT A.ID,A.NAME,B.PHONENUMBER,B.PHONETYPE,ROW_NUMBER() OVER(PARTITION BY A.ID ORDER BY A.ID) RN
FROM #USERMASTER A JOIN #USERPHONE B ON A.ID=B.ID)
SELECT E.ID,
E.NAME,
E.PHONENUMBER,
E.PHONETYPE,
UserPhoneDetail_JSON = (
SELECT A.ID,B.PHONETYPE,PHONENUMBER,A.NAME FROM #USERMASTER A JOIN #USERPHONE B ON A.ID=B.ID
FOR JSON PATH )
FROM E WHERE RN=1;
END
output:
1 JOHN 1356487965 W [{"ID":1,"PHONETYPE":"W","PHONENUMBER":1356487965,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"M","PHONENUMBER":9841007493,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"O","PHONENUMBER":7255952105,"NAME":"JOHN"},{"ID":2,"PHONETYPE":"M","PHONENUMBER":9874563212,"NAME":"VICTOR"}]
2 VICTOR 9874563212 M [{"ID":1,"PHONETYPE":"W","PHONENUMBER":1356487965,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"M","PHONENUMBER":9841007493,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"O","PHONENUMBER":7255952105,"NAME":"JOHN"},{"ID":2,"PHONETYPE":"M","PHONENUMBER":9874563212,"NAME":"VICTOR"}]
EXPECTED OUTPUT:
1 JOHN 1356487965 W [{"ID":1,"PHONETYPE":"W","PHONENUMBER":1356487965,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"M","PHONENUMBER":9841007493,"NAME":"JOHN"},{"ID":1,"PHONETYPE":"O","PHONENUMBER":7255952105,"NAME":"JOHN"}]
2 VICTOR 9874563212 M [{"ID":2,"PHONETYPE":"M","PHONENUMBER":9874563212,"NAME":"VICTOR"}]
If I understand you correctly and you need to generate JSON content for each first row per ID, this statement is an option:
SELECT
t.ID, t.NAME, t.PHONENUMBER, t.PHONETYPE,
UserPhoneDetail_JSON = (
SELECT t.ID, PHONETYPE, PHONENUMBER, t.NAME
FROM #USERPHONE
WHERE ID = t.ID
FOR JSON PATH
)
FROM (
SELECT
m.ID, m.NAME, p.PHONENUMBER, p.PHONETYPE,
ROW_NUMBER() OVER (PARTITION BY m.ID ORDER BY p.ID) RN
FROM #USERMASTER m
JOIN #USERPHONE p ON m.ID = p.ID
) t
WHERE t.RN = 1
This is my sql in SQL Server. How can I achieve this in MySQL?
SELECT COUNT(*) as total FROM (SELECT personal.*
CROSS APPLY
(
SELECT TOP 1 educ_attain.school
FROM educ_attain
WHERE personal.empno = educ_attain.empno
) educ
WHERE personal.status = 'ACTIVE'
) as num
My goal is to exclude employees who doesn't have a record in educ_attain. I have tried using sub-query but still returning employees with no record.
You can try any of these following-
SELECT *
FROM personal
INNER JOIN educ_attain
ON personal.empno = educ_attain.empno
WHERE personal.status = 'ACTIVE'
OR
SELECT *
FROM personal
WHERE empno IN
(
SELECT DISTINCT empno FROM educ_attain
)
AND status = 'ACTIVE'
To get count - Just use SELECT COUNT(*) for any of the above query.
Not Exists, employees do not have record in eductable then show
select * from
employee e
where not exists
(
select 1 from eductable t
on e.emp_id = t.emp_id
)
I am trying to update all city values of the table team as: “city” + “ #p” + “number of players” +” g” + “number of goals forward” (e.g. “Tokyo #p25 g74”).
I tried to do this and have this two queries. One for get the number of players and one for get the number of goals.
Query for the number of players:
select t.city + '#p' + CONVERT(varchar(10), count(pt.playerId)) + '#g'
from team t,
player_team pt
where pt.teamID = t.teamID
group By t.teamId,t.city
Query for the number of goals:
select count(*) totalgoals,
pt.teamID
from goals g,
player_team pt
where g.playerId = pt.playerId
group by pt.teamID
i couldn't merge theese two counts.
Help me out pls...
Also my tables hierarchy and fields like the shown below
player
(
playerID int,
firstName nvarchar(25),
lastName nvarchar(25),
nationality varchar(25),
birthDate smalldatetime,
age smallint,
position varchar(25)
)
team
(
teamID int,
name nvarchar(50),
city nvarchar(25)
)
player_team
(
playerID int,
teamID int,
season varchar(5)
)
match
(
matchID int,
homeTeamID int,
visitingTeamID int,
dateOfMatch smalldatetime,
week tinyint
)
goals
(
matchID int,
playerID int,
isOwnGoal bit,
minute tinyint
)
EDIT : Select query with the given below is worked well and give me the right results.But how can i update the table with this multiple records? When i try to update it as a subquery into update statement, it gives me compile error and complaining about multirecords...
Error:Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Assuming your 2 queries are producing the desired results, try combining them with an an outer join and a subquery:
select pt.teamId,
t.city + ' #p' + CONVERT(varchar(10), count(pt.playerId))
+ ' g' + CONVERT(varchar(10), t2.totalgoals)
from team t
inner join player_team pt on pt.teamID = t.teamID
left join (
select count(*) totalgoals,
pt.teamID
from goals g inner join player_team pt on g.playerId = pt.playerId
group by pt.teamID
) t2 on t.teamid = t2.teamid
group By pt.teamId,t.city,t2.totalgoals
SQL Fiddle Demo
The simplest way probably is to use correlated subqueries to look up the values for each team:
SELECT t.city || ' #p' ||
CONVERT(varchar(10), (SELECT count(*)
FROM player_team
WHERE teamId = t.teamId))
|| ' #g ' ||
CONVERT(varchar(10), (SELECT count(*)
FROM goals
JOIN player_team USING (playerId)
WHERE teamId = t.teamId))
FROM team t
Try this:
SELECT a.city '#p' + CONVERT(varchar(10), a.playerCount) + '#g' + CONVERT(varchar(10), b.totalgoals)
FROM (SELECT t.teamId, t.city, count(pt.playerId) as playerCount
FROM team t JOIN player_team pt on (t.teamID = pt.teamID)
GROUP BY t.teamId, t.city)a JOIN
(SELECT pt.teamId, count(*) as totalgoals,
FROM goals g JOIN player_team pt on (g.playerId = pt.playerId)
GROUP BY pt.teamId) b ON (a.teamId = b.teamId)
I have an SQL task where it asks to "Show a list of persons with First Name and Last Name, who did not contribute in any composition."
Here is what I've attempted, but to no avail.
select * from (select count(a.person_id)num,a.first_name ||' '|| a.last_name name
from LABPRJ_PERSON a, LABPRJ_COMPOSITION_DETAIL w
where a.person_id=w.person_id group by last_name, a.first_name
union
select 0,a.first_name ||' '|| a.last_name name from LABPRJ_PERSON a where person_id not in (select person_id
from LABPRJ_COMPOSITION_DETAIL)) where num = 0;
Any help would be greatly appreciated.
You should just need this, as far as I can tell:
SELECT * FROM LABPRJ_PERSON WHERE person_id NOT IN (SELECT person_id FROM LABPRJ_COMPOSITION_DETAIL);
You can also use NOT EXISTS, it has better performance than NOT IN:
SELECT last_name, first_name FROM LABPRJ_PERSON p
WHERE NOT EXISTS
(SELECT * FROM LABPRJ_COMPOSITION_DETAIL c ON c.person_id = p.person_id)
IF YOU ARE USING SQL SERVER PLEASE TRY THIS ONE.
SELECT
CAST(A.FIRST_NAME AS AS VARCHAR(MAX)) + ' '+ CAST(A.LAST_NAME AS VARCHAR(MAX))
FROM
LABPRJ_PERSON A
WHERE NOT EXISTS
(
SELECT
B.PERSON_ID
FROM
LABPRJ_COMPOSITION_DETAIL B WHERE A.PERSON_ID = B.PERSON_ID
)
SELECT last_name, first_name
FROM LABPRJ_PERSON AS T1
RIGHT OUTER JOIN
LABPRJ_COMPOSITION_DETAIL AS T2
ON T1.person_id=T2.person_id
WHERE T2.person_id IS NULL;
I need your support here.I want to change this to an update statement,I want to update all null values of E.Age with Birthage from PersonDeatl .The below script just selects .Table Employee and and PersonDeatl do not have keys to join them nor with EmpRet.Thanks guys
select distinct * from ((Select
E.EmployeeId,E.Address,E.City,E.Zip,E.State,E.Age
from Employee E join
EmpRet R
on E.EmployeeId=R.EmployeeId
where R.Dscription='Age not Qualify' and E.Age is null)Ag inner join
(select address,city,zip,state,BirthAge from PersonDeatl)Pd
on ag.Address=Pd.Address and ag.City=Pd.City and ag.zip=Pd.Zip)
You can try this.
With EmpCTE ( Age, BirthAge ) as (
SELECT E.Age, P.BirthAge
FROM Employee E
JOIN PersonDeatl P
ON E.Address = P.Address
AND E.City=P.City
AND E.Zip=P.Zip
Where E.Age IS NULL
)
UPDATE EmpCTE
SET Age = BirthAge