SQL计数并比较两个创建的_AT列
我有一张桌子如下,
id | createat_1 | createat_2 |
---|---|---|
abc | 2022-06-10 20:28:37 | |
CFR | 2022-06-13 10:00 | :00:12 2022-06-10 20:28:14 |
PFR | 20222-06-06-17 12 12 :20:40 | |
XYZ | 2022-06-15 11:00:12 | 2022-06-10 16:45:05 |
DFL | 2022-06-13 15:00:06 | |
FGT | 2022-06-20 10:00:00: | 20 2022-06-10 13:34:55 |
我已经使用此查询来分别计算每个列的特定日期的行数
SELECT
(CAST(datetrunc(‘day’, ‘createdAt_1’ + (INTERVAL '1 day'))) AS timestamp) + (INTERVAL '-1 day')) AS ‘new user’,
count(*) AS ‘count’
FROM Table
WHERE time_interval
GROUP BY ‘new user’
:得到类似:
Day | Count |
---|---|
2022-06-10 | 1 |
2022-06-13 | 2 |
2022-06-15 | 1 |
我希望能够比较这两个列并获得特定的百分比day count(createat_1) / count(createat_2) * 100,但我看不到如何轻松完成。
I have got a table as follows,
ID | CreatedAt_1 | CreatedAt_2 |
---|---|---|
ABC | 2022-06-10 20:28:37 | |
CFR | 2022-06-13 10:00:12 | 2022-06-10 20:28:14 |
PFR | 2022-06-17 12:20:40 | |
XYZ | 2022-06-15 11:00:12 | 2022-06-10 16:45:05 |
DFL | 2022-06-13 15:00:06 | |
FGT | 2022-06-20 10:00:20 | 2022-06-10 13:34:55 |
I already used this query to count number of rows on specific date for each column separately :
SELECT
(CAST(datetrunc(‘day’, ‘createdAt_1’ + (INTERVAL '1 day'))) AS timestamp) + (INTERVAL '-1 day')) AS ‘new user’,
count(*) AS ‘count’
FROM Table
WHERE time_interval
GROUP BY ‘new user’
And get something like :
Day | Count |
---|---|
2022-06-10 | 1 |
2022-06-13 | 2 |
2022-06-15 | 1 |
I would like to be able to compare both columns and get percentage on specific day count(createdAt_1) / count(createdAt_2) * 100 but i don’t see how to easily do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法验证是metabase SQL支持SQL Standard
cocece()
函数,但是该函数的目的是返回传递给它的参数之间遇到的第一个非零值(从左到右)。如果得到支持,我建议以下查询。cocece('createat_1','createat_2')
将在createat_1
中返回该值,如果不是null,但会返回create> create> createat_2 如果
Createat_1
为null。如果这两个列为零,则总体而言,它将返回null。dateTrunc()
函数本身应该足够。I wasn't able to verify is Metabase SQL supports the SQL standard
coalesce()
function, but the purpose of that function is to return the first non-null value it encounters amongst the parameters passed into it (left to right). If it is supported I suggest the query below.coalesce('createdAt_1', 'createdAt_2')
would return the value increatedAt_1
if that isn't NULL, but would return the value increatedAt_2
ifcreatedAt_1
is NULL. If both columns are NULL then overall it returns NULL.datetrunc()
function should be sufficient in itself.