0
select to_char(T_12895_DET_ENTERED_DATE,'yyyy-mm') as entered_date, 
CASE
    when T_12916_VIA = 'E'   then 'Internet'
    when T_12916_VIA = 'R'   then 'Store'
    when (T_12916_VIA in ('M','F','P') or T_12916_VIA is null) then 'All Others'
end as VIA_CODE,
count(*)
from cmlbrc.applicants
where to_char(T_12895_DET_ENTERED_DATE,'yyyy') >= '2010'
group by to_char(T_12895_DET_ENTERED_DATE,'yyyy-mm'), T_12916_VIA
order by 1,2;

上面的代码给了我多行作为 yyyy-mm 的输出。为什么“所有其他人”不能组合成一排?2010-05 所有其他 278 2010-05 所有其他 975 2010-05 所有其他 223 2010-05 互联网 5124 2010-05 商店 19641

谢谢丹

4

2 回答 2

1

您可以将您的CASE声明移动到您GROUP BY的应该删除重复项:

select to_char(T_12895_DET_ENTERED_DATE,'yyyy-mm') as entered_date, 
   CASE
       when T_12916_VIA = 'E'   then 'Internet'
       when T_12916_VIA = 'R'   then 'Store'
       when (T_12916_VIA in ('M','F','P') or T_12916_VIA is null) then 'All Others'
   end as VIA_CODE,
   count(*)
from cmlbrc.applicants
where to_char(T_12895_DET_ENTERED_DATE,'yyyy') >= '2010'
group by to_char(T_12895_DET_ENTERED_DATE,'yyyy-mm'), 
   CASE
       when T_12916_VIA = 'E'   then 'Internet'
       when T_12916_VIA = 'R'   then 'Store'
       when (T_12916_VIA in ('M','F','P') or T_12916_VIA is null) then 'All Others'
   end
order by 1,2;
于 2013-05-28T12:39:10.867 回答
0

请参考以下脚本:

您还需要在 group by 子句中指定 case 块。

Create Table #Temp1(T_12895_DET_ENTERED_DATE smalldatetime,T_12916_VIA char(1))

insert into #Temp1 ( T_12895_DET_ENTERED_DATE,T_12916_VIA )
Select dateadd( d,id,getdate()), case When a.ID <= 10 Then 'E'
When a.ID <= 20 Then 'R'
When a.ID > 20 Then 'M' End
  from Tally As a
Where a.ID < 30
Order by a.ID 

Select * from #Temp1

select Year(T_12895_DET_ENTERED_DATE) as entered_date, 
CASE
    when T_12916_VIA = 'E'   then 'Internet'
    when T_12916_VIA = 'R'   then 'Store'
    when (T_12916_VIA in ('M','F','P') or T_12916_VIA is null) then 'All Others'
end as VIA_CODE,
count(*)
from #Temp1
group by Year(T_12895_DET_ENTERED_DATE) , 
CASE
    when T_12916_VIA = 'E'   then 'Internet'
    when T_12916_VIA = 'R'   then 'Store'
    when (T_12916_VIA in ('M','F','P') or T_12916_VIA is null) then 'All Others'
end 
order by 1,2
于 2013-05-28T12:47:29.580 回答