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的情况的更多相关文章

  1. MySQL累加值时,考虑到值有为NULL的情况.

    一个字段,表示报名人数,默认为null,经考虑,以以下sql执行加1: ) where id='xxx'

  2. SQL Server-聚焦LEFT JOIN...IS NULL AND NOT EXISTS性能分析(十七)

    前言 本节我们来分析LEFT JOIN和NOT EXISTS,简短的内容,深入的理解,Always to review the basics. LEFT JOIN...IS NULL和NOT EXIS ...

  3. 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. ...

  4. 使用@Autowired时,取值为null

    如果取不到,可以考虑其他方式 场景: @Autowired private StringRedisTemplate redisTemplate; 想使用redisTemplate,但是使用时为null ...

  5. 有关Mysql的mysql_store_result函数返回NULL的情况以及其他注意事项

    成功调用mysql_query()后,mysql_store_result()能够返回NULL.出现该情况时,表明出现了下述条件之一: ·         出现了malloc()故障(例如,如果结果集 ...

  6. php 在linux 用fopen() 函数打开,file_get_contents(),fread()函数 读取 另外一台服务器映射过来的文件 总是返回false,null的情况。

    php 在linux 用fopen() 函数打开,fread()函数 读取  另外一台服务器映射过来的文件 总是返回false,null的情况. #获取平台类型   $type='android';  ...

  7. mybatis sql语句中 in() 长度为0或null的情况

    mybatis sql语句中 in() 长度为0或null的情况 比如: select * from A where colName IN <foreach collection="m ...

  8. 从DB灌值到DataTable时,字段值为NULL时报错相关信息;

    报错信息: 1.  2.  3.  4.  5.  6.  解决方法: 1. Data Layer SQL 语句取数据时,把其列值有为null的字段用0.00替换,(ISNULL的用法): 2. #r ...

  9. 关于join时显示no join predicate的那点事

    我们偶尔,非常偶尔的情况下会在一个查询计划中看到这样的警告: 大红叉,好吓人啊! 把鼠标放上去一看显示这样的信息 No join predicate 直译过来就是:没有连接谓词 在真实的生产环境下我们 ...

随机推荐

  1. 08-FPGA状态机设计实例——小梅哥FPGA设计思想与验证方法视频教程配套文档

    芯航线--普利斯队长精心奉献   实验目的:1.学习状态机的相关概念 2.理解一段式.两段式以及三段式状态机的区别以及优缺点 实验平台:芯航线FPGA核心板 实验原理: 状态机全称是有限状态机(fin ...

  2. MVC5 + EF6 简单示例

    本文所使用的软件及环境: Visual Studio Ultimate 2013 (下载地址:http://www.visualstudio.com/downloads/download-visual ...

  3. sharepoint2013用户切换实现方式

    作为一个刚学sharepoint的新人,今天在账号的切换中烦躁无比,不知道有木有人和我一样,sharepoint2013没有了切换用户,真的很不方便,当然了,也不是没有办法加上去,经过本人一个下午的研 ...

  4. Fisher–Yates shuffle 洗牌(shuffle)算法

    今天在敲undersore的源码,数组里面有一个shuffle,把数组随机打乱. _.shuffle = function(obj) { var set = isArrayLike(obj) ? ob ...

  5. sublime text 配置 builder [build system]

    有时候需要用运行一段 PHP 代码,比如测试某个函数返回值等等,如果启动 Http Server,再打开浏览器,那黄花菜都凉了.我们可以在 Sublime Text 3 中创建 PHP 的 build ...

  6. vchar2和nvchar2

    Oracle中varchar2 nvarchar2 VARCHAR2(size),可变长度的字符串,其最大长度为 size 个字节.size 的最大值是 4000,而最小值是 1.您必须指定一个 VA ...

  7. Command调用存储过程小实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  8. [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)

    题目链接:http://codeforces.com/contest/752/problem/D 题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...

  9. linux多文本替换内容

    之前不小心写错了代码,把nil写成了Nil,脚本写的传到git中了,批量替换解决办法: grep "Nil" -rl $PATH/ |xargs sed -i 's/Nil/nil ...

  10. set集合(set是一个无序且不重复的元素集合)

    功能: 一.可以去掉列表,元组中的重复项 二.可以求交集,合集,差集等 def add(self, *args, **kwargs): """ 添加 "&quo ...