操纵两个不同的行

时间:2013-07-03 14:34:39

标签: sql if-statement plsql count

我有以下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 RESULT

正如你所看到的,我已经高了两行。在上面的SQL语句我现在计算两行来获得一个百分比是一个棘手的部分和我的问题而不是在行上有“Erhverv”和“ErhvervOverflow”我想把它们算作一个!

有没有办法实现这个目标?

1 个答案:

答案 0 :(得分: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)。我认为包含这两个值会使意图更清晰。