日期:2014-05-18 浏览次数:20521 次
select '类别(含EM,EN,EX)' '类别1', sum(case when(charindex('EM',tag)>0 or charindex('EN',tag)>0 or charindex('EX',tag)>0) then id else 0 end) '总数' ,'类别(除类别1外,以E开头)' '类别2', sum(case when(charindex('EM',tag)<=0 and charindex('EN',tag)<=0 and charindex('EX',tag)<=0 and charindex('E',tag)=2) then id else 0 end) '总数' ,'类别(其它)' '类别3', sum(case when(charindex('E',tag)<=0) then id else 0 end) '总数' from [tb]
------解决方案--------------------
//前面回复错了,这个为准
--建表 create table #CarMonitor(ID int, CarID varchar(20)) --数据 insert into #CarMonitor select 1,'苏EM' union all select 2,'苏EM' union all select 3,'苏EX' union all select 4,'苏EG' union all select 5,'京A' union all select 6,'渝G' union all select 7,'苏A' --分组统计 select case charindex('苏E',CarID) when 0 then '其他' else case charindex(CarID,'苏EM,苏EN,苏EX') when 0 then '非苏EM苏EN苏EX' else '苏EM苏EN苏EX' end end as CarGroup, count(0) as [count] from #CarMonitor group by case charindex('苏E',CarID) when 0 then '其他' else case charindex(CarID,'苏EM,苏EN,苏EX') when 0 then '非苏EM苏EN苏EX' else '苏EM苏EN苏EX' end end --结果 CarGroup count -------------- ----------- 非苏EM苏EN苏EX 1 其他 3 苏EM苏EN苏EX 3 (3 行受影响)
------解决方案--------------------
--显示 SELECT * FROM table_name ORDER BY CASE WHEN CHARINDEX(LEFT(车牌号, 3), N'苏EM,苏EN,苏EX') > 0 THEN 1 --苏EM,苏EN,苏EX为一组 WHEN LEFT(车牌号, 2) = N'苏E' THEN 2 --不包括以上三类且以苏E开头的为一组 ELSE 3 --其它3 END --统计 SELECT 车牌号类型 = CASE WHEN CHARINDEX(LEFT(车牌号, 3), N'苏EM,苏EN,苏EX') > 0 THEN N'苏EM|苏EN|苏EX' WHEN LEFT(车牌号, 2) = N'苏E' THEN N'苏E' ELSE N'其它' END, 摄像头数量 = COUNT(1) FROM table_name
------解决方案--------------------
因为你用的是oracle,这里是SQL SERVER版,我们的是T-SQL!
oracle里用 instr 函数代替charindex