Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 October SQL Puzzle: SQL Advance Query – Find the average shared salary by each department

SQL Puzzle: SQL Advance Query – Find the average shared salary by each department

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to find the average percentage of shared salary by each employee department.

Input Data:

1
2
3
4
5
6
7
8
9
10
11
12
EmpId EmpName DeptID Salary
----------- ---------- ----------- -----------
1 ABC 101 10000
2 XYZ 101 20000
3 FGH 102 25000
4 WER 102 30000
5 TYU 102 40000
6 QWE 103 10000
7 XCV 103 15000
8 ASD 104 18000
9 UIO 104 20000
10 JKL 104 35000

Expected Output:

1
2
3
4
5
6
DeptID DeptTotalSalary AllTotal SharedPercentage
----------- --------------- ----------- ----------------
101 30000 223000 13
102 95000 223000 42
103 25000 223000 11
104 73000 223000 32

Create a table with data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CREATE TABLE tbl_Emp
(
EmpId INT
,EmpName VARCHAR(10)
,DeptID INT
,Salary INT
)
GO
INSERT INTO tbl_Emp VALUES
(1,'ABC',101,10000)
,(2,'XYZ',101,20000)
,(3,'FGH',102,25000)
,(4,'WER',102,30000)
,(5,'TYU',102,40000)
,(6,'QWE',103,10000)
,(7,'XCV',103,15000)
,(8,'ASD',104,18000)
,(9,'UIO',104,20000)
,(10,'JKL',104,35000)
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
;with ctetest as
(
select DeptID, SUM(Salary) as DeptTotalSalary
from tbl_Emp
group by DeptID
)
,cte as
(
select SUM(Salary) as AllTotal
from tbl_Emp
)
,cte2 as
(
select DeptID,DeptTotalSalary,AllTotal from ctetest as a
cross apply
(
select AllTotal from cte
) as t
)
select *, round(((DeptTotalSalary *100))/AllTotal,2) as SharedPercentage
from cte2

Please try the different solution for this puzzle and share it via comment...

Oct 24, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Find the date of all third Sunday for Year 2017SQL Puzzle: SQL Advance Query - Generate the report on Missing Year - Month data
Comments: 4
  1. Dinesh.IS
    October 25, 2017 at 5:28 am

    Declare @Total As Numeric(19,2);
    Select @Total=Sum(IsNull(Salary,0)) From TBL_EMP
    Select DeptID,Sum(IsNull(Salary,0)) As Salary,@Total As AllTotal,Floor(((Sum(IsNull(Salary,0))/IsNull(@Total,0))*100)) As SharedPercentage
    From tbl_Emp
    Group By DeptID

    ReplyCancel
  2. Ankush
    October 1, 2018 at 6:54 am

    Select dept_id, DeptTotalSalary, AllTotal ,ROUND((DeptTotalSalary*100)/AllTotal,3) as percent from (Select foo.emp_id, foo.emp_name, foo.dept_id, foo.salary, foo2.AllTotal,DeptTotalSalary from tbl_Emp foo
    CROSS JOIN
    (select sum(salary) as AllTotal from tbl_Emp )foo2
    JOIN
    (Select sum(salary) as DeptTotalSalary, dept_id from tbl_Emp group by dept_id) foo3 ON foo.dept_id = foo3.dept_id) foo4
    group by dept_id, DeptTotalSalary, AllTotal

    ReplyCancel
  3. Lakshman
    April 27, 2019 at 5:37 am

    SELECT distinct DeptId,SUM(Salary) OVER (PARTITION BY DeptId) AS DeptTotalSalary,SUM(Salary) over () AS AllTotal
    ,SUM(Salary) OVER (PARTITION BY DeptId)*100/SUM(Salary) over () AS SharedPercentage
    from tbl_emp

    ReplyCancel
  4. ANIL
    August 1, 2019 at 10:56 am

    SELECT DEPTID,DEPTSAL,TOTSAL,(DEPTSAL*100/TOTSAL)AS PER
    FROM

    (SELECT DEPTID,SUM(SALARY)AS DEPTSAL,
    (SELECT SUM(A.SALARY) AS TOTSAL FROM TBL_EMP A WHERE A.DEPTID=DEPTID) AS TOTSAL

    FROM TBL_EMP GROUP BY DEPTID ORDER BY DEPTID) AS F

    ReplyCancel

Leave a Reply to Ankush Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

October 24, 2017 4 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....