简体   繁体   English

在 CubeJS 中实现 DISTINCT ON

[英]Implementing DISTINCT ON in CubeJS

I have a Postgres table like this, with device ID, timestamp, and the status of the device at that time:我有一个像这样的 Postgres 表,其中包含设备 ID、时间戳和当时设备的状态:

dev_id  | timestamp             | status
----------------------------------------
1       | 2020-08-06 23:00:00   | 1
2       | 2020-08-06 23:00:00   | 0
3       | 2020-08-06 23:00:00   | 1
2       | 2020-08-06 23:05:00   | 1
3       | 2020-08-06 23:05:00   | 0
1       | 2020-08-06 23:10:00   | 0

I want to see in their respective latest timestamp, how many of devices were functioning and how many not functioning.我想在他们各自的最新时间戳中查看有多少设备正在运行,有多少没有运行。 In Postgres, I can use DISTINCT ON and write the query like this:在 Postgres 中,我可以使用DISTINCT ON并像这样编写查询:

SELECT status, COUNT(status) 
FROM
  (
    SELECT DISTINCT ON (dev_id) dev_id,
      timestamp,
      status 
    FROM
      sample_metrics_data 
    ORDER BY
      dev_id,
      timestamp DESC
  ) sub 
GROUP BY status; 

This will result in:这将导致:

value   | count
---------------
0       | 2
1       | 1

(2 devices, #1 & #3, have a status of 0, while 1, #2, has a status of 1.) How can I create something like this in CubeJS? (2 个设备,#1 和 #3,状态为 0,而 1,#2,状态为 1。)如何在 CubeJS 中创建类似的东西? Is DISTINCT ON supported, and if not, what is the way around it?是否支持DISTINCT ON ,如果不支持,解决方法是什么?

Alternatively, the query can be written using inner join:或者,可以使用内部联接编写查询:

SELECT status,
       Count(status)
FROM   sample_metrics_data
       JOIN (SELECT dev_id         id,
                    Max(timestamp) ts
             FROM   sample_metrics_data
             GROUP  BY dev_id) max_ts
         ON timestamp = max_ts.ts
            AND dev_id = max_ts.id
GROUP BY status; 

I would need to do an inner join, but it seems only LEFT JOIN is available.我需要进行内部连接,但似乎只有 LEFT JOIN 可用。

In your case, if you need to build a graph of how many devices were online, then a typical solution to your problem would be在您的情况下,如果您需要构建一个有多少设备在线的图表,那么您的问题的典型解决方案是

  1. Build a cube in which there will be data on the change in the number of devices online.构建一个立方体,其中会有关于在线设备数量变化的数据。
  2. Create measures with rollingWindow使用rollingWindow创建度量

For example, I made a table as in your question例如,我在你的问题中做了一个表格

sample_metrics 表结构

And create this cube并创建这个立方体

cube(`SampleMetricsData`, {
  sql: "SELECT *, device_status - COALESCE(LAG(device_status) OVER (PARTITION BY id ORDER BY timemark ASC), 0) as rolling_status FROM ab_api_test.sample_metrics ORDER BY `sample_metrics`.`timemark` DESC",
   
  measures: { 
    rollingStatusTotal: {
      sql: `rolling_status`,
      type: `sum`, 
      rollingWindow: { 
        trailing: `unbounded`, 
      }, 
    },  
  },
  
  dimensions: {
    id: {
      sql: `id`,
      type: `number`,
      primaryKey: true
    },
    
    timemark: {
      sql: `timemark`,
      type: `time`
    }, 
  }
});

On this cube you can see online device chart with this query在这个多维数据集上,您可以使用此查询查看在线设备图表

{"measures":["SampleMetricsData.rollingStatusTotal"],"timeDimensions":[{"dimension":"SampleMetricsData.timemark","granularity":"hour","dateRange":"This month"}],"order":{},"dimensions":[],"filters":[]}

Possibly you should see this tutorial , It looks like something similar for your task.可能您应该看到本教程,它看起来与您的任务类似。 And one more related question is here还有一个相关的问题是here

Note笔记

You can also write a query like this to create a cube from your data.您还可以编写这样的查询来从您的数据创建一个多维数据集。 But this is not best practices但这不是最佳实践

select * from (
     SELECT DISTINCT ON (dev_id) dev_id,
       timestamp,
       status
     FROM
       sample_metrics_data
     ORDER BY
       dev_id,
       timestamp DESC
) as sample_metrics

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

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