博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL 的坑1 除法“”不可用“”
阅读量:5239 次
发布时间:2019-06-14

本文共 2271 字,大约阅读时间需要 7 分钟。

今天工作中遇见 一问题,有5各部分,现要求5个部分各自的比例,SQL语句没有问题,后来还试了“加”,“减”,“乘”,“Round”,结果都对,唯独“除法”得不到结果。

之前的语句和结果

select Date_1,    isnull(APP,0) as APP,    isnull(邮件,0)as YouJ,    isnull(微信,0) as WeiX,    isnull(微博,0) as WeiB,    isnull(电信渠道,0) as DXQD,    (isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0))as total,    isnull(APP,0)+(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)) as APP_Ratio_text,    ROUND(isnull(邮件,0)/(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)),6) as YouJ_Ratio,    ROUND(isnull(微信,0)*(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)),6) as WeiX_Ratio_test,    isnull(微博,0)/(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)) as WeiB_Ratio,    isnull(电信渠道,0)/(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)) as DXQD_Ratio    from(select convert(varchar(100),date_1,111) as Date_1,promotion_unit,sum(amount) as amount1from dbo.EDM_message_consume('2016/06/26','2016/06/27')where project_type is not null group by promotion_unit,date_1) newtbpivot (sum(amount1)  for newtb.promotion_unit in (APP,邮件,微信,微博,电信渠道)) as pivottb

o_sql1.jpg

后同事提醒试乘1.0,遂解决 如下

select Date_1,    isnull(APP,0) as APP,    isnull(邮件,0)as YouJ,    isnull(微信,0) as WeiX,    isnull(微博,0) as WeiB,    isnull(电信渠道,0) as DXQD,    (isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0))as total,    isnull(APP,0)*1.0/(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)) as APP_Ratio,    isnull(邮件,0)*1.0/(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)) as YouJ_Ratio,    isnull(微信,0)*1.0/(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)) as WeiX_Ratio,    isnull(微博,0)*1.0/(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)) as WeiB_Ratio,    isnull(电信渠道,0)*1.0/(isnull(APP,0)+isnull(邮件,0)+isnull(微信,0)+isnull(微博,0)+isnull(电信渠道,0)) as DXQD_Ratio    from(select convert(varchar(100),date_1,111) as Date_1,promotion_unit,sum(amount) as amount1from dbo.EDM_message_consume('2016/06/26','2016/06/27')where project_type is not null group by promotion_unit,date_1) newtbpivot (sum(amount1)  for newtb.promotion_unit in (APP,邮件,微信,微博,电信渠道)) as pivottb

o_sql1.0.jpg

转载于:https://www.cnblogs.com/li-volleyball/p/5623814.html

你可能感兴趣的文章
Hibernate-缓存
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>
提高PHP性能的10条建议
查看>>
svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法...
查看>>
熟用TableView
查看>>
Java大数——a^b + b^a
查看>>
poj 3164 最小树形图(朱刘算法)
查看>>
百度贴吧图片抓取工具
查看>>
服务器内存泄露 , 重启后恢复问题解决方案
查看>>
ajax post 传参
查看>>
2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」
查看>>
android一些细节问题
查看>>
KDESVN中commit时出现containing working copy admin area is missing错误提示
查看>>
利用AOP写2PC框架(二)
查看>>
【动态规划】skiing
查看>>
java定时器的使用(Timer)
查看>>
Android实现静默安装与卸载
查看>>
ef codefirst VS里修改数据表结构后更新到数据库
查看>>
boost 同步定时器
查看>>
[ROS] Chinese MOOC || Chapter-4.4 Action
查看>>