日期:2014-05-18  浏览次数:20503 次

同一表查询问题?在线等!(由于小弟分不多了,所以很寒酸!)
有一表(tb)如下:
xm                                                     kc                                                   cj
-------------------------
刘德华                             语文                                 89
张学友                             数学                                 78
郭富城                             语文                                 90
吴邦国                             数学                                 67
刘德华                             数学                                 87
张学友                             语文                                 67
郭富城                             数学                                 94

请统计出如下结果:
xm             kc               cj         kc               cj
-------------------------
刘德华     语文           89         数学           87
张学友     数学           78         语文           67
郭富城     语文           90         数学           94
吴地睦     数学           67
---------------------------
注意:吴地睦只有一科.



------解决方案--------------------
select xm,kc=max(case when kc= '语文 ' then kc end),
cj=max(case when kc= '语文 ' then cj end),
kc=max(case when kc= '数学 ' then kc end),
cj=max(case when kc= '数学 ' then cj end)
from tb
group by xm
------解决方案--------------------
--只有语文和数学?

select isnull(t1.xm,t2.xm) xm,isnull(t1.kc, ' ') kc,isnull(t1.cj,0) cj,
isnull(t2.kc, ' ') kc,isnull(t2.cj,0) cj
from
(select * from tb where kc = '语文 ') t1
full join
(select * from tb where kc = '数学 ') t2
on t1.xm = t2.xm