使用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 直译过来就是:没有连接谓词 在真实的生产环境下我们 ...
随机推荐
- php时间戳之间相互转换
第一种情况: 将时间戳转换成年月日格式 <?php echo date('Y-m-d H:i:s',$v['apply_time']);?> 第二种情况: 将年月日转换成时间戳 strto ...
- angular.extend()和 angular.copy()的区别
1.angular.copy angular.copy(source, [destination]); // source: copy的对象. 可以使任意类型, 包括null和undefined. ...
- 设置button键隐藏文字text
新手的第一天,从最简单的view开始做起.首先先来做个最简单的button携带不可见文本,想要的时候,get它就行了. 编程的都知道,get .set方法,就不多介绍了. 创建一个类,MyButto ...
- java性能监控常用命令
jps -m -l:主要用来输出JVM中运行的进程状态信息 jstack -l pid 来观察锁持有情况 jsatck pid | grep pid(十六进制):输出进程pid的堆栈信息 jmap - ...
- 将光标放到input内容的最后
在使用setSelectionRange方法的时候一定要加setTimeout,否则无效,代码 var num = $('#input_phone').val().length; setTimeout ...
- jquery 获取元素背景图片backgroungImage的url
$("#").css("backgroundImage").replace('url(','').replace(')','');
- rethinkdb的dataexplorer查询使用
首先安装rethinkdb(只有linux和mac能够安装) $ sudo apt-get install rethinkdb 然后启动: $ rethinkdb 此时8080端口就可以访问本机的数据 ...
- spring mvc文件上传
package com.haier.controller.newuser; import com.haier.commons.entity.Response; import com.haier.com ...
- 关于mysql登录异常处理方法 - mysql ERROR 1045 (28000)
今天在开发过程中遇到了一个很令人头痛的问题?? 使用 百度经验的步骤 [http://jingyan.baidu.com/article/495ba841ef412d38b30edeb2.html]修 ...
- 【网摘】CURL常用命令
原文地址: http://www.thegeekstuff.com/2012/04/curl-examples/ 下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://w ...