我有这张桌子:
现在,我希望获得“XYZ GmbH'”员工所花费的时间百分比。工作。
所以我需要通过以下方式获得完整的花费时间:
SELECT SUM(STDANZ)
FROM MITPRO;
......以及XYZ-GmbH在他们的项目上花费的时间:
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';
然后公式为:
SUM(STDANZ_of_XYZ) / SUM(TOTAL) * 100
我尝试了这个,但我总是收到错误:
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';
该错误表示它不是单组群组功能。
我该怎么办?
谢谢, 儒略
答案 0 :(得分:0)
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
这里AA和BB是中间临时表,每个临时表包含单个总和行。我们在这两个单行表上进行CROSS JOIN,基本上只给出一行。
答案 1 :(得分:0)
您希望获得“XYZ GmbH'员工”项目所花费的时间百分比。工作(这意味着每个员工,而不是所有员工在一起),这是一种方法,有两个步骤:
见下面的查询:
--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