您的位置:程序门 -> .net技术 -> c#



关于sql查询的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于sql查询的问题
发表于:2008-01-21 09:47:12 楼主
数据库中有table   a数据如下:
id     name     password
1       jack     123
2       lulu     456
3       jason   789
我要用个什么样的查询语句来实现查询的结果如下所示:
id         1         2         3
name     jack   lulu   jason
password   123   456   789
请高手指教
发表于:2008-01-21 09:48:291楼 得分:0
select   *   from   a
order   by   id   ,   name,   password  
发表于:2008-01-21 09:51:132楼 得分:0
加入case

发表于:2008-01-21 09:56:303楼 得分:0
sql code
select 'name' as [id], case [id] when 1 then [name] else '' end as [1], case [id] when 2 then [name] else '' end as [2], case [id] when 3 then [name] else '' end as [3] from [a] union select 'password' as [id], case [id] when 1 then [password] else '' end as [1], case [id] when 2 then [password] else '' end as [2], case [id] when 3 then [password] else '' end as [3] from [a]
发表于:2008-01-21 10:01:354楼 得分:0
select   [id],max([1])   as   [1],max([2])   as   [2],max([3])   as   [3]
from

select
    'name'   as   [id],
    case   [id]
        when   1   then   [name]
        else   ''
    end   as   [1],
    case   [id]
        when   2   then   [name]
        else   ''
    end   as   [2],
    case   [id]
        when   3   then   [name]
        else   ''
    end   as   [3]
from   [a]
)   [b]
group   by   [id]
union
select   [id],max([1])   as   [1],max([2])   as   [2],max([3])   as   [3]
from

select
    'password'   as   [id],
    case   [id]
        when   1   then   [password]
        else   ''
    end   as   [1],
    case   [id]
        when   2   then   [password]
        else   ''
    end   as   [2],
    case   [id]
        when   3   then   [password]
        else   ''
    end   as   [3]
from   [a]
)   [b]
group   by   [id]
发表于:2008-01-21 10:11:575楼 得分:0
sql code
create table f(id int,name varchar10),password varchar10)) insert into f select 1,'jack','123' insert into f select 2,'lulu','456' insert into f select 3,'jason','789' create table #(col1 varchar50),col2 varchar50),colname varchar50)) insert into # select * fromselect id,row_number() overorder by id) as px,'id' as colname from f)tp insert into # select * fromselect name,row_number() overorder by id) as px ,'name' as colname from f)tp insert into # select * fromselect password,row_number() overorder by id) as px ,'password' as colname from f)tp select maxcase when colname='id' and col2='1' then col1 when colname='name' and col2='1' then col1 when colname='password' and col2='1' then col1 end) , maxcase when colname='id' and col2='2' then col1 when colname='name' and col2='2' then col1 when colname='password' and col2='2' then col1 end), maxcase when colname='id' and col2='3' then col1 when colname='name' and col2='3' then col1 when colname='password' and col2='3' then col1 end) from # group by colname


(无列名) (无列名) (无列名)
1 2 3
jack lulu jason
123 456 789
发表于:2008-01-21 10:16:096楼 得分:0
sql code
--旋转例子 create table test1(a varchar20),b int,c int,d int,e int) insert into test1 select 'x',1,2 ,3 ,4 insert into test1 select 'y',5,6 ,7 ,8 insert into test1 select 'z',9,10,11,12 go --生成中间数据表 declare @s varchar8000) set @s = 'create table test2(a varchar(20)' select @s = @s + ',' + a + ' varchar(10)' from test1 set @s = @s + ')' EXEC@s) print @s --借助中间表实现行列转换 declare @name varchar20) declare t_cursor cursor for select name from syscolumns where id=object_id'test1') and colid > 1 order by colid open t_cursor fetch next from t_cursor into @name while @@fetch_status = 0 begin EXEC'select ' + @name + ' as t into test3 from test1') set @s='insert into test2 select ''' + @name + '''' select @s = @s + ',''' + rtrim(t) + '''' from test3 EXEC@s) EXEC'drop table test3') fetch next from t_cursor into @name end close t_cursor deallocate t_cursor --查看行列互换处理结果 select * from test1 select * from test2 --删除表 drop table test1 drop table test2 ------------------------------------------------ /*固定的写法:*/ select t1.* , t2.y , t3.z fromselect a = 'b' , x = b from test1 where a = 'x') t1, (select a = 'b' , y = b from test1 where a = 'y') t2, (select a = 'b' , z = b from test1 where a = 'z') t3 where t1.a = t2.a and t1.a = t2.a union all select t1.* , t2.y , t3.z fromselect a = 'c' , x = c from test1 where a = 'x') t1, (select a = 'c' , y = c from test1 where a = 'y') t2, (select a = 'c' , z = c from test1 where a = 'z') t3 where t1.a = t2.a and t1.a = t2.a union all select t1.* , t2.y , t3.z fromselect a = 'd' , x = d from test1 where a = 'x') t1, (select a = 'd' , y = d from test1 where a = 'y') t2, (select a = 'd' , z = d from test1 where a = 'z') t3 where t1.a = t2.a and t1.a = t2.a union all select t1.* , t2.y , t3.z fromselect a = 'e' , x = e from test1 where a = 'x') t1, (select a = 'e' , y = e from test1 where a = 'y') t2, (select a = 'e' , z = e from test1 where a = 'z') t3 where t1.a = t2.a and t1.a = t2.a


a x y z
b 1 5 9
c 2 6 10
d 3 7 11
e 4 8 12
发表于:2008-01-21 10:56:007楼 得分:0
case   then是对的
发表于:2008-01-21 11:50:078楼 得分:0
这种需要的帖发了不只十次!如果你用的是sql   server2005告诉你一个简单的办法!
sql   server   2005之pivot/unpivot行列转换
发表于:2007年12月25日   14时14分30秒阅读(0)评论(1)本文链接:http://user.qzone.qq.com/113620916/blog/1198563270

sql   server   2005之pivot/unpivot行列转换
sql   server2005引入了很多迎合开发者口味的新特性,虽然改动不大,却大大了减少了开发者的工作量,这种替用户考虑的开发思路,值得称赞。
在sql   server2000中,要实现行列转换,需要综合利用聚合函数和动态sql,实现起来需要一定的技巧,所以在csdn的sql讨论区里可以看到大量询问行列转换如何实现的问题。到了2005中,使用新引进的关键字pivot/unpivot,可以轻松实现行列转换的需求。
好像oracle11g也准备引入pivot/unpivot特性,对于oracle开发来说,it's   a   good   news。
本文通过两个简单的例子展示pivot/unpivot的用法。详细的语法请参考联机帮助。

pivot
创建测试表,插入测试数据
create   table   test(id   int,name   varchar(20),quarter   int,profile   int)  
insert   into   test   values(1,'a',1,1000)
insert   into   test   values(1,'a',2,2000)
insert   into   test   values(1,'a',3,4000)
insert   into   test   values(1,'a',4,5000)
insert   into   test   values(2,'b',1,3000)
insert   into   test   values(2,'b',2,3500)
insert   into   test   values(2,'b',3,4200)
insert   into   test   values(2,'b',4,5500)
select   *   from   test
id   name   quarter   profile
-----------   --------------------   -----------   -----------
1   a   1   1000
1   a   2   2000
1   a   3   4000
1   a   4   5000
2   b   1   3000
2   b   2   3500
2   b   3   4200
2   b   4   5500
(8   row(s)   affected)
利用pivot将个季度的利润转成横向显示:
select   id,name,
[1]   as   "一季度",
[2]   as   "二季度",
[3]   as   "三季度",
[4]   as   "四季度"
from
test
pivot

sum(profile)
for   quarter   in
([1],[2],[3],[4])
)
as   pvt
id   name   一季度   二季度   三季度   四季度
-----------   --------------------   -----------   -----------   -----------   -----------
1   a   1000   2000   4000   5000
2   b   3000   3500   4200   5500
(2   row(s)   affected)
unpivot
建立测试表,插入测试数据
drop   table   test
create   table   test(id   int,name   varchar(20),   q1   int,   q2   int,   q3   int,   q4   int)
insert   into   test   values(1,'a',1000,2000,4000,5000)
insert   into   test   values(2,'b',3000,3500,4200,5500)

select   *   from   test
id   name   q1   q2   q3   q4
-----------   --------------------   -----------   -----------   -----------   -----------
1   a   1000   2000   4000   5000
2   b   3000   3500   4200   5500
(2   row(s)   affected)
利用unpivot,将同一行中四个季度的列数据转换成四行数据:
select   id,name,quarter,profile
from
test
unpivot

profile
for   quarter   in
([q1],[q2],[q3],[q4])
)  
as   unpvt
id   name   quarter   profile
-----------   --------------------   ----------   -----------
1   a   q1   1000
1   a   q2   2000
1   a   q3   4000
1   a   q4   5000
2   b   q1   3000
2   b   q2   3500
2   b   q3   4200
2   b   q4   5500
(8   row(s)   affected)  
发表于:2008-01-21 20:55:179楼 得分:0
这题目和普通行转列不一样,普通行转列简单,这可以说是旋转式行转列,pivot/unpivot很难实现
发表于:2008-01-23 12:42:2810楼 得分:0
pivot/unpivot   不错,学习了


快速检索

最新资讯
热门点击