简体   繁体   English

如何在 cube.js 中实现子查询

[英]How to implement subqueries in cube.js

I'm struggling to see how I would represent the following type of postgres SQL query in a cube.js schema:我正在努力了解如何在 cube.js 模式中表示以下类型的 postgres SQL 查询:

SELECT
CASE
    WHEN COUNT(tpp.net_total_amount) > 0 THEN
        SUM(tpp.net_total_amount) / COUNT(tpp.net_total_amount)
    ELSE
        NULL
    END AS average_spend_per_customer
FROM
    (
        SELECT
            SUM(ts.total_amount) AS net_total_amount
        FROM
            postgres.transactions AS ts
        WHERE
            ts.transaction_date >= '2020-11-01' AND
            ts.transaction_date < '2020-12-01'
        GROUP BY
            ts.customer_id,
            ts.event_id
    ) AS tpp
;

I had the feeling that pre-aggregations might be what I'm after, but that doesn't seem to be the case after looking into them.我感觉预聚合可能是我所追求的,但在研究它们之后似乎并非如此。 I can get a list of total amount spent per customer per event with the following schema:我可以使用以下架构获得每个客户每个事件的总花费列表:

cube(`TransactionTotalAmountByCustomerAndEvent`, {
  sql: `SELECT * FROM postgres.transactions`,

  joins: {

  },

  measures: {
    sum: {
      sql: `SUM(total_amount)`,
      type: `number`
    }
  },

  dimensions: {
    eventId: {
      sql: `event_id`,
      type: `string`
    },

    customerId: {
      sql: `customer_id`,
      type: `string`
    },

    transactionDate: {
      sql: `transaction_date`,
      type: `time`
    }
  },

  preAggregations: {
    customerAndEvent: {
      type: `rollup`,
      measureReferences: [sum],
      dimensionReferences: [customerId, eventId]
    }
  }
});

But that is really just giving me the output of the inner SELECT statement grouped by customer and event.但这实际上只是给了我按客户和事件分组的内部 SELECT 语句的 output。 How do I query the cube to get the average customer spend per event figure I'm after?如何查询多维数据集以获取我所追求的每个事件的平均客户支出?

You might find it easier to model the dataset as two different cubes, Customers and Transactions .您可能会发现将数据集 model 作为两个不同的多维数据集CustomersTransactions更容易。 You'll then need to set up a join between the cubes and then create a special dimension with the subQuery property set to true .然后,您需要在多维数据集之间建立连接,然后创建一个特殊维度,并将subQuery属性设置为true I've included an example below to help you understand:我在下面提供了一个示例以帮助您理解:

cube('Transactions', {
  sql: `SELECT * FROM postgres.transactions`,

  measures: {
    spend: {
      sql: `total_amount`,
      type: `number`,
    },
  },

  dimensions: {
    eventId: {
      sql: `event_id`,
      type: `string`
    },

    customerId: {
      sql: `customer_id`,
      type: `string`
    },

    transactionDate: {
      sql: `transaction_date`,
      type: `time`
    },
  },
})

cube('Customers', {
  sql: `SELECT customer_id FROM postgres.transactions`,

  joins: {
    Transactions: {
      relationship: `hasMany`,
      sql: `${Customers}.id = ${Transactions}.customerId`
    }
  },

  measures: {
    averageSpend: {
      sql: `${spendAmount}`,
      type: `avg`,
    },
  },

  dimensions: {
    id: {
      sql: `customer_id`,
      type: `string`
    },
    spendAmount: {
      sql: `${Transactions.spend}`,
      type: `number`,
      subQuery: true
    },
  }
})

You can find more information on the Subquery page on the documentation您可以在文档的子查询页面上找到更多信息

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

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