日期返回null而不是null值mysql

时间:2012-03-09 20:31:13

标签: mysql null date

所以我有一个临时表,我需要填写必须处理注册历史的记录

我使用的数据集如下所示:

sccphidsid    enroll_date disenroll_date    status
abc123x        2009-01-01  2010-31-12           0
abc123x        2011-01-01  null                 0
abc123x        2011-03-01  2012-01-01           0

所以我需要它返回所有具有相同sccphidsid的记录并返回它们,包括null和not null值。

必须满足的条件是

  • 取消注册日期不为空
  • 取消注册日期为空
  • 记录有多个disenroll_dates

当前解决方案:

select *, count(disenroll_date is null)
              from enrollment _test
              where Status = 0
              group by Sub_Client_Cd, Policy_Holder_ID, Suffix_ID
              having count(disenroll_date is null) > 1;

然而,这会返回太多记录,有没有办法隔离这个查询,可能更简单,可以防止它通过约束或参数返回太多记录?

全部谢谢

2 个答案:

答案 0 :(得分:1)

SELECT Sub_Client_Cd, Policy_Holder_ID, Suffix_ID,
       SUM(CASE WHEN disenroll_date IS NULL THEN 1 ELSE 0 END) AS NullDateCount,
       SUM(CASE WHEN disenroll_date IS NOT NULL THEN 1 ELSE 0 END) AS NotNullDateCount
    FROM enrollment_test
    WHERE Status = 0
    GROUP BY Sub_Client_Cd, Policy_Holder_ID, Suffix_ID
    HAVING COUNT(*) > 1;

答案 1 :(得分:1)

实际上这照顾了它......不管怎样,谢谢。

 select *, count(disenroll_date is null) from enrollment_test 
 where  Status = 0 
 and disenroll_date is null 
 group by Sub_Client_Cd, Policy_Holder_ID, Suffix_ID              
 having count(disenroll_date is null) > 1;