我有这样的SQL请求:
SELECT date_format(`user_date_accountcreated`, "%Y-%m-%d") as date, Count(*) as total from users
where `user_date_accountcreated`
BETWEEN '2016-09-18 00:00:00' AND '2016-10-03 23:59:59'
GROUP BY date_format(`user_date_accountcreated`, "%y%m%d")
但是当没有记录时,由于GROUP BY
我的结果是空的,没有什么可以分组......
我尝试使用ISNULL
这样:
SELECT date_format(`user_date_accountcreated`, "%Y-%m-%d") as date, ISNULL(Count(*), 0) as total from users
where `user_date_accountcreated` BETWEEN '2016-09-18 00:00:00' AND '2016-10-03 23:59:59'
GROUP BY date_format(`user_date_accountcreated`, "%y%m%d")
但我得到一个SQL错误:#1582 - 调用本机函数时参数计数不正确' ISNULL'
当我们使用GROUP BY
?
答案 0 :(得分:1)
使用条件聚合:
SELECT DATE_FORMAT(user_date_accountcreated, "%Y-%m-%d") AS date,
SUM(CASE WHEN user_date_accountcreated BETWEEN '2016-09-18 00:00:00' AND
'2016-10-03 23:59:59'
THEN 1 ELSE 0 END) AS total_from_users
GROUP BY DATE_FORMAT(user_date_accountcreated, "%y%m%d")
答案 1 :(得分:0)
检查
SELECT
date_format(`user_date_accountcreated`, "%Y-%m-%d") as date,
Count( date_format(`user_date_accountcreated`, "%Y-%m-%d") ) as total
from users
where `user_date_accountcreated`
BETWEEN '2016-09-18 00:00:00' AND '2016-10-03 23:59:59'
GROUP BY date_format(`user_date_accountcreated`, "%y%m%d")
答案 2 :(得分:0)
首先,查询无法返回不存在的记录。如果你想看到它们,你必须生成它们。以下查询使用UNION ALL在查询未返回任何行时从查询或生成的记录中获取记录:
[HTTP] --> POST /wd/hub/session/9d9d5814-2085-4f25-bb5e-e4cc0838fa25/elements {"using":"id","value":"Performances button"}
[MJSONWP] Calling AppiumDriver.findElements() with args: ["id","Performances button"...
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"Performances button","context":"","multiple":true}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"Performances button","context":"","multiple":true}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'Performances button' using 'ID' with the contextId: '' multiple: true
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=uk.co.bbc.music:id/Performances button]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=uk.co.bbc.music:id/Performances button]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0)
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=uk.co.bbc.music:id/Performances button]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=android:id/Performances button]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=android:id/Performances button]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0)
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=android:id/Performances button]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[DESCRIPTION=Performances button]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[DESCRIPTION=Performances button]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0)
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[DESCRIPTION=Performances button, INSTANCE=0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (1)
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[DESCRIPTION=Performances button, INSTANCE=1]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":[{"ELEMENT":"11"}]}
[debug] [AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Responding to client with driver.findElements() result: [{"ELEMENT":"11"}]
[HTTP] <-- POST /wd/hub/session/9d9d5814-2085-4f25-bb5e-e4cc0838fa25/elements 200 80730 ms - 90
使用with子句可以实现同样的目的:
SELECT
date_format(user_date_accountcreated, '%Y-%m-%d') AS date,
COUNT(*) AS total
FROM users
WHERE user_date_accountcreated >= '2016-09-18'
AND user_date_accountcreated < '2016-10-04'
GROUP BY date_format(user_date_accountcreated, '%Y-%m-%d')
UNION ALL
SELECT null, 0
WHERE NOT EXISTS
(
SELECT *
FROM users
WHERE user_date_accountcreated >= '2016-09-18'
AND user_date_accountcreated < '2016-10-04'
);
我还纠正了你的查询中的两个错误:
WITH mydates AS
(
SELECT
date_format(user_date_accountcreated, '%Y-%m-%d') AS date,
COUNT(*) AS total
FROM users
WHERE user_date_accountcreated >= '2016-09-18'
AND user_date_accountcreated < '2016-10-04'
GROUP BY date_format(user_date_accountcreated, '%Y-%m-%d')
)
SELECT date, total FROM mydates
UNION ALL
SELECT null, 0 WHERE NOT EXISTS (select * from mydates);
,如'
。'%Y-%m-%d'
与'%Y-%m-%'
)分组相同的值。它适用于您的情况,但在其他情况下可能会导致问题。