找回密码
 注册
搜索
热搜: java php web
查看: 1386|回复: 5

实用SQL语句——行转列

[复制链接]
发表于 2009-1-25 18:21:48 | 显示全部楼层 |阅读模式
/* 大概是这样子的:

有三个数据库表,
一个是学生表S(SNO,SNAME),字段分别表示学号,姓名;
一个是课程表C(CNO,CNAME),字段分别表示课程号,课程名称;
一个是成绩表T(SNO,CNO,SCORE),字段分别表示学号,课程号,该学生该课程成绩。

现在要实现这个表:

姓名 语文 数学 英语 物理 化学 ...
张三  分数 分数 分数 分数 分数  
李四 分数 分数 分数 分数 分数
王五 分数 分数 分数 分数 分数
...

不知道有多少个课程*/

建表
if object_id('s') is not null
drop table s
if object_id('c') is not null
drop table c
if object_id('t') is not null
drop table t
create table S(sno int,sname varchar(300))

create table c(cno int,cname varchar(300))

create table t(sno int,cno int,score int)

insert   into   S(sno,sname)
select   1,'张三'
union   
select   2,'李四'
union
select   3,'王五'
union
select   4,'甲六'

insert   into   c(cno,cname)
select   1,'语文'
union   
select   2,'数学'
union
select   3,'英语'
union   
select   4,'物理'
union   
select   5,'化学'
union
select   6,'历史'
union
select   7,'历史'

insert   into   t(SNO,CNO,score)
select   1,1,80
union
select   1,2,70
union
select   1,3,50
union
select   1,4,60
union
select   1,5,90
union
select   1,6,60
union
select   2,1,41
union
select   2,2,42
union
select   2,3,53
union
select   2,4,64
union
select   3,1,43
union
select   3,2,44
union
select   3,3,55
union
select   3,4,66

/* 下面是用动态SQL语句的情况*/
select * from s
select * from c
select * from t
declare   @abc   varchar(3000)
set   @abc   =   ''
/*取最高分用max(),最低分用min(),平均用avg()*/
select   @abc   =   @abc   +   ',avg(   case     when   cno   =   '+  cast(cno   as   varchar(8))   +'   then     score     end   )   as   '''+cname+ '''' from   c
declare   @sql     varchar(3000)
set   @sql   =   'select   (select   sname     from   S  where S.sno=   T.sno   )   as   sname     '+@abc   +'   from   T   group   by   sno'
exec   (@sql)


/* 下面是不用动态SQL语句的情况*/

select sname 姓名,
max(yw) 语文,
max(sx) 数学,
max(yy) 英语,
max(wl) 物理,
max(hx) 化学,
max(ls) 历史
from (
        select sname ,(case when cname='语文' then score  end) yw,
(case when cname='数学' then score end) sx,
(case when cname='英语' then score  end) yy,
(case when cname='物理' then score  end) wl,
(case when cname='化学' then score  end) hx,
(case when cname='历史' then score  end) ls
    from t left join s on t.sno=s.sno left join c on t.cno=c.cno
  ) tmp
  group by sname




/* 不用动态SQL语句的情况*/

if object_id('stat') is not null
drop table stat

create table stat(class varchar(30), fruit varchar(30), amount int)


insert into stat (class,fruit,amount)
select 'class a', 'apple', 30
union
select 'class a', 'pear', 15
union
select 'class b', 'apple', 40
union
select 'class b', 'pear', 20
union
select 'class a','apple',11
union  
select 'class b','apple',12

select * from stat

select class, sum(apple_amount) apple_amount, sum(pear_amount) pear_amount
  from (
    select class, (case when fruit='apple' then amount else 0 end) as apple_amount, (case when fruit='pear' then amount else 0 end) as pear_amount
    from stat
  ) tmp
  group by class




select * from stat
select class, sum(apple_amount) apple_amount, sum(pear_amount) pear_amount
  from (
    select class, (case when fruit='apple' then amount else 0 end) as apple_amount, (case when fruit='pear' then amount else 0 end) as pear_amount
    from stat
  ) tmp
  group by class
发表于 2009-1-25 19:54:06 | 显示全部楼层
It's sound very good
for me
thanks a lot~
回复

使用道具 举报

发表于 2009-1-25 19:32:39 | 显示全部楼层
Thank u so much
回复

使用道具 举报

发表于 2009-1-25 20:07:58 | 显示全部楼层
回复

使用道具 举报

发表于 2009-1-25 19:17:58 | 显示全部楼层
很又心思哦,,学习学习。
回复

使用道具 举报

发表于 2009-1-25 20:21:59 | 显示全部楼层
学习  学习
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|软晨网(RuanChen.com)

GMT+8, 2024-9-20 15:39

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表