使用COALESCE时注意left join为null的情况
1.使用COALESCE时,用到group by with cube,如果之前两个表left join时,有数据为null,就会使得查出的数据主键不唯一
例如:
select
COALESCE (c.value,'total_count')as coupon_price,
COALESCE (c.souform,'total_count')as souform,
count(c.id)as coupon_num,
count(distinct c.auserid,c.code)as user_num,
sum(if(c.realprice is not null,c.realprice,)) as realprice,
count(if(c.couponid is not null,c.couponid,null)) as use_coupon_num,
count(distinct if(c.couponid is not null,c.buserid,null))as use_user_num,
count(distinct if(c.couponid is not null and c.realprice>,c.couponid,null)) as use_coupon_num_real,
count(distinct if(c.couponid is not null and c.realprice>,c.buserid,null)) as user_coupon_num_real
from
(
select a.value ,b.souform,a.id,a.userid as auserid,a.code,b.realprice,b.couponid,b.userid as buserid
from
(s
select id,userid,code,value
from tutor.ori_mysql_tutor_coupon_coupon_da
where to_date( from_unixtime(int(expiredtime/)))='2016-12-14' and
to_date( from_unixtime(int(createdtime /)))=date_sub('2016-12-14',)
and appkey=''
)a
left join
(
select case when source='' then 'hh' when source='' then 'aa' when source='' then 'cc'
when source='' then 'dd' when source='' then 'ee' else 'others' end as souform,
if(fee-discount-coupon_price>,fee-discount-coupon_price,)as realprice,couponid,coupon_price,userid
from tutor.ods_tutor_order
where to_date(from_unixtime(int( paidtime/))) between date_sub('2016-12-14',) and '2016-12-14'
)b
on a.id=b.couponid
) c
group by c.value,c.souform with cube
得到的数据会出现两组key一样但数据不一样的情况:

说明其中第二个数据是a表中没有匹配到souform的空值
使用COALESCE时注意left join为null的情况的更多相关文章
- MySQL累加值时,考虑到值有为NULL的情况.
一个字段,表示报名人数,默认为null,经考虑,以以下sql执行加1: ) where id='xxx'
- SQL Server-聚焦LEFT JOIN...IS NULL AND NOT EXISTS性能分析(十七)
前言 本节我们来分析LEFT JOIN和NOT EXISTS,简短的内容,深入的理解,Always to review the basics. LEFT JOIN...IS NULL和NOT EXIS ...
- SQL Server-聚焦NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL性能分析(十八)
前言 本节我们来综合比较NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL的性能,简短的内容,深入的理解,Always to review the basics. ...
- 使用@Autowired时,取值为null
如果取不到,可以考虑其他方式 场景: @Autowired private StringRedisTemplate redisTemplate; 想使用redisTemplate,但是使用时为null ...
- 有关Mysql的mysql_store_result函数返回NULL的情况以及其他注意事项
成功调用mysql_query()后,mysql_store_result()能够返回NULL.出现该情况时,表明出现了下述条件之一: · 出现了malloc()故障(例如,如果结果集 ...
- php 在linux 用fopen() 函数打开,file_get_contents(),fread()函数 读取 另外一台服务器映射过来的文件 总是返回false,null的情况。
php 在linux 用fopen() 函数打开,fread()函数 读取 另外一台服务器映射过来的文件 总是返回false,null的情况. #获取平台类型 $type='android'; ...
- mybatis sql语句中 in() 长度为0或null的情况
mybatis sql语句中 in() 长度为0或null的情况 比如: select * from A where colName IN <foreach collection="m ...
- 从DB灌值到DataTable时,字段值为NULL时报错相关信息;
报错信息: 1. 2. 3. 4. 5. 6. 解决方法: 1. Data Layer SQL 语句取数据时,把其列值有为null的字段用0.00替换,(ISNULL的用法): 2. #r ...
- 关于join时显示no join predicate的那点事
我们偶尔,非常偶尔的情况下会在一个查询计划中看到这样的警告: 大红叉,好吓人啊! 把鼠标放上去一看显示这样的信息 No join predicate 直译过来就是:没有连接谓词 在真实的生产环境下我们 ...
随机推荐
- filter(滤镜) 属性 内部资料 请勿转载 谢谢合作
Filter 描述 none 默认值,没有效果. blur(px) 给图像设置高斯模糊."radius"一值设定高斯函数的标准差,或者是屏幕上以多少像素融在一起, 所以值越大越模糊 ...
- mybatis框架下解决数据库中表的列的字段名和实体类属性不相同的问题
导包.... 实体类中的属性,getter,setter,tostring,构造等方法就不写了 private int id; private String orderNo; private floa ...
- 如何让你的App适配iOS7?
随着苹果在2013年9月18日发布iOS7最新的系统以来,iOS各种设备升级到iOS7的数字就已经不断刷新记录.目前据外界统计iOS7设备装机量已经达到2.5亿部,已占iOS设备的64%.由此可见让自 ...
- Eclipse+maven+scala2.11.8+spark2.0.0的环境部署
主要在maven-for-scalaIDE纠结了,因为在eclipse版本是luna4.x 里面有自己带有的maven. 根据网上面无脑的下一步下一步,出现了错误,在此讲解各个插件的用途,以此新人看见 ...
- c++的多线程和多进程
一.多进程和多线程对比 多进程:进程不止一个,开销比较大,通信方式比较复杂(可以用过管道.文件.消息队列进行通信),维护成本不高. 多线程:利用共享内存的方式进行指令的执行,开销比较低,但是维护起来比 ...
- switch case
Console.WriteLine("1土豆"); Console.WriteLine("2玉米"); Console.WriteLine("3小麦& ...
- 存储过程里面使用in变量列表异常的处理
在写一个存储过程的时候,由于需要用到类似:select id,name from tablename where id in(id1,id2,id3...)的查询语句,同时括号里面的变量是拼接得到的, ...
- 通过js给android控件WebView设padding
项目中有个界面是用来显示一个网页的,很简单,放个WebView就ok了,可是返回按钮放在哪 ,ui的设计是在底部显示一个半透明的条,左边有一个返回按钮.这个条显示在内容上面.我有一个担心,就是要是最下 ...
- LINQ之路 8: 解释查询(Interpreted Queries)
LINQ提供了两个平行的架构:针对本地对象集合的本地查询(local queries),以及针对远程数据源的解释查询(Interpreted queries). 在讨论LINQ to SQL等具体技术 ...
- GitHUb 代码提交遇到的问题以及解决办法
git 添加代码出现以下错误: fatal: Unable to create 'F:/wamp/www/ThinkPhpStudy/.git/index.lock': File exists. If ...