select [id],[名稱1]=max(case 名稱 when 'aa' then 名稱 end),
[名稱2]=max(case 名稱 when 'bb' then 名稱 end),
[名稱3]=max(case 名稱 when 'cc' then 名稱 end)
from table1 group by [id]
[名稱2]=max(case 名稱 when 'bb' then 名稱 end),
[名稱3]=max(case 名稱 when 'cc' then 名稱 end)
from table1 group by [id]
解决方案 »
- 根据字段的值来决定select 字段
- 换一种方法实现xml
- 在线等:aas数据库中的表a的数据导入到BB数据库的表a的SQL语句(菜鸟问题)
- 求一个sql语句,急
- 请大家推荐一本SQL Server 2000与Analysis Services比较经典的书,小弟万分感谢!!
- SQL一个算法 与 存储过程的改写问题
- 紧急问题,关于数据库 'distribution' 的!!
- 2个相同的查询只是条件不同为什么差别很大
- key:exists select waiting……
- 日期范围
- 带表名参数存储过程,添加记录失败,急!
- 2|3|5|6|13|12|7|16 ,如何查||间有2的数字的记录查出来,注意不能把12,122,22这样有2的记录的查出来!
insert tbname
select 1,'aa' union all
select 1,'bb' union all
select 1,'cc' union all
select 2,'dd' union all
select 2,'ee'
goselect a.[id],[名稱1]=min(名稱),
aa2=(case when count(1)>1 then (select top 1 名稱 from tbname where id=a.id and 名稱>min(a.名稱)
order by 名稱 ) else null end),
aa3=(case count(1) when 3 then (select top 1 名稱 from tbname where id=a.id order by 名稱 desc) else null end)
from tbname a group by [id]/*
id 名稱1 aa2 aa3
----------- -------------------- -------------------- --------------------
1 aa bb cc
2 dd ee NULL(所影响的行数为 2 行)
*/drop table tbname