不同地点的每位员工的工资百分比

时间:2017-03-08 14:43:28

标签: sql

当我试图获得在不同地点工作的每个员工的薪水%时,我遇到了一些困难,并且使用该%计算每个地点的员工工资贡献。我已经附上了表结构和预期结果。我试图使用以下查询,但我想我错过了将它们组合在一起的东西。查询中存在错误。有人可以帮我吗?

Table Structure and Expected Result

SELECT p.[Period End Date], p.[EE#], p.Department, 
(p.[Gross Wages]/ SUM([Gross Wages])) 
WHERE [Location] = 'A' AS 'Gross Wages % in Location A',
(p.[Gross Wages]/ SUM[Gross Wages]) 
WHERE [Location] = 'B' AS 'Gross Wages % in Location B',
b.[Health Insurance]*(p.[Gross Wages]/ SUM[Gross Wages]) 
WHERE [Location] = 'A' AS 'Health Insurance % in Location A',
b.[Health Insurance]*(p.[Gross Wages]/ SUM[Gross Wages]) 
WHERE [Location] = 'B' AS 'Health Insurance % in Location B',
c.[Tax]*(p.[Gross Wages]/ SUM[Gross Wages]) 
WHERE [Location] = 'A' AS 'Tax % in Location A',
c.[Tax]*(p.[Gross Wages]/ SUM[Gross Wages]) 
WHERE [Location] = 'B' AS 'Tax % in Location B',
FROM [Table A] p
INNER JOIN [Table B] b ON p.[Employee Number] = b.[Employee Number]
INNER JOIN [Table C] b ON p.[Employee Number] = c.[Employee Number]
GROUP BY p.[location]
Order by p.[Employee Number]
    INNER JOIN [Table B] b ON p.[Employee Number] = b.[Employee Number]
    INNER JOIN [Table C] b ON p.[Employee Number] = c.[Employee Number]
    GROUP BY p.[location]
    Order by p.[Employee Number]

查询已编辑的更新。 错误: 1.预期'发现其他''第2行 2.找到位置A'的总工资%但是预计'。第2行 3.找到位置A'的总工资%但是预计'。第2行

SELECT p.[Period End Date], p.[EE#], p.Department, 
    SUM(CASE WHEN [Location]='A' THEN(p.[Gross Wages]/ SUM([Gross Wages]) ELSE '0' END) AS 'Gross Wages % in Location A'),
    SUM(CASE WHEN [Location]='B' THEN(p.[Gross Wages]/ SUM([Gross Wages]) ELSE '0' END) AS 'Gross Wages % in Location B'),
    SUM(CASE WHEN [Location]='A' THEN(b.[Health Insurance]*(p.[Gross Wages]/SUM([Gross Wages]) ELSE '0' END) AS 'Health Insurance in Location A'),
    SUM(CASE WHEN [Location]='B' THEN(b.[Health Insurance]*(p.[Gross Wages]/ SUM([Gross Wages]) ELSE '0' END) AS 'Health Insurance in Location B'),
    SUM(CASE WHEN [Location]='A' THEN(b.[Tax]*(p.[Gross Wages]/ SUM[Gross Wages]) ELSE '0' END) AS 'Tax in Location A'),
    SUM(CASE WHEN [Location]='B' THEN(b.[Tax]*(p.[Gross Wages]/ SUM[Gross Wages]) ELSE '0' END) AS 'Tax in Location B'),
FROM [Table A] p
INNER JOIN [Table B] b ON p.[EE#] = b.[EE#]
INNER JOIN [Table C] b ON p.[EE#] = c.[EE#]
GROUP BY p.[EE#], p.[location]
ORDER BY p.[EE#]

1 个答案:

答案 0 :(得分:0)

对正在发生的事情做了很多猜测,但这应该很接近:

我在下面做的关键是使用JOINing来展平结果并减少CASE语句。在这里,您只需要有两个连接,然后使用正确的连接进行列的计算,而不必执行两个案例语句和一个组。

SELECT 
  P.[EE#], P.location,
--  p.[Period End Date], 
--  p.[EE#], p.Department,
  (AA.[Gross Wages]/ SUM(AA.[Gross Wages])  AS 'Gross Wages % in Location A',
  (AB.[Gross Wages]/ SUM(AB.[Gross Wages])  AS 'Gross Wages % in Location B',
  BA.[Health Insurance]*(AA.[Gross Wages]/ SUM(AA.[Gross Wages])) AS 'Health insurance Location A',
  BB.[Health Insurance]*(AB.[Gross Wages]/ SUM(AB.[Gross Wages])) AS 'Health insurance Location B'
-- etc something with CA and CB
FROM (SELECT distinct [EE#], location FROM [Table A]) AS P
LEFT JOIN [TABLE A] AA ON p.[EE#] = AA.[EE#] AND BA.Location = 'A'
LEFT JOIN [TABLE A] AB ON p.[EE#] = AB.[EE#] AND BB.Location = 'B'
LEFT JOIN [TABLE B] BA ON p.[EE#] = BA.[EE#] AND BA.Location = 'A'
LEFT JOIN [TABLE B] BB ON p.[EE#] = BB.[EE#] AND BB.Location = 'B'
LEFT JOIN [TABLE C] CA ON p.[EE#] = CA.[EE#] AND CA.Location = 'A'
LEFT JOIN [TABLE C] CB ON p.[EE#] = CB.[EE#] AND CB.Location = 'B'
GROUP BY p.[EE#], p.[location]
Order by p.[EE#]

另请注意:

我希望您能希望以下查询获得所有EE#不仅仅是[表A]中的那些:

FROM (
  SELECT distinct [EE#], location FROM [Table A]
  UNION
  SELECT distinct [EE#], location FROM [Table B]
  UNION
  SELECT distinct [EE#], location FROM [Table C]
) AS P

当然,如果你的某个员工表更好,那么你可以做一些像

这样的事情
FROM (SELECT DISTINCT [EE#], location FROM Employee) AS P

然后,如果您的员工不在A,B或C中 - 他们仍然会出现在您的报告中。