把查询出来的的数据一列分为两列的SQL语句怎么写啊?请各位高手帮帮忙!
时间:2009-03-27
来源:互联网
VALUE ID
111 2
234 3
312 3
65 3
78 2
541 2
现在我需要把VALUE这一列根据ID的值分开,想得到的结果如下:
VALUE2 VALUE3
111 234
78 312
541 65
这个SQL语句怎么写啊?
环境是在sql server2000中
作者: liaozhanghui 发布时间: 2009-03-27
select max(case px when 1 then value else 0 end) value2, max(case px when 2 then value else 0 end) value3 from ( select * , px = (select count(1) from tb where id = t.id and value < t.value) + 1 from tb t ) m group by id
作者: dawugui 发布时间: 2009-03-27
SQL code
select m.value value1 , n.value value2 from (select * , px = (select count(1) from tb where id = t.id and value < t.value) + 1 from tb t) m, (select * , px = (select count(1) from tb where id = t.id and value < t.value) + 1 from tb t) n where m.px = n.px
作者: dawugui 发布时间: 2009-03-27
SQL code
create table tb(VALUE int, ID int) insert into tb values(111 , 2 ) insert into tb values(234 , 3 ) insert into tb values(312 , 3 ) insert into tb values(65 , 3 ) insert into tb values(78 , 2 ) insert into tb values(541 , 2 ) go select m.value value1 , n.value value2 from (select * , px = (select count(1) from tb where id = 2 and id = t.id and value < t.value) + 1 from tb t where id = 2) m, (select * , px = (select count(1) from tb where id = 3 and id = t.id and value < t.value) + 1 from tb t where id = 3) n where m.px = n.px drop table tb /* value1 value2 ----------- ----------- 111 234 78 65 541 312 (所影响的行数为 3 行) */
作者: dawugui 发布时间: 2009-03-27
SQL code
create table tb(VALUE int, ID int) insert into tb values(111 , 2 ) insert into tb values(234 , 3 ) insert into tb values(312 , 3 ) insert into tb values(65 , 3 ) insert into tb values(78 , 2 ) insert into tb values(541 , 2 ) go select m.value value1 , n.value value2 from (select * ,px = row_number() over(partition by id order by value) from tb ) m , (select * ,px = row_number() over(partition by id order by value) from tb ) n where m.id = 2 and n.id = 3 and m.px = n.px5 drop table tb /* value1 value2 ----------- ----------- 78 65 111 234 541 312 (3 行受影响) */
作者: dawugui 发布时间: 2009-03-27
作者: jinjazz 发布时间: 2009-03-27
我也遇到这样的问题.
作者: lth3696 发布时间: 2011-12-01
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28