0

我有一个非常简单的 SQL -

SELECT      QUEUE,
            COUNT (QUEUE) AS 'TOTAL'
FROM        MY_TABLE
GROUP BY    QUEUE

这会产生 10 行数据,例如 -

First Queue    15  
Second Queue   23  
Third Queue    48  
Fourth Queue   12  
etc

我需要做的是将第一个队列的结果添加到第二个队列的结果中,然后将第二个队列的名称更改为新的第二个队列,所以我应该以 -

New Second Queue   38  
Third Queue        48  
Fourth Queue       12  
etc

我写 SQL 的时间不长,花了很多时间试图解决这个问题,但没有成功。

4

2 回答 2

1
select queue,
       count(*) as total
from
  (
  select case queue
           when 'First Queue' then 'New Second Queue'
           when 'Second Queue' then 'New Second Queue'
           else queue
         end as queue
  from YourTable
  ) as Q
group by queue      
于 2012-08-14T08:53:04.817 回答
0

尝试这个:

with cte as(
SELECT      QUEUE,
            COUNT (QUEUE) AS 'TOTAL',
            ROW_NUMBER() over(order by QUEUE) as rownum
FROM        MY_TABLE
GROUP BY    QUEUE)
select 'New '+QUEUE [QUEUE],TOTAL+(select TOTAL from cte where rownum=1) [TOTAL]
 from cte where rownum=2
 union all
 select QUEUE,TOTAL from cte where rownum>2
于 2012-08-14T08:50:25.093 回答