Microsoft SQL Server 2012 中带有计数和条件的嵌套 SQL 查询

Nested SQL query with counts and where conditions in Microsoft SQL Server 2012

我在 table GAontime 中有以下数据。 OrgEinheit表示部门,GALaufzeit是吞吐时间:

CREATE TABLE GAontime
    ([Auftragsart] varchar(9), 
     [GAZurueckIST] varchar(23), 
     [GALaufzeit] int, 
     [OrgEinheit] varchar(9), 
     [GAErteilungsJahr] int
    );

INSERT INTO GAontime
    ([Auftragsart], [GAZurueckIST], [GALaufzeit], [OrgEinheit], [GAErteilungsJahr])
VALUES
    ('Gutachten', '2003-10-02 00:00:00.000', 2, 'FE11', 2003),
    ('Gutachten', '2003-10-09 00:00:00.000', 2, 'FE12', 2003),
    ('Gutachten', '2003-11-13 00:00:00.000', 3, 'FE12', 2003),
    ('Gutachten', '2006-12-01 00:00:00.000', 1045, 'FO11', 2004),
    ('Gutachten', '2003-11-18 00:00:00.000', 4, 'FE11', 2003),
    ('Gutachten', '2003-11-25 00:00:00.000', 6, 'FE12', 2003),
    ('Gutachten', '2003-12-11 00:00:00.000', 3, 'FE11', 2003),
    ('Gutachten', '2003-12-10 00:00:00.000', 2, 'FE12', 2003),
    ('Gutachten', '2003-12-19 00:00:00.000', 1, 'FE11', 2003),
    ('Gutachten', '2004-01-08 00:00:00.000', 1, 'FE11', 2004),
    ('Gutachten', '2004-02-25 00:00:00.000', 34, 'FE11', 2004),
    ('Gutachten', '2004-03-19 00:00:00.000', 57, 'FE12', 2004),
    ('Gutachten', '2004-02-25 00:00:00.000', 35, 'FE11', 2004),
    ('Gutachten', '2004-08-12 00:00:00.000', 94, 'FO1', 2004),
    ('Gutachten', '2004-02-05 00:00:00.000', 1, 'FE11', 2004),
    ('Gutachten', '2004-02-12 00:00:00.000', 6, 'FE12', 2004),
    ('Gutachten', '2004-06-18 00:00:00.000', 121, 'FE11', 2004),
    ('Gutachten', '2004-02-25 00:00:00.000', 7, 'FE11', 2004),
    ('Gutachten', '2004-03-05 00:00:00.000', 10, 'FE12', 2004),
    ('Gutachten', '2004-02-25 00:00:00.000', 0, 'FE11', 2004),
    ('Gutachten', '2004-03-19 00:00:00.000', 8, 'FE12', 2004)
;

我最终想要得到的是一个table这样的:

OrgEinheit GAErteilungsJahr TotalNumber NumberFaster22d Percentage
FE11       2003             10          3               0.3
FE11       2004             20          6               0.3
FE12       ...

所以我需要的是:

  1. 每年'OrgEinheit'的请求总数

  2. 提前期快于 22 天的请求数量

  3. 1.) + 2.)的计算百分比

对于 1.) 我设法得到了

的结果
SELECT OrgEinheit, 
COUNT(*) AS Anzahl, GAErteilungsJahr
FROM GAontime
GROUP BY OrgEinheit, GAErteilungsJahr

对于 2.) 我有:

SELECT OrgEinheit, Count(*) As Total
FROM GAontime
WHERE (GALaufzeit < 22)
GROUP BY OrgEinheit, GAErteilungsJahr

其余的我被语法问题搞得一团糟,因为我想在单个 SQL 查询中执行此操作。

我计划的以下步骤是:

我如何编写这么大的 SQL 查询?感谢任何帮助。

您正在寻找这样的东西吗?您的数据和示例并不完全匹配,但这是根据您的尝试得出的:

select
  *,
  cast(NumberFaster22d as float) / TotalNumber as Percentage
from
(
  select
    OrgEinheit,
    GAErteilungsJahr,
    sum(1) as TotalNumber,
    sum(case when GALaufzeit < 22 then 1 else 0 end) as NumberFaster22d  
  from
    GAontime
  group by
    OrgEinheit,
    GAErteilungsJahr
) X
order by
  OrgEinheit,
  GAErteilungsJahr

大小写仅用于计算值 < 22 的行,导出的 table 用于计算比率。

您可以在 SQL Fiddle

中进行测试

您可以通过应用 CASE .. WHEN 投影并计算正匹配来在同一个查询中组合 'filter'。然后,您可以应用百分比计算,而无需使用派生的 table 或 CTE:

重复计算
SELECT OrgEinheit, GAErteilungsJahr, TotalNumber, NumberFaster22d, 
    CAST(NumberFaster22d AS DECIMAL(10,2)) / TotalNumber AS Percentage
FROM
(
    SELECT OrgEinheit, GAErteilungsJahr, COUNT(*) AS TotalNumber, 
           SUM(Case WHEN GALaufzeit < 22 THEN 1 ELSE 0 END) AS NumberFaster22d
    FROM GAontime
    GROUP BY OrgEinheit,GAErteilungsJahr
) x
ORDER BY OrgEinheit,GAErteilungsJahr;