多列的COUNT个非空值是否相同?

时间:2016-07-21 17:01:10

标签: mysql sql count

我正在尝试为三列计算非空值。但是,尽管具有不同数量的非空值,但是对于三列中的每一列都返回相同的COUNT。我不确定为什么会这样(我是否需要重新设置列数据类型?错误的SQL子句?)。以下是我的尝试和结果。

编辑:用文字替换图像。制作了较短的别名。

SELECT  CAST(users.install_ts AS Date) AS inst_date, 
        COUNT(users.install_ts) AS total_inst, 
        COUNT(users.firstlogin_ts) AS total_first_logins,
        COUNT(users.firstpurchase_ts) AS conv_cust,
        SUM(CASE WHEN users.firstpurchase_ts IS NULL THEN 1 ELSE 0 END) AS conv_cust
FROM users
GROUP BY CAST(users.install_ts AS Date)

inst_date   total_inst  total_first_logins  conv_cust  conv_cust  
----------  ----------  ------------------  ---------  ---------  
2015-01-01  17191       17191               17191      0          
2015-01-02  41038       41038               41038      0          
2015-01-03  41176       41176               41176      0          
2015-01-04  41072       41072               41072      0          
2015-01-05  41115       41115               41115      0          
2015-01-06  8417        8417                8417       0          
2015-05-16  9991        9991                9991       0      

这是users表:

uid     device_id   install_ts           firstlogin_ts        firstpurchase_ts     
------  ----------  -------------------  -------------------  -------------------  
121045  GalaxyS3    2015-01-01 14:00:01  2015-01-01 14:00:01  (null)               
121046  GalaxyS1    2015-01-01 14:00:03  2015-01-01 14:00:07  2015-01-02 06:00:07  
121047  iPhone3     2015-01-01 14:00:03  2015-01-01 14:00:03  (null)               
121048  GalaxyS1    2015-01-01 14:00:04  (null)               (null)               
121049  iPhone5     2015-01-01 14:00:07  2015-01-01 14:00:08  (null)               
121050  iPad4       2015-01-01 14:00:07  2015-01-01 14:00:09  (null)               
121051  iPhone4s    2015-01-01 14:00:11  (null)               (null)               
121052  iPhone4s    2015-01-01 14:00:13  (null)               (null)               
121053  GalaxyTab3  2015-01-01 14:00:16  (null)               (null)               
121054  iPhone4     2015-01-01 14:00:19  2015-01-01 14:00:22  (null)               
121055  iPad1       2015-01-01 14:00:22  2015-01-01 14:00:26  (null)               
121056  iPad2       2015-01-01 14:00:26  2015-01-01 14:00:29  (null)               
121057  GalaxyTab2  2015-01-01 14:00:30  2015-01-01 14:00:31  (null)               
121058  iPhone5s    2015-01-01 14:00:34  2015-01-01 14:00:38  (null)               
121059  GalaxyS5    2015-01-01 14:00:34  (null)               (null)               
121060  GalaxyS5    2015-01-01 14:00:34  2015-01-01 14:00:38  (null)               
121061  GalaxyS5    2015-01-01 14:00:37  (null)               (null)               
121062  iPhone3     2015-01-01 14:00:39  (null)               (null)            

表格说明:

DESCRIBE users
Field             Type         Null  Key  Default              Extra                        
----------------  -----------  ----  ---  -------------------  ---------------------------  
uid               int(11)      YES   MUL  (null)                                            
device_id         varchar(64)  YES        (null)                                            
install_ts        timestamp    NO         CURRENT_TIMESTAMP    on update CURRENT_TIMESTAMP  
firstlogin_ts     timestamp    NO         0000-00-00 00:00:00                               
firstpurchase_ts  timestamp    NO         0000-00-00 00:00:00                               

编辑:一次执行一个会产生相同的结果。所以也许这是一个分组问题。如果是这样,我该如何实现预期的结果呢?

一次一个:

SELECT  COUNT(users.firstlogin_ts) AS total_first_logins
FROM users
GROUP BY CAST(users.install_ts AS Date)

total_first_logins
17191  
41038  
41176  
41072  
41115  
8417   
9991   

另一栏:

SELECT COUNT(users.install_ts) AS total_inst
FROM users
GROUP BY CAST(users.install_ts AS Date)

total_inst
17191  
41038  
41176  
41072  
41115  
8417   
9991   

2 个答案:

答案 0 :(得分:1)

我猜你的问题可能存在显示问题。您显示的数据显示(null),但表格描述表明字段首先不可为空。但是,字段默认为'0000-00-00 00:00:00',这不是该类型的有效值,但允许(这很奇怪)。有些API将这些值转换为null,即使它们在数据库中实际上不是NULL。试试这个:

SELECT  CAST(users.install_ts AS Date) AS inst_date, 
        COUNT(1) AS total_inst, 
        COUNT(NULLIF(users.firstlogin_ts, '0000-00-00 00:00:00')) AS total_first_logins,
        COUNT(NULLIF(users.firstpurchase_ts, '0000-00-00 00:00:00')) AS conv_cust
FROM users
GROUP BY CAST(users.install_ts AS Date)

NULLIF会将此类值转换为真值NULL

答案 1 :(得分:0)

您可以尝试使用https://dba.stackexchange.com/questions/64927/count-null-and-not-null-values-in-a-column

中指定的内容
SELECT  CAST(users.install_ts AS Date) AS inst_date, 
        COUNT(!ISNULL(users.install_ts)) AS total_inst, 
        COUNT(!ISNULL(users.firstlogin_ts)) AS total_first_logins,
        COUNT(!ISNULL(users.firstpurchase_ts)) AS conv_cust,
        SUM(!ISNULL(users.firstpurchase_ts)) AS conv_cust
FROM users
GROUP BY CAST(users.install_ts AS Date)