department wise Sum of salary of employee with each employee details [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I Have following table which contains details of Employee
EmpId EmpName Mgr Salary Dept
1 Deepak 2 20000 1
2 Annu NULL 22000 1
3 Jai 2 19500 1
4 Jitendra 1 18000 2
5 Vaishali 1 18000 2
6 Philip 4 15000 3
I wants to show salary of each dept with each employee details,if it repeats no issues as shown below
EmpId EmpName Mgr Salary Dept DeptSal
1 Deepak 2 20000 1 61500
2 Annu NULL 22000 1 61500
3 Jai 2 19500 1 61500
4 Jitendra 1 18000 2 36000
5 Vaishali 1 18000 2 36000
6 Philip 4 15000 3 15000

You should look into the SUM() OVER(PARTITION) windowing functions in SQL Server.
See this MSDN link
This link should help you in solving your problem.
If you are still compelled to get a solution rather than understanding how you can solve these type of problems, then answer is mentioned in a single line as spoiler below
select *, DeptSal=sum(Salary) over (partition by Dept ) from t

Related

SQL JOIN,LEFT JOIN,RIGHT JOIN [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I have 3 tables like
TAB_1
ID
NUMBER
1
101
2
102
3
103
4
104
5
105
6
106
7
107
8
108
9
109
10
110
TAB_2
ID
NUMBER
1
101
2
102
3
105
TAB_3
ID
NUMBER
1
104
2
107
3
110
The output needs to be:
ID
NUMBER
1
103
2
106
3
108
4
109
I think u can use NOT IN with Subqueries
Like;
SELECT
*
FROM
Table_1
WHERE
Number NOT IN (Select number from Table_2) and
Number NOT IN (Select number from Table_3)

How to build a SQL query for the below requirement? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Let's suppose and EMP_Table consists of following columns
EMPID,SAL,DEPTID,MGRID
Req: List all managers whose salary is > all of team members salary
Sample Input:
EmpID SAL DEPTID MGRID
101 10000 A 102
102 30000 A 102
103 15000 A 102
104 10000 B 106
105 30000 B 106
106 45000 B 106
107 50000 B 106
108 20000 C 109
109 50000 C 109
110 45000 C 109
Expected output:
EmpID SAL DEPTID MGRID
102 30000 A 102
109 50000 C 109
SELECT M.EmpID, M.Salary, M.DepId, M.MgrId
FROM dbo.Employee E INNER JOIN
dbo.Employee M ON
E.EmpID = M.MgrId
WHERE E.Salary >= (SELECT MAX(DE.Salary) FROM dbo.Employee DE WHERE DE.DepId = E.DepId)
AND M.Salary >= E.Salary

How to update a table with data from same table in sql [duplicate]

This question already has answers here:
SQL How to Update SUM of column over group in same table
(3 answers)
Closed 2 years ago.
I have a table in SQL which I want to update
NAME Emp_ID Points TotalPoints
ABC 1 50 0
ABC 1 40 0
XYZ 2 20 0
LMN 3 30 0
LMN 3 50 0
XYZ 2 10 0
LMN 3 5 0
Please help me to update the same table as shown below by summing up the points
NAME Emp_ID Points TotalPoints
ABC 1 50 90
ABC 1 40 90
XYZ 2 20 30
LMN 3 30 85
LMN 3 50 85
XYZ 2 10 30
LMN 3 5 85
I only tried the below SQL on Oracle 18c database, but I believe it is fairly standard SQL and so should work with all the major DBMS
update EMPS E1
set E1.TOTAL_POINTS = (select sum(E2.POINTS)
from EMPS E2
where E2.EMP_ID = E1.EMP_ID)
Try the following, here is the DEMO. This code work for MySQL 8.0, SQL Server and PostgreSQL.
select
name,
emp_id,
points,
sum(points) over (partition by emp_id) as total_points
from yourTable
order by
name

Is it possible to select a distinct column and get another column's value count? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I think I might know enough to do parts individually but is it possible to do it with one statement? I need to display a model count for each year it appears in.
I have the following data:
id model year
-----------------
1 45A 1992
2 45A 1992
3 45B 1992
4 45A 1996
5 45B 1996
6 33C 2000
7 33C 2000
8 45B 2000
9 45B 2010
It should come out something like:
year model count
------------------
1992 45A 2
1992 45B 1
1996 45A 1
1996 45B 1
2000 33C 2
2000 45B 1
2010 45B 1
How do I accomplish this in SQL? Is it a group by year and count the models?
Unless I'm overlooking something, you just need to GROUP BY the two columns you're interested in. I changed the name of the last column to avoid any keyword issues.
SELECT
year,
model,
count(*) as modelcount
FROM
table
GROUP BY
year,
model
ORDER BY
year;

postgresql query to delete duplicate entries in a table [duplicate]

This question already has answers here:
How to delete duplicate entries?
(16 answers)
Closed 6 years ago.
I have a table as :
id product id merchant id price upc
1 124 2 2000000 1234XDE
2 124 2 200000 1234XDE
3 124 2 200000 1234XDE
4 124 2 200000 1234XDE
5 124 2 200000 ASDER36
6 134 1 300 ASERT56
7 134 2 300 ASERT56
I want to delete all the multiple entries from the table.
Delete from
table where id not in (Select min(id) from table group by(merchant id))
but no success. I want resulting table as:
id product id merchant id price upc
1 124 2 2000000 1234XDE
5 124 2 2000000 ASDER36
6 134 1 300 ASERT56
7 134 2 300 ASERT56
Can someone help me in writing a query for this.
This should do it:
delete from flash
where id not in (select min(id)
from flash
group by product_id, merchant_id, upc);
SQLFiddle example: http://sqlfiddle.com/#!15/9edef/1