使用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 直译过来就是:没有连接谓词 在真实的生产环境下我们 ...
随机推荐
- maven的搭建
1.安装JDK. 2.Maven是 Apache 下的一个项目,官网下载 Maven:http://maven.apache.org/download.cgi 系统变量:M2_HOME= G:\vis ...
- Java 集合常用方法锦集
Java集合非常的重要,尤其在业务中,如果你在熟练的使用Java数据结果的集合工作,将会大大的提高工作效率,减少代码量. 1.集合的互换 1.1 Map转Set Map<Integer, Str ...
- 数据持久化以及DAO模式的简单使用
持久化:(是将程序中的数据在瞬时状态和持久状态间转换机制) 即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘).持久化的主要应用是将内存中的对象存储在关系型的数据库中,当然 ...
- sql 语句纵表变横表
现把转换方法列举如下: 1.纵表转横表: 纵表结构 TableA Name Course Grade 张三 语文 75 张三 数学 80 张三 英语 90 李四 语文 95 李四 数学 55 横表结构 ...
- 个人作业-Week3
个人作业-Week3 1. 软件工程师的成长 同学们在上这门课的时候,还是大三,你的困难和迷茫,别人一定有过.请看看别人怎么学习的,有些是科班,有些是野路子,有些成功,有些失败. 请读完下面所有博客( ...
- JS验证码
1.引用jquery 2.Html页面 <div> <canvas id="canvas" style="cursor: pointer; height ...
- [poj2247] Humble Numbers (DP水题)
DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...
- NPOI分层导出
using NPOI.HSSF.UserModel; using NPOI.POIFS.FileSystem; using org.in2bits.MyXls; using System; using ...
- .NET蓝牙开源库:32feet.NET
在用C#调用蓝牙编程一文中我留个小悬念就是:InTheHand.Net.Personal.dll是怎么来的?这篇文章来解答这个问题,InTheHand.Net.Personal.dll就是来源于今天要 ...
- 软件工程(C编码实践篇)总结
陆伟丹 + 原创作品转载请注明出处 + <软件工程(C编码实践篇)>MOOC课程http://mooc.study.163.com/course/USTC-1000002006 对软件工程 ...