I Have a Table like this
Employeeid Name CompanyID
1 Achal 1
2 Anil 1
3 Anil 1
4 Sachi 2
5 Anil 2
6 Sachi 1
7 Sachi 2
I want to update the names of the employee if multiple employees are there in a same company
My resultant table should be like this
Employeeid Name CompanyID
1 Achal 1
2 Anil(1) 1
3 Anil(2) 1
4 Sachi(1) 2
5 Anil 2
6 Sachi 1
7 Sachi(2) 2
My query is something like this
Update tblemplayee emp
join
(
select sname,count(*)
from tblemployee
group by sname,companyid
) innertable
on innertable.employeeid=emp.employeeid
set sname = concat(sname,'(', ,')') .
How can i change my query to get the result.
If you need to execute your query only once, you could use this query:
UPDATE
employees INNER JOIN (
SELECT e1.Employeeid, COUNT(e2.Employeeid) n
FROM
employees e1 INNER JOIN employees e2
ON e1.Name=e2.Name
AND e1.CompanyID=e2.CompanyID
AND e1.Employeeid>=e2.Employeeid
INNER JOIN (SELECT Name, CompanyID
FROM employees
GROUP BY Name, CompanyID
HAVING COUNT(*)>1) dup
ON e1.Name=dup.Name AND e1.CompanyID=dup.CompanyID
GROUP BY
e1.Employeeid, e1.Name) counts
ON employees.Employeeid = counts.Employeeid
SET
Name = CONCAT(Name, '(', counts.n, ')');
Please see fiddle here.
Related
I have an EMPLOYEE table below:
EMP_ID DEPT_ID
101 1
102 2
103 3
104 1
And a DEPARTMENT table as:
DEPT_ID COUNTS
1
2
3
I want to write a query which would count the number of Employee that belong to a department and store it into Department column table so the Department table will look like:
DEPT_ID COUNTS
1 2
2 1
3 1
I tried:
update department p
set p.counts = (select count(*) from EMPLOYEE e where p.dept_id = e.dept_id
group by e.dept_id)
but it doesn't work.
Remove the group by and the first alias:
update department p
set counts = (select count(*) from EMPLOYEE e where p.dept_id = e.dept_id);
I am working on following SQL:
select *
from `STUDENTLOC` l,
STUDENT s,
ATTENDANCE a
where l.STUDENTID = s.ID
and l.LOCID = 3
Now I need to make sure that the values are not already present in ATTENDANCE table. It has following structure:
ID StudentID ScheduleID
1 6 6
2 3 3
It is a simple list where I need to display list of students whose record have not been added in ATTENDANCE table.
You can use not exists:
select *
from `STUDENTLOC` l
join STUDENT s on l.STUDENTID = s.ID
where not exists (
select 1
from ATTENDANCE a
where a.STUDENTID = l.STUDENTID
)
and l.LOCID = 3
Also, always use modern explicit join syntax instead of comma based join syntax.
Example table student and table payments:
id_student name id id_student datepayment
1 Lisa 1 1 2017-01-01
2 2 1 2017-02-03
3 Asher 3 2 2017-03-05
4 Lee 4 1 2017-03-03
SELECT a.name, a.datepayment
FROM
(SELECT s.name, p.datepayment
FROM
students s
LEFT OUTER JOIN payment p ON s.id_student = p.id_student) AS a
WHERE datepayment IS NULL;
Result:
name datepayment
Asher NULL
Lee NULL
id name count
------------
1 abc
2 xyz
3 xyz
4 xyz
The following query "select count(name) from temp group by name;" gives me:
count(name)
--------
1
3
I want this result to be updated to the column 'count'. To be precise I want my table to look like :
id name count
------------
1 abc 1
2 xyz 3
3 xyz 3
4 xyz 3
You can get those values with a COUNT / GROUP BY. You can do an UPDATE statement which joins your table with the sub query:-
UPDATE temp a
INNER JOIN
(
SELECT name, COUNT(*) AS name_count
FROM temp
GROUP BY name
) b
ON a.name = b.name
SET a.name_count = b.name_count;
If my table looks like the following, how can I select classmates of a given student?
student_class_assn
student_id class_id
1 1
2 1
3 1
4 2
5 2
6 3
For example, Student 1 has students 2 and 3 as classmates.
Please help me write a query that pivots on the known student_id and selects only the classmates (and not including the given student).
Here is an example using a sub-select. But can you help me write it with joins instead?
SET #KNOWN_STUDENT=1;
SELECT
student_id
FROM
student_class_assn
WHERE
class_id IN (
SELECT class_id FROM student_class_assn WHERE student_id = #KNOWN_STUDENT
)
AND student_id != #KNOWN_STUDENT;
Given my table above I expect the following results for each given student_id:
1 returns 2,3
2 returns 1,3
3 returns 1,2
4 returns 5
5 returns 4
6 returns NULL
all you need to do is just join the table and pass a conditional
SELECT t1.student_id
FROM student_class_assn t
JOIN student_class_assn t1
ON t1.class_id = t.student_id
AND t1.student_id <> 1
WHERE t.student_id = 1
FIDDLE
This will not show students with no classmates:
select s1.sid,group_concat(s2.sid order by s2.sid,',')
from
stud_class s1 inner join
stud_class s2 on s1.cid=s2.cid
where s1.sid <> s2.sid
group by s1.sid
order by 1
Having two tables
Department table
//Department
D# DNAME
-------------------
1 SALES
2 ACCOUNTING
3 GAMES
5 SPORTS
Project table
//Project
P# D#
-----------
1001 1
1002 3
1003 5
1004 5
When output display it should be something like:
Department Total Project
---------------------------
1 1
2 0
3 1
5 2
Currently my statement
SELECT D# FROM DEPARTMENT
WHERE (SELECT COUNT(*) FROM PROJECT WHERE DEPARTMENT.D# = PROJECT.D#);
but what should i display 0 if no any project in that D# ?
SELECT D.D#,
COUNT(p.P#)
FROM Department d
LEFT JOIN Project p
ON Project.D#=Department.D
GROUP BY d.D#;
Try This:
SELECT D# , CASE WHEN A.COUNT > 0 THEN D# ELSE 0 END AS TOTAL_PROJECT
FROM DEPARTMENT JOIN (SELECT D# , COUNT(*) FROM PROJECT GROUP BY PROJECT.D#) A ON
DEPARTMENT.D# = A.D#;
A simple left join with group by could achieve such requirement.
SELECT
DEPARTMENT.D#,
COUNT(PROJECT.P#) AS TotalProject
FROM DEPARTMENT
LEFT JOIN PROJECT ON DEPARTMENT.D# = PROJECT.D#
GROUP BY DEPARTMENT.D#
Check below query:
SELECT
D# as 'Dept_ID',count(P#) as 'Count'
from
department d
left join project p
on d.D#=p.D#
group by d.D#;