mysql/oracle竖表转换横表

1. 一条记录count两个表记录数
selecta.a,b.bfrom(
selectcount(*)asa,'1'astypefromdfswriter_point
)ajoin(
selectcount(*)asb,'1'astypefromdfswriter_done
)bona.type=b.type
 
 
2. mysql竖表转换横表
sample:
CREATETABLE`grade`(
`NO`INT(10)NULL,
`subject`VARCHAR(50)NULL,
`grade`INT(10)NULL
)
1.先group by no (得到no唯一的数据)
2.再从以上数据中判断具体某一列的值
  
if(subject='语文',grade,0)
1 语文 99
  1 数学 0
  1 英语 0
  1 物理 0
  1 化学 0

以上数据经过函数max后就得到"语文"成绩,接下来同理得到其他科目的成绩
selectno,max(if(subject='语文',grade,0))as语文,
max(if(subject='数学',grade,0))as数学,
max(if(subject='英语',grade,0))as英语,
max(if(subject='物理',grade,0))as物理,
max(if(subject='化学',grade,0))as化学,
max(if(subject='地理',grade,0))as地理fromgradegroupbyno;
3. oracle竖表转换横表
http://www.west263.com/www/info/27990-1.htm
 
 
posted @ 2012-05-24 16:36  kkk3044147  阅读(1240)  评论(0编辑  收藏  举报