Sql数据库查询,如何实现只显示为某值的字段?

Sql数据库查询,如何实现只显示为某值的字段?,第1张

实现只显示为某值的字段,可以通过行列转换实现。

以下是以sql server为例来说明:

select b.stu_name,

max(case a.subject when '语文' then a.grade else '' end) as 语文,

max(case a.subject when '数学' then a.grade else '' end) as 数学,

max(case a.subject when '英语' then a.grade else '' end) as 英语

from stu_grade a,stu_master b

where a.stu_no=b.stu_no

group by b.stu_name

数据库为oralce的话执行

select b.stu_name,

max(case a.subject when '语文' then to_char(a.grade) else '' end) as 语文,

max(case a.subject when '数学' then to_char(a.grade) else '' end) as 数学,

max(case a.subject when '英语' then to_char(a.grade) else '' end) as 英语

from stu_grade a,stu_master b

where a.stu_no=b.stu_no

group by b.stu_name

是这个意思吗?

SUM(CASE WHEN W.TypeID = 999 AND (SUM(CASE WHEN W.TypeID = 999 AND BDate='2014-04-02' THEN BValue ELSE 0 end) as EndingBalance)='2014-04-02' THEN BValue ELSE 0 end) as EndingBalance

请详细描述一下需求

查数据库一个表中的一个行的不同项,肯定要用到【Group By】语法。

示例1:

查询A列,在表Table_temp中不同项

select A from Table_temp group by a

示例2:

查询A列,在表Table_temp中不同项出现的次数

select A,count(0) as 出现次数 from Table_temp group by a


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10657315.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-10
下一篇2023-05-10

发表评论

登录后才能评论

评论列表(0条)

    保存