SQL计数并比较两个创建的_AT列

发布于 2025-02-13 06:34:30 字数 1209 浏览 3 评论 0原文

我有一张桌子如下,

idcreateat_1createat_2
abc2022-06-10 20:28:37
CFR2022-06-13 10:00:00:12 2022-06-10 20:28:14
PFR20222-06-06-17 12 12 :20:40
XYZ2022-06-15 11:00:122022-06-10 16:45:05
DFL2022-06-13 15:00:06
FGT2022-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’

:得到类似:

DayCount
2022-06-101
2022-06-132
2022-06-151

我希望能够比较这两个列并获得特定的百分比day count(createat_1) / count(createat_2) * 100,但我看不到如何轻松完成。

I have got a table as follows,

IDCreatedAt_1CreatedAt_2
ABC2022-06-10 20:28:37
CFR2022-06-13 10:00:122022-06-10 20:28:14
PFR2022-06-17 12:20:40
XYZ2022-06-15 11:00:122022-06-10 16:45:05
DFL2022-06-13 15:00:06
FGT2022-06-20 10:00:202022-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 :

DayCount
2022-06-101
2022-06-132
2022-06-151

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2025-02-20 06:34:30

我无法验证是metabase SQL支持SQL Standard cocece()函数,但是该函数的目的是返回传递给它的参数之间遇到的第一个非零值(从左到右)。如果得到支持,我建议以下查询。

SELECT
      datetrunc('day', coalesce('createdAt_1', 'createdAt_2')) AS "new user"
    , count(*) AS "count"
FROM TABLE
-- WHERE time_interval is true ?? 
GROUP BY
      datetrunc('day', coalesce('createdAt_1', 'createdAt_2'))

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.

SELECT
      datetrunc('day', coalesce('createdAt_1', 'createdAt_2')) AS "new user"
    , count(*) AS "count"
FROM TABLE
-- WHERE time_interval is true ?? 
GROUP BY
      datetrunc('day', coalesce('createdAt_1', 'createdAt_2'))

coalesce('createdAt_1', 'createdAt_2') would return the value in createdAt_1 if that isn't NULL, but would return the value in createdAt_2 if createdAt_1 is NULL. If both columns are NULL then overall it returns NULL.

  • typically labels are denoted by double quotes e.g. "new user"
  • I don't believe you need to add or subtract intervals, or convert to timestamp. The datetrunc() function should be sufficient in itself.
  • I have not used the "new user" alias in the group by clause as this is my preference
  • It isn't clear to me what that where clause is achieving.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文