0

我有以下 SQL 语句:

SELECT   TRUNC(TIDSPUNKT) AS PERIOD,
         QUEUE,
         ROUND(SUM(CASE WHEN BESVARET_25_SEK > 0 THEN BESVARET_25_SEK END ) / SUM(CASE WHEN ANTAL_KALD > 0 THEN ANTAL_KALD END) * 100) AS SVAR_PROCENT
FROM     KS_DRIFT.PERO_NKM_KØ_OVERSIGT
WHERE    TIDSPUNKT >= '2013-06-17'
AND      TIDSPUNKT <= '2013-07-02'
AND      ANTAL_KALD > 0
AND QUEUE not in ('TekniskHotline')
GROUP BY TRUNC(TIDSPUNKT), QUEUE
ORDER BY PERIOD

这给了我以下结果:

SQL 结果

如您所见,我已经突出显示了两行。在上面的 SQL 语句中,我现在计算两行以获得一个百分比,这是棘手的部分,我的问题不是在一行上有“Erhverv”和“ErhvervOverflow”,而是我想将它们算作一个!

有没有办法做到这一点?

4

1 回答 1

1

您可以使用case语句来分配 和selectgroup by

SELECT   TRUNC(TIDSPUNKT) AS PERIOD,
         (case when QUEUE in ('Erhverv', 'ErhvervOverflow') then 'Erhverv'
               else QUEUE
          end) as QUEUE,
         ROUND(SUM(CASE WHEN BESVARET_25_SEK > 0 THEN BESVARET_25_SEK END ) / SUM(CASE WHEN ANTAL_KALD > 0 THEN ANTAL_KALD END) * 100) AS SVAR_PROCENT
FROM     KS_DRIFT.PERO_NKM_KØ_OVERSIGT
WHERE    TIDSPUNKT >= '2013-06-17'
AND      TIDSPUNKT <= '2013-07-02'
AND      ANTAL_KALD > 0
AND QUEUE not in ('TekniskHotline')
GROUP BY TRUNC(TIDSPUNKT),
         (case when QUEUE in ('Erhverv', 'ErhvervOverflow') then 'Erhverv'
               else QUEUE
          end)
ORDER BY PERIOD;

请注意,我在when子句中包含了这两个值。你可以这样做: when QUEUE = 'ErhvervOverflow' then 'Erhverv' else QUEUE end)。我认为包含这两个值会使意图更加清晰。

于 2013-07-03T14:47:30.403 回答