I've got this tables:
Now I want to get the percentage of hours spent on projects which employees of 'XYZ GmbH' worked on.
So I need to get the complete spent hours by:
SELECT SUM(STDANZ)
FROM MITPRO;
...and the hours spent by XYZ-GmbH on their projects by:
SELECT SUM(mp.STDANZ)
FROM PROJEKT p INNER JOIN MITPRO mp ON (p.PNR = mp.PNR)
INNER JOIN FIRMA f ON (f.FNR = p.FNR)
WHERE f.FNAME='XYZ GmbH';
The formula would then be:
SUM(STDANZ_of_XYZ) / SUM(TOTAL) * 100
I tried this, but I always get an error:
SELECT SUM(mp.STDANZ) / (SELECT SUM(STDANZ) FROM MITPRO)
FROM PROJEKT p INNER JOIN MITPRO mp ON (p.PNR = mp.PNR)
INNER JOIN FIRMA f ON (f.FNR = p.FNR)
WHERE f.FNAME='XYZ GmbH';
The error says that it's not a single-group group function.
What should I do?
Thanks, Julian
SELECT AA.FSUM/BB.TSUM FROM
(
SELECT SUM(mp.STDANZ) AS FSUM
FROM PROJEKT p INNER JOIN MITPRO mp ON (p.PNR = mp.PNR)
INNER JOIN FIRMA f ON (f.FNR = p.FNR)
WHERE f.FNAME='XYZ GmbH'
) AA ,
(
SELECT SUM(STDANZ) AS TSUM FROM MITPRO
)BB
Here AA and BB are intermediate temp tables which contain single sum row each. We are doing a CROSS JOIN on these 2 single row tables which essentially gives a single row.
You want to get the percentage of hours spent on projects which employees of 'XYZ GmbH' worked on (which means for each employee and not for all employees together), here's one way of doing it, there are 2 steps to this:
See below query:
--1. Identify the total hours for the project itself (so as to arrive at the percentage); from your question,
-- we need to consider only xyz gmbh project
DECLARE @TOTAL_SUM INT
SET @TOTAL_SUM =
(SELECT
SUM(MI.STDANZ) TOTAL_SUM
FROM
FIRMA F
INNER JOIN PROJEKT P
ON F.FNR = P.FNR
INNER JOIN MITPRO MI
ON MI.PNR = P.PNR
--INNER JOIN MITARBEITER M
--ON M.MNR = MI.MNR
WHERE
F.FNAME = 'XYZ GmbH')t
--2. Identify the total hours for each employee for the xyz gmbh project
SELECT
M.MNR -- Employee id
,M.MName -- Employee name
,SUM(MI.STDANZ) TOTAL_SUM -- Total hours
, (SUM(MI.STDANZ) / @TOTAL_SUM)*100 AS PERCENTAGE --Percentage of hours
FROM
FIRMA F
INNER JOIN PROJEKT P
ON F.FNR = P.FNR
INNER JOIN MITPRO MI
ON MI.PNR = P.PNR
INNER JOIN MITARBEITER M
ON M.MNR = MI.MNR
WHERE
F.FNAME = 'XYZ GmbH'
GROUP BY
M.MNR
,M.MNAME
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.