如何采用mysql实现oracle的decode()函数的来解决一个查询问题??急!急!

hesandy 2009-10-26 03:42:38
我在oracle里写了如下一个查询统计语句,那位能帮我用mysql实现一下,谢谢了,oracle的sql语句如下:
select t.serv_id,
sum(decode(t.acct_item_type_id,
11000,
t.charge,
11002,
t.charge,
0)) 分摊信息费,
sum(decode(t.acct_item_type_id, 11003, t.charge, 0)) 初装费,
sum(decode(t.acct_item_type_id, 11004, t.charge, 0)) 移机费,
sum(decode(t.acct_item_type_id,
11007,
t.charge,
20020,
t.charge,
20021,
t.charge,
0)) 开通费,
sum(decode(t.acct_item_type_id, 11010, t.charge, 0)) 过户费,
sum(decode(t.acct_item_type_id, 10030, t.charge, 0)) 配件扣费,
sum(decode(t.acct_item_type_id, 10031, t.charge, 0)) 购猫费
from acct_item t
where t.billing_cycle_id = '2009080101'
and t.state = '20D'
group by t.serv_id
...全文
14529 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZhangNan20100811 2012-08-24
  • 打赏
  • 举报
回复
case then
不错!
川哥119 2010-08-10
  • 打赏
  • 举报
回复
正好需要使用这个 学习了
vinsonshen 2009-10-26
  • 打赏
  • 举报
回复
if...then钎套啊
ACMAIN_CHM 2009-10-26
  • 打赏
  • 举报
回复
IF() 只有两种结果

而 CASE WHEN 则与 DECODE 类似,是多条件分支。
hesandy 2009-10-26
  • 打赏
  • 举报
回复
没有符合条件的时候执行结果如下
mysql> select sum(if(t.acct_item_type_id=11007 or t.acct_item_type_id=20020 or
-> t.acct_item_type_id=20021,t.charge,0)) 开通费 from acct_item t;
+--------+
| 开通费 |
+--------+
| NULL |
+--------+
1 row in set (0.00 sec)


mysql> select sum(case t.acct_item_type_id when 11000 then t.charge when 11002 then t.charge else 0 end) 分摊信息费 from acct_item t;
hen t.charge else 0 end) 分摊信息费 from acct_item t;
+------------+
| 分摊信息费 |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
谢谢大家
vinsonshen 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 acmain_chm 的回复:]
#14楼 shiyiwan#6的,通用sql

是的,oracle decode() 功能上应该和 case when 相对应。
[/Quote]

是一样的
即if... then...
ACMAIN_CHM 2009-10-26
  • 打赏
  • 举报
回复
[Quote= #14楼 shiyiwan ]#6的,通用sql[/Quote]

是的,oracle decode() 功能上应该和 case when 相对应。
shiyiwan 2009-10-26
  • 打赏
  • 举报
回复
#6的,通用sql
ACMAIN_CHM 2009-10-26
  • 打赏
  • 举报
回复
decode 换成

IF()
或者
CASE WHEN

详细用法你可以参考一下MYSQL的文档。
MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
shiyiwan 2009-10-26
  • 打赏
  • 举报
回复
有标准的case when语句可以改呀
WWWWA 2009-10-26
  • 打赏
  • 举报
回复
sum(if(t.acct_item_type_id=11007 or t.cct_item_type_id=20020 or
t.cct_item_type_id=20021,t.charge,0)) 开通费
少一个括号
hesandy 2009-10-26
  • 打赏
  • 举报
回复
谢谢大家,我回去验证一下,回来给分
wwwwb 2009-10-26
  • 打赏
  • 举报
回复
其它的不用写了吧
wwwwb 2009-10-26
  • 打赏
  • 举报
回复
sum(if(t.acct_item_type_id=11007 or t.cct_item_type_id=20020 or
t.cct_item_type_id=20021,t.charge,0) 开通费
hesandy 2009-10-26
  • 打赏
  • 举报
回复
mysql的deode函数是解密函数,与oracle的decode函数不一样
vinsonshen 2009-10-26
  • 打赏
  • 举报
回复
select t.serv_id, 
sum(case t.acct_item_type_id when 11000 then t.charge when 11002 then t.charge else 0 end) 分摊信息费,
sum(case t.acct_item_type_id when 11003 then t.charge else 0 end) 初装费,
sum(case t.acct_item_type_id when 11004 then t.charge else 0 end) 移机费,
sum(case t.acct_item_type_id when 11007 then t.charge when 20020 then t.charge when 20021 then t.charge else 0 end) 开通费,
sum(case t.acct_item_type_id when 11010 then t.charge else 0 end) 过户费,
sum(case t.acct_item_type_id when 10030 then t.charge else 0 end) 配件扣费,
sum(case t.acct_item_type_id when 10031 then t.charge else 0 end) 购猫费
from acct_item t
where t.billing_cycle_id = '2009080101'
and t.state = '20D'
group by t.serv_id
WWWWA 2009-10-26
  • 打赏
  • 举报
回复
sum(
case t.acct_item_type_id
decode(t.acct_item_type_id,
when 11000 then t.charge
when 11002 then t.charge
else 0 end) 分摊信息费,
hesandy 2009-10-26
  • 打赏
  • 举报
回复
sum(decode(t.acct_item_type_id,
11007,
t.charge,
20020,
t.charge,
20021,
t.charge,
0)) 开通费
这个哪
wwwwb 2009-10-26
  • 打赏
  • 举报
回复
or
case t.acct_item_type_id
when 11003 then t.charge
else 0 end
WWWWA 2009-10-26
  • 打赏
  • 举报
回复
sum(iif(t.acct_item_type_id=11003, t.charge, 0)) 初装费,
加载更多回复(1)
课程亮点: 从无到有、手把手教你编写CA/TA,快速上手,快速部署项目标准的开发,开发一套CA/TA,可部署到不同的TEE OS上。受益人群: 汽车行业主机厂、tier1、SOC芯片公司的安全部门同事手机行业,ODM/OEM、SOC芯片公司的安全部门同事学生课程收益: 熟悉CA/TA开发的步骤和流程。快速上手,快速搭建自己开发环境。熟悉各类TEE、基于各类TEE的CA/TA开发步骤。搭建自己的安全平台熟悉各类常规安全应用熟悉tee密码学算法、tee存储  课程大纲  Hello大家好,上架一门新的视频课程,课程主要包含两大部分,第一部分搭建环境,第二部分从无到有的编写代码。带领大家手把手编写。 具体大纲如下:(1)qemu v8环境搭建- 搭建一个qemu_v8的环境,用于跑BL1-->BL2-->BL31-->BL32-->BL33-->Linux kernel;- 直接使用已搭建好的镜像- 工程使用以及说明(2)CA/TA开发编程实践从无到有编写代码,已完成的大纲如下:- 2秒钟快速编写(clone)一组CA/TA程序- 安全存储详解以及代码示例- CA到TA双向传参数的四种方式(value、temref、memref),区别?优缺点?- 对称密码学算法aes的使用,CBC/ECB/CTR/XTS分组密码的使用,加密解密,pending等- aeskey的操作,如何随机生成aeskey(TEE_GenerateKey),objectHandle和aesbuf有什么区别? 如何将handle- 认证加密算法,如aes-GCM的使用- 非对称密码学算法RSA的使用,包括加密、解密、签名、验签- RSA key的处理,包含如何生成RSA KEY,rsakey object如何转换成可见的数组,如何转换der,如何转换pem,反向又如何转换- ECC/ECDSA的使用- 国密sm2 sm3 sm4的使用.  其中sm4包含加密、解密、签名、验签等- encode和decode实现- TA属性的定制以及API的使用- 数字摘要  SHA1 sha224 sha256 sha384 sha512等- 消息认证码 HMAC- TEE侧获取时间的函数有哪些(TEE_GetSystemTime、TEE_GetREETime),有什么区别?分别是怎样使用的? - 如何获取随机数(TEE_GenerateRandom)?- TA调用TA的示例和演示后续可能继续补充的如下(也欢迎大家提需求):- multi-session和multi-instance的使用- CA LOGIN flag的使用 

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧