永发信息网

MS sql server 2005 数据查询语句如何写?

答案:5  悬赏:0  手机版
解决时间 2021-08-11 15:58
  • 提问者网友:星軌
  • 2021-08-10 16:06
表名  table_1id name kumu fenshu 1 zs yuwen 232 3 ls shuxue 32 4 zs shuxue 12 5 ls lishi 343 6 ww yuwen 1245 8 zs lishi 121

写出sql语句,查询结果如下: name  yuwen shuxue lishi   zl       232     12      121  ls     0        32   343   ww    1245     0        0请问改如何书 ms sql 2005数据库.
最佳答案
  • 五星知识达人网友:猎心人
  • 2021-08-10 16:59
select id,name,kumu,fenshu from table_1
全部回答
  • 1楼网友:骨子里都是戏
  • 2021-08-10 20:38

SELECt NAME , a.[yuwen],a.[shuxue].a.[lishi]

from table-1

pivot ( sum(fenshu) for kumu in ([yuwen],[shuxue],[lishi])) as a

  • 2楼网友:拜訪者
  • 2021-08-10 20:02

典型的交叉表应用(即行列转换)

SELECt [NAME], SUM(CASE KUMU WHEN 'YUWEN' THEN FENSHU ELSE 0 END) AS YUWEN, SUM(CASE KUMU WHEN 'SHUXUE' THEN FENSHU ELSE 0 END) AS SHUXUE, SUM(CASE KUMU WHEN 'LISHI' THEN FENSHU ELSE 0 END) AS LISHI FROM table_1 GROUP BY [NAME]

执行结果

  • 3楼网友:想偏头吻你
  • 2021-08-10 18:31
直接运用数据库语句select id,name,kumu,fenshu from table_1
  • 4楼网友:轻雾山林
  • 2021-08-10 18:25

写的有点复杂,但能实现你要的效果,而且效率很底,建议你用存储过程比较好。

select (select fenshu from table_1 where (name=a.name) and (kemu='yuwen')) yuwen, (select fenshu from table_1 where (name=a.name) and (kemu='shuxue')) shuxue, (select fenshu from table_1 where (name=a.name) and (kemu='lishi')) lishi from table_1 a where (a.name in (select name from table_1)) group by a.name

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯