简体   繁体   English

Cube.dev - 如何在滚动 window 中获得参数“今天的日期”?

[英]Cube.dev - how to have the parameter "today date" in rolling window?

I have a PostgreSQL database in which users can order from a start date to an end date .我有一个 PostgreSQL 数据库,用户可以在其中订购从开始日期到结束日期 I want to know, for each day, how many users would be able to order.我想知道每天有多少用户可以订购。

Let's make an example, given:让我们举个例子,给定:

id ID from to
A一种 2023-01-01 2023-01-01 2023-01-03 2023-01-03
B 2023-01-02 2023-01-02 2023-01-07 2023-01-07
C C 2023-01-02 2023-01-02 2023-01-09 2023-01-09
D 2023-01-10 2023-01-10 2023-01-12 2023-01-12

For the first two weeks (let's suppose that the 01/01/2023 were on Monday), I would have (the third column is not needed):对于前两周(假设 01/01/2023 是星期一),我会有(不需要第三列):

day n_of_users_that_can_order n_of_users_that_can_order who
2023-01-01 2023-01-01 1 1个 A一种
2023-01-02 2023-01-02 3 3个 A, B, C甲、乙、C
2023-01-03 2023-01-03 3 3个 A, B, C甲、乙、C
2023-01-04 2023-01-04 2 2个 B, C乙、C
2023-01-05 2023-01-05 2 2个 B, C乙、C
2023-01-06 2023-01-06 2 2个 B, C乙、C
2023-01-07 2023-01-07 2 2个 B, C乙、C
2023-01-08 2023-01-08 1 1个 C C
2023-01-09 2023-01-09 1 1个 C C
2023-01-10 2023-01-10 1 1个 D
2023-01-11 2023-01-11 1 1个 D
2023-01-12 2023-01-12 0 0
2023-01-13 2023-01-13 0 0
2023-01-14 2023-01-14 0 0

My end result should be the above table aggregated per week:我的最终结果应该是每周汇总的上表:

week星期 total全部的
2023-01-01 to 2023-01-07 2023-01-01 至 2023-01-07 15 15
2023-01-08 to 2023-01-14 2023-01-08 至 2023-01-14 4 4个

I don't know how to do it with cube.dev, this is the idea I had for now:我不知道如何用 cube.dev 做到这一点,这是我现在的想法:

ube(`Users`, {
  sql: `SELECT * FROM public.users`,

  measures: {
    usersPerDayThatCanOrder: {
      type: `count`,
      sql: `id`,
      rollingWindow: {
        trailing: `1 day`,
        offset: `start`
      },
      filters: [
        { sql: `${CUBE}.can_order_from >= ${TODAY} AND ${CUBE}.can_order_to <= ${TODAY}` }, // NOTE: today doesn't exist
      ]
    }
  },

  dimensions: {
    id: {
      sql: `id`,
      type: `number`,
      primaryKey: true
    },

    canOrderFrom: {
      sql: `can_order_from`,
      type: `time`
    },

    canOrderTo: {
      sql: `can_order_to`,
      type: `time`
    },
  },

  dataSource: `default`
});

But it does not work because I do not know how to have the real ${TODAY} value and also how to aggregate per week.但它不起作用,因为我不知道如何获得真正的 ${TODAY} 价值以及如何每周汇总。

Here is a sql query which provides your expected result assuming that your table is named test :这是一个 sql 查询,假设您的表名为test ,它会提供您的预期结果:

SELECT d.week AS "week start"
     , (d.week + interval '6 days') :: date AS "week end"
     , sum( upper(daterange(d.week, (d.week + interval '1 week') :: date, '[)') * daterange(t."from", t."to", '[]'))
          - lower(daterange(d.week, (d.week + interval '1 week') :: date, '[)') * daterange(t."from", t."to", '[]'))
          )
  FROM
     ( SELECT generate_series(min(date_trunc('week', "from")), max("to"), interval '1 week') :: date AS week
         FROM test
     ) AS d
 INNER JOIN test AS t
    ON daterange(d.week, (d.week + interval '1 week') :: date, '[)') && daterange(t."from", t."to", '[]')
 GROUP BY d.week
 ORDER BY d.week
  • The subquery calculates the weeks start date covered by table test子查询计算表test涵盖的周开始日期
  • The INNER JOIN clause intersects the weeks with the user date ranges INNER JOIN子句将周与用户日期范围相交
  • Then rows are concatenated by weeks and the number of days are summed for all the users然后按周连接行,并对所有用户的天数求和

By the way, the 2023/01/01 seems not to be Monday but Sunday.顺便说一句,2023/01/01 似乎不是星期一而是星期日。

Result:结果:

week start周开始 week end周末 sum
2022-12-26 2022-12-26 2023-01-01 2023-01-01 1 1个
2023-01-02 2023-01-02 2023-01-08 2023-01-08 15 15
2023-01-09 2023-01-09 2023-01-15 2023-01-15 4 4个

see test result in dbfiddledbfiddle中查看测试结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM