♣
请高手帮我看一个sql语句(有点难度),不胜感激,等待中!!!!!
create table test(id varchar(12), number varchar(5),class varchar(4),pieceid varchar(4))
insert into test
select 20050613001, 0, 甲,
union all select 20050613002, 10.5, 甲,
union all select 20050613003, 30, 甲,
union all select 20050613004, 35, 甲,
union all select 20050613005, 40.8, 甲,
union all select 20050613006, 48, 甲,
union all select 20050613007, 52.4, 甲,
union all select 20050613008, 60, 甲,
union all select 20050613009, 61, 甲,
union all select 20050613010, 89, 甲,
union all select 20050613011, 89.9, 甲,
union all select 20050613012, 90, 甲,
union all select 20050613013, 91.2, 甲,
union all select 20050613014, 100, 甲,
union all select 20050613015, 103.5, 甲,
union all select 20050613016, 107.5, 甲,
union all select 20050613017, 119.5, 甲,
布匹检验时,每个班次都有一个开始,结束的计数值(中间还有其他计数值)
在一个班次内,一般从开始值往后每30m(<=30)为一个匹长,为每一个匹长加一个匹号,
即 开始 结束 匹号
0 - 30 1,
30 - 60 2,
60 - 90 3,
若最后一个匹长<18,则匹号为前一个匹号,
若最后一个匹长>=18,则匹号为下一个匹号
sql语句如下:
select id, number, class,
pieceid =
cast
(case
when number = 0 then 1
when cast(cast(number as float) as int) / 30 = (cast(number as float) + 0.0)/ 30
then cast(number as float) / 30
else cast(number as float) / 30 + 1
end as int)
into #t from test a
update test set test.pieceid=#t.pieceid from #t where test.id=#t.id
select * from test
drop table #t
我在算最后一个pieceid时继续不下去了
(先选pieceid最大的数据,如果max(number)/30<18,则pieceid=max(pieceid)-1
否则pieceid=max(pieceid))
想得到的结果如下
id number class pieceid
20050613001 .0 甲 1
20050613002 10.5 甲 1
20050613003 30.0 甲 1
20050613004 35.0 甲 2
20050613005 40.8 甲 2
20050613006 48.0 甲 2
20050613007 52.4 甲 2
20050613008 60.0 甲 2
20050613009 61.0 甲 3
20050613010 89.0 甲 3
20050613011 89.9 甲 3
20050613012 90.0 甲 3
20050613013 91.2 甲 4
20050613014 100.0 甲 4
20050613015 103.5 甲 4
20050613016 107.5 甲 4
20050613017 119.5 甲 4
· 网友精彩回答:
- 更多问题:
- · 大家可不可以说说他们的区别吗?
- · 高分求助:删除默认网站后如何新建一个默认网站?
- · 如何将已有的图标文件在程序中使用?
- · 如何调用这个DLL,最好有示例!
- · 六位数的QQ不要钱啦,点击马上申请!放心这里不是病毒
- · CPropertySheet的问题,跪求各位给个答案
- · 打包时设置注册码的问题?
- · 安裝redhat 7.2找不到顯卡怎麼辦?
- · QQ六位号免费申请网址,请大家放心,这里不是病毒
- · 学生的和工作的差别到底在什么地方?
- · fastreport中显示两个数据集的问题
- · 为什么我在编译程序的时候总回弹出请选择可执行文件 对话框
- · 请问为什么不能将信息写入XML文档?
- · 怎么实现工具栏的自动隐藏
- · 请教华硕P2B-F上图拉丁的问题!!
- · 留言本问题

