我想从mysql的输出中删除这些空值我有事件列,我分为3,4,5,6列

时间:2017-10-06 06:17:04

标签: mysql

SELECT t1.client_name,FROM_UNIXTIME(t1.timestamp,"%d")AS Date, 
case whent1.event='ALL_OFF'then
    (SUM(Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),
         FROM_UNIXTIME(t1.timestamp)))) end as ALL_OFF,
case when t1.event = 'HOME_ON' then 
   (SUM(Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp) , 
        FROM_UNIXTIME(t1.timestamp))))  end as HOME_ON,
case when t1.event = 'OFFICE_ON' then 
   (SUM(Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp) , 
         FROM_UNIXTIME(t1.timestamp)))) end as OFFICE_ON,
case when t1.event = 'PARTY_ON' then 
   (SUM(Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp) , 
        FROM_UNIXTIME(t1.timestamp))))  end as PARTY_ON,
case when t1.event = 'OUTDOOR_ON' then 
   (SUM(Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp) , 
         FROM_UNIXTIME(t1.timestamp))))  end as OUTDOOR_ON
FROM logs13 AS t1 
JOIN logs13 AS t2 ON (t2.id = t1.id - 1) 
       where  t1.wonderFit_ID = '0000000037fd0ef6' and t1.client_name ='Puma'  And FROM_UNIXTIME(t1.timestamp,"%Y")='2017' and FROM_UNIXTIME(t1.timestamp,"%M")='April'  and Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp) , FROM_UNIXTIME(t1.timestamp)) >= 0 
group by t1.event, FROM_UNIXTIME(t1.timestamp,"%d")  
order by FROM_UNIXTIME(t1.timestamp,"%d");

enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

您期望在NULL的位置获得什么价值?

如果在NULL的位置需要0之类的东西,可以使用以下格式

CASE WHEN expr1 = expr2 THEN value1 ELSE value2 END

示例:

CASE when t1.event='ALL_OFF' THEN (SUM(Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp), FROM_UNIXTIME(t1.timestamp)))) ELSE 0 END as ALL_OFF

答案 1 :(得分:0)

将整个案例表达式放在SUM(case ... else ... end)函数中,然后使用COALESCE(SUM(...),0)

SELECT
        t1.client_name
      , FROM_UNIXTIME(t1.timestamp,"%d") AS Date
      , COALESCE(SUM(case when t1.event = 'ALL_OFF'    then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end),0) as ALL_OFF
      , COALESCE(SUM(case when t1.event = 'HOME_ON'    then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end),0) as HOME_ON
      , COALESCE(SUM(case when t1.event = 'OFFICE_ON'  then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end),0) as OFFICE_ON
      , COALESCE(SUM(case when t1.event = 'PARTY_ON'   then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end),0) as PARTY_ON
      , COALESCE(SUM(case when t1.event = 'OUTDOOR_ON' then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end),0) as OUTDOOR_ON
FROM logs13 AS t1 
JOIN logs13 AS t2 ON (t2.id = t1.id - 1) 
WHERE  t1.wonderFit_ID = '0000000037fd0ef6' 
AND t1.client_name ='Puma'  
AND FROM_UNIXTIME(t1.timestamp,"%Y")='2017' 
AND FROM_UNIXTIME(t1.timestamp,"%M")='April'  
AND Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp) , FROM_UNIXTIME(t1.timestamp)) >= 0 
GROUP BY t1.event, FROM_UNIXTIME(t1.timestamp,"%d")  
ORDER BY FROM_UNIXTIME(t1.timestamp,"%d")
;

在MYSQL中你可以使用IFNULL()而不是COALESCE(),但是后期是SQL标准的一部分,我觉得最好尽可能避免使用dbms特定的函数。

OR to oputput''而不是零

SELECT
        t1.client_name
      , FROM_UNIXTIME(t1.timestamp,"%d") AS Date
      , COALESCE(CAST(SUM(case when t1.event = 'ALL_OFF'    then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end) AS CHAR),'') as ALL_OFF
      , COALESCE(CAST(SUM(case when t1.event = 'HOME_ON'    then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end) AS CHAR),'') as HOME_ON
      , COALESCE(CAST(SUM(case when t1.event = 'OFFICE_ON'  then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end) AS CHAR),'') as OFFICE_ON
      , COALESCE(CAST(SUM(case when t1.event = 'PARTY_ON'   then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end) AS CHAR),'') as PARTY_ON
      , COALESCE(CAST(SUM(case when t1.event = 'OUTDOOR_ON' then Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp),FROM_UNIXTIME(t1.timestamp)) end) AS CHAR),'') as OUTDOOR_ON
FROM logs13 AS t1 
JOIN logs13 AS t2 ON (t2.id = t1.id - 1) 
WHERE  t1.wonderFit_ID = '0000000037fd0ef6' 
AND t1.client_name ='Puma'  
AND FROM_UNIXTIME(t1.timestamp,"%Y")='2017' 
AND FROM_UNIXTIME(t1.timestamp,"%M")='April'  
AND Timestampdiff(MINUTE,FROM_UNIXTIME(t2.timestamp) , FROM_UNIXTIME(t1.timestamp)) >= 0 
GROUP BY t1.event, FROM_UNIXTIME(t1.timestamp,"%d")  
ORDER BY FROM_UNIXTIME(t1.timestamp,"%d")
;