这些数分别属于不同的区域01 05 09 13 是属于1区
02 06 10 14 是属于2区
03 07 11 15 是属于3区
04 08 12 16 是属于4区现在有一列记录
LH
16
15
02
04
05
05
02
10
06
04
14
13
05
06
16
13
13
06
03
02
04
13
03
11
06
02
04
14执行语句后LH[em]所属区域
16[em]4-------'16'这个数字属于第‘4’区域,记作‘4’15[em]3
02[em]2
04[em]4
05[em]1
05[em]1
02[em]2
10[em]2-----'10'这个数字属于第‘2’区域,记作‘2’06[em]2,以此类推04[em]4
14[em]2
13[em]1
05[em]1
06[em]2
16[em]4
13[em]1
13[em]1
06[em]2
03[em]3
02[em]2
04[em]4
13[em]1
03[em]3
11[em]3
06[em]2
02[em]2
04[em]4
14[em]2
请问用SQL语句如何完成?谢谢
02 06 10 14 是属于2区
03 07 11 15 是属于3区
04 08 12 16 是属于4区现在有一列记录
LH
16
15
02
04
05
05
02
10
06
04
14
13
05
06
16
13
13
06
03
02
04
13
03
11
06
02
04
14执行语句后LH[em]所属区域
16[em]4-------'16'这个数字属于第‘4’区域,记作‘4’15[em]3
02[em]2
04[em]4
05[em]1
05[em]1
02[em]2
10[em]2-----'10'这个数字属于第‘2’区域,记作‘2’06[em]2,以此类推04[em]4
14[em]2
13[em]1
05[em]1
06[em]2
16[em]4
13[em]1
13[em]1
06[em]2
03[em]3
02[em]2
04[em]4
13[em]1
03[em]3
11[em]3
06[em]2
02[em]2
04[em]4
14[em]2
请问用SQL语句如何完成?谢谢
解决方案 »
- 急,求救,提问关于使用sp_executesql的问题
- Select 查询问题
- SQL SERVER密码
- 简单问题::::怎么用select语句把'1'变成'0001','20'变成'0020',等等这样的操作???
- 从2000到2005转移数据库出现问题 无法初始化连接服务器"(null)"的OLEDB访问接口"MSDASQL"的数据源对象
- 错误信息
- 求助 可人民币感谢 修复损坏的SQL数据库文件
- 有一个已有的系统 他认为数据库中存在表A 可是现在这个表已经不叫A 而且列名 也发生了变化 可不可以创建一个叫A的视图来欺骗?
- ODBC SQLState:42000
- SQL2008R2怎么设置备份
- 求助SQL高手,insert如何实现按行导入?
- sqlserver 2000中怎么导入导出数据库中表的数据(急。。。)
when lh in ('02', '06', '10', '14') then lh+ '[em]2'
when lh in ('03', '07', '11', '15') then lh+ '[em]3'
when lh in ('04', '08', '12', '16') then lh+ '[em]4' end as result
from tab
16[em]4-------'16'这个数字属于第‘4’区域,记作‘4’
15[em]3
02[em]2
04[em]4
05[em]1
05[em]1
02[em]2
10[em]2-----'10'这个数字属于第‘2’区域,记作‘2’,以此类推
06[em]2
04[em]4
14[em]2
13[em]1
05[em]1
06[em]2
16[em]4
13[em]1
13[em]1
06[em]2
03[em]3
02[em]2
04[em]4
13[em]1
03[em]3
11[em]3
06[em]2
02[em]2
04[em]4
14[em]2
请问用SQL语句如何完成?谢谢
select lh,case when lh in ('01', '05', '09', '13') then '1'
when lh in ('02', '06', '10', '14') then '2'
when lh in ('03', '07', '11', '15') then '3'
when lh in ('04', '08', '12', '16') then '4' end as AddField
from tab
when LH in ('01', '05', '09', '13') then LH as 1
when LH in ('02', '06', '10', '14') then LH as 2
when LH in ('03', '07', '11', '15') then LH as 3
when LH in ('04', '08', '12', '16') then LH as 4
end as NQQFROM [master].[dbo].[1074历史号码数据]
我的问题就是:这个语句有错,高手帮忙修改
drop table tb
go
create table tb (LH varchar(2))
insert into tb
select '16' union all
select '15' union all
select '02' union all
select '04' union all
select '05' union all
select '05' union all
select '02' union all
select '10' union all
select '06' union all
select '04' union all
select '14' union all
select '13' union all
select '05' union all
select '06' union all
select '16' union all
select '13' union all
select '13' union all
select '06' union all
select '03' union all
select '02' union all
select '04' union all
select '13' union all
select '03' union all
select '11' union all
select '06' union all
select '02' union all
select '04' union all
select '14'declare @tb table (lh varchar(20),区域 int)
insert into @tb
select '01 05 09 13', 1 union all
select '02 06 10 14', 2 union all
select '03 07 11 15', 3 union all
select '04 08 12 16', 4
select a.LH,b.区域 as 所属区域
from tb a join (select lh=SUBSTRING(a.lh,number,CHARINDEX(',',a.lh+',',number)-b.number),a.区域
from( select REPLACE(lh,' ',',') lh, 区域
from @tb
) a join master..spt_values b ON b.type='p'
and CHARINDEX(',',','+a.lh,number)=number) b on a.LH=b.lhLH 所属区域
05 1
05 1
05 1
13 1
13 1
13 1
13 1
02 2
02 2
02 2
02 2
06 2
06 2
06 2
06 2
10 2
14 2
14 2
03 3
03 3
11 3
15 3
04 4
04 4
04 4
04 4
16 4
16 4