• SQL 的坑1 除法“”不可用“”


    今天工作中遇见 一问题,有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 amount1
    from dbo.EDM_message_consume('2016/06/26','2016/06/27')
    where project_type is not null 
    group by promotion_unit,date_1) newtb
    pivot (sum(amount1)  for newtb.promotion_unit in (APP,邮件,微信,微博,电信渠道)) as pivottb
    

    后同事提醒试乘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 amount1
    from dbo.EDM_message_consume('2016/06/26','2016/06/27')
    where project_type is not null 
    group by promotion_unit,date_1) newtb
    pivot (sum(amount1)  for newtb.promotion_unit in (APP,邮件,微信,微博,电信渠道)) as pivottb
    

  • 相关阅读:
    css sprites图片背景优化技术
    CSS3新特性(整理贴)
    span HTML元素
    jQuery1.2选择器(2)
    JavaScript 弹出窗口总结
    jQuery1.2选择器(1)
    jquery如何判断checkbox(复选框)是否被选中
    使用 Lightbox 2 和 JavaScript 构建出色的图片库
    如何在一台电脑同时安装IE6、IE7、FF2、FF3进行网页调试
    IE的有条件注释详解(附实例代码)
  • 原文地址:https://www.cnblogs.com/li-volleyball/p/5623814.html
Copyright © 2020-2023  润新知