表a:
车牌号
苏EQ7654
苏EA1234
京AQ1234
苏EM1234
苏EN7541
。
。
就这么一张简单的表a,就一个字段车牌号,现有如下要求:
苏EM、苏EN —— 为一组
除苏EM、苏EN之外的以苏E开头的 —— 为一组
余下的为一组 —— 为一组
总共三组。请写出统计这三组每一组的数量的sql语句,因为这张表的元组数目很多,达几十万,所以sql性能要求比较高!(尽量少用连接和数据库自带的字符API串函数)
其实,我已经写了出来,但效率不高。汗 特来寻找高人记住,只有一条sql语句哦
车牌号
苏EQ7654
苏EA1234
京AQ1234
苏EM1234
苏EN7541
。
。
就这么一张简单的表a,就一个字段车牌号,现有如下要求:
苏EM、苏EN —— 为一组
除苏EM、苏EN之外的以苏E开头的 —— 为一组
余下的为一组 —— 为一组
总共三组。请写出统计这三组每一组的数量的sql语句,因为这张表的元组数目很多,达几十万,所以sql性能要求比较高!(尽量少用连接和数据库自带的字符API串函数)
其实,我已经写了出来,但效率不高。汗 特来寻找高人记住,只有一条sql语句哦
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-03-08 22:47:57
-- Verstion:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([车牌号] varchar(8))
insert [tb]
select '苏EQ7654' union all
select '苏EA1234' union all
select '京AQ1234' union all
select '苏EM1234' union all
select '苏EN7541'
--------------开始查询--------------------------
select
sum(case when substring(车牌号,1,3) in('苏EM','苏EN') then 1 else 0 end),
sum(case when substring(车牌号,1,3) not in('苏EM','苏EN') and substring(车牌号,1,2) like '%苏E' then 1 else 0 end),
sum(case when substring(车牌号,1,2)<>'苏E' then 1 else 0 end)
from [tb]
----------------结果----------------------------
/* ----------- ----------- -----------
2 2 1(1 行受影响)
*/
sum(case when left(车牌号,2)='苏E'
and (left(车牌号,3)<>'苏EM' or left(车牌号,3))<>'苏EN' then 1 else 0 end),
sum(case when left(车牌号,2)<>'苏E' then 1 else 0)
from tb
sum(case when left(车牌号,2)='苏E'
and (left(车牌号,3)<>'苏EM' or left(车牌号,3))<>'苏EN' then 1 else 0 end) as [2],
sum(case when left(车牌号,2)<>'苏E' then 1 else 0 end) as [3]
from tb
sum(case when left(车牌号,2) = '苏E' and left(车牌号,3) not in ('苏EM','苏EN') then 1 else 0 end) [2],
sum(case when left(车牌号,2) <> '苏E' then 1 else 0 end) [3]
from tb
case when left(CarId,3) = '苏EM' or left(CarId,2) = '苏EN' then CarId else '' end as '苏EM',
case when left(CarId,2) = '苏E' and left(CarId,3) != '苏EN' and left(CarId,3) != '苏EM' then CarId else '' end as '苏E',
case when left(CarId,2) != '苏E' then CarId else '' end as '其他'
from tb
select CarId,
case when left(CarId,3) = '苏EM' or left(CarId,3) = '苏EN' then CarId else '' end as '苏EM',
case when left(CarId,2) = '苏E' and left(CarId,3) != '苏EN' and left(CarId,3) != '苏EM' then CarId else '' end as '苏E',
case when left(CarId,2) != '苏E' then CarId else '' end as '其他'
from tb
我提交的是一条sql去远程书库库,不能建立临时表,所有的数据库操作都是用API封装好的,所以只能写一条sql
这也是难点
case
when 车牌号 like '苏E[MN]%' then 1
when 车牌号 like '苏E%' then 2
else 3
end,
count(1) as cn
from tb group by
case
when 车牌号 like '苏E[MN]%' then 1
when 车牌号 like '苏E%' then 2
else 3
end
select (case when (车牌号 like '苏EM%' or 车牌号 like '苏EN%') then 'EN/EM'
else (case when (车牌号 like '苏E%') then 'E' else 'else' end) end) as Gid,
count(车牌号)as 条数 from #carTB
group by
(case when (车牌号 like '苏EM%' or 车牌号 like '苏EN%') then 'EN/EM'
else (case when (车牌号 like '苏E%') then 'E' else 'else' end) end)