微软BI开拓者数据库专区SQL Server开发 【挑战TSQL】将电子指示牌的数字转换成数字

1  /  1  页   1 跳转 查看:2270

【挑战TSQL】将电子指示牌的数字转换成数字

【挑战TSQL】将电子指示牌的数字转换成数字

将电子指示牌的数字如下:

 附件: 您所在的用户组无法下载或查看附件
转成后的数字如下:

 附件: 您所在的用户组无法下载或查看附件




要求:
1、使用基于集合的方式进行处理
2、不能使用表变量、临时表、while循环、自定义函数
3、可以使用CTE



说明:
1、电子指示牌的每个数字由9个格组成,即3*3个格
2、电子指示牌里仅使用空格、下划线或者竖线
3、每个指示牌的数字都是9位数字
4、可能有些指示牌的某一位显示的数字不对,对这种数字,用x代替




使用下面附件中的代码生成测试数据:



 附件: 您所在的用户组无法下载或查看附件
最后编辑拓狼 最后编辑于 2010-01-11 03:42:49
虽有智慧,不如乘势;虽有鎡基,不如待时。
君子学以聚之,问以辨之,宽以居之,仁以行之。
独学而无友,则孤陋而寡闻。
 

回复:【挑战TSQL】将电子指示牌的数字转换成数字

DECLARE @t TABLE (id int, ScanNumber NVARCHAR(116))
INSERT INTO @t
SELECT  1,--> 000 007 059
'_  _  _  _  _  _  _  _  _
| || || || || |  || ||_ |_|
|_||_||_||_||_|  ||_| _| _|
' UNION
SELECT  2, --> 490 067 715
'  _  _  _  _  _  _    _
|_||_|| || ||_  |  |  ||_
  | _||_||_||_|  |  |  | _|
' UNION
SELECT  3, --> 680 068 279
'_  _  _  _  _  _  _  _  _
|_ |_|| || ||_ |_| _|  ||_|
|_||_||_||_||_||_||_  | _|
'
UNION
SELECT  4, --> 490 867 716
'  _  _  _  _  _  _    _
|_||_|| ||_||_  |  |  ||_
  | _||_||_||_|  |  |  ||_|
'  UNION
SELECT  5, --> X80 867 716
'  _  _  _  _  _  _    _
| ||_|| ||_||_  |  |  ||_
  | _||_||_||_|  |  |  ||_|
'
UNION SELECT 6, --> 012 345 678
'_    _  _    _  _  _  _
| |  | _| _||_||_ |_  ||_|
|_|  ||_  _|  | _||_|  ||_|
';
with cte_splitgroup as
(
select id,id_h=1,b.number,Char=substring(' '+a.ScanNumber,27*0+0+b.number*3+1,3)
from @t a inner join
master.dbo.spt_values b on b.type='P' and b.number<9
union all
select id,id_h=2,b.number,Char=substring(' '+a.ScanNumber,27*1+1+b.number*3+2,3)
from @t a inner join
master.dbo.spt_values b on b.type='P' and b.number<9
union all
select id,id_h=3,b.number,Char=substring(' '+a.ScanNumber,27*2+2+b.number*3+3,3)
from @t a inner join
master.dbo.spt_values b on b.type='P' and b.number<9
)
,
cte_number as
(select number=0,String='
_
| |
|_|' union all select 1,'
 
  |
  |' union all select 2,'
_
_|
|_ ' union all select 3,'
_
_|
_|' union all select 4,'
 
|_|
  |' union all select 5,'
_
|_
_|' union all select 6,'
_
|_
|_|' union all select 7,'
_
  |
  |' union all select 8,'
_
|_|
|_|' union all select 9,'
_
|_|
_|')
,
cte_r as
(
select a.id,a.number,Value=isnull(rtrim(d.number),'X')
from cte_splitgroup a inner join
cte_splitgroup b on a.id=b.id and a.number=b.number and a.id_h=1 and b.id_h=2 inner join
cte_splitgroup c on a.id=c.id and c.id_h=3 and a.number=c.number left join
cte_number d on char(13)+a.Char+char(13)+b.Char+char(13)+c.Char=d.String
)
,
cte_r1 as
(
select id,number,Value=cast(Value as varchar(9))
from cte_r where number=0
union all
select b.id,b.number,Value=cast(a.Value+b.Value as varchar(9))
from cte_r1 a inner join
cte_r b on a.id=b.id and b.number=a.number+1
)
select id,Value from cte_r1
where number=8
order by id
 

回复: 【挑战TSQL】将电子指示牌的数字转换成数字


 附件: 您所在的用户组无法下载或查看附件好像直接回复不了,上传个sql脚本
 

回复: 【挑战TSQL】将电子指示牌的数字转换成数字


 附件: 您所在的用户组无法下载或查看附件好像直接回复不了,上传个sql脚本
 

回复:【挑战TSQL】将电子指示牌的数字转换成数字

看看
 
1  /  1  页   1 跳转

版权所有 微软BI开拓者 

Powered by Discuz!NT 2.1.202    Copyright © 2001-2012 Comsenz Inc.
Processed in 0.1093792 second(s) , 4 queries.
返顶部