將整個案件表達的SUM(情況下,當內.. 。否則...結束)函數,然後使用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中,你可以使用IFNU LL()而不是COALESCE(),但後期是SQL標準的一部分,我覺得儘可能避免使用dbms特定函數會更好。
或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")
;
我不想0以及因爲當使用趨勢線在我的儀表盤我沒有看起來good..so我希望值僅 –
燦你給你的預期產出?這將有所幫助 –
**我不想要0 **,但這些是數字列,您必須將所有數字轉換爲字符串才能輸出''而不是NULL。如果要將這些列保留爲數字,則它爲零或NULL。 –