如何使用SQLite确定特定披萨店每天的平均订单?
表看起来像这样:
CREATE TABLE pizzaorders (
id STRING PRIMARY KEY,
pizza_place STRING,
created_utc STRING,
);
我设法为每天的比萨店'Dominos'设置了GROUP BY的比萨饼数量(顺便说一下,created_utc属性以UTC世纪格式存储):
SELECT
strftime('%d-%m-%Y', created_utc, 'unixepoch') AS dt,
COUNT(*) AS orders
FROM pizzaorders WHERE pizza_place = 'dominos'
GROUP BY dt;
26-10-2017|543
24-10-2017|581
18-10-2017|593
30-10-2017|608
31-10-2017|621
25-10-2017|647
但是,如何使用AVG()
获取特定披萨店每天的平均订单(例如本例中的多米诺骨牌)?
您可以将总计数除以不同的天数:
SELECT COUNT(*) /
COUNT(DISTINCT STRFTIME('%d-%m-%Y', created_utc, 'unixepoch'))
FROM pizzaorders
WHERE pizza_place = 'dominos'