繁体   English   中英

使用 Cube.js 进行外部连接

[英]Outer join using Cube.js

我正在使用 Cube.js 从 Postgres 中获取数据。 默认情况下,cube.js 对 2 个选定的表进行左连接。 有什么方法可以通过完全外连接获得结果?

用户登记表
身份证时间
1 10.00
3 9.00

最后一个活动表
身份证时间
1 11.00
2 10.00

所以我想要的 output 是
ID Last_active_time Register_time
1 11.00 10.00
2 10.00 ----
3 ---- 9.00

Cube.js 仅使用LEFT JOIN来表达立方体之间的关系,以鼓励正确的 Cube.js 架构设计: https://cube.dev/docs/joins 您的案例可以表示为以下 Cube.js 架构:

cube(`Users`, {
  sql: `
  SELECT DISTINCT id FROM users_register 
  UNION 
  SELECT DISTINCT id FROM last_active`,

  joins: {
    UsersRegister: {
      sql: `${Users}.id = ${UsersRegister}.id`,
      relationship: `hasMany`
    },
    UsersLastActive: {
      sql: `${Users}.id = ${UsersLastActive}.id`,
      relationship: `hasMany`
    }
  },

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

cube(`UsersRegister`, {
  sql: `select * from users_register`,

  measures: {
    registerTime: {
      sql: `time`,
      type: `min`
    }
  },

  dimensions: {
    id: {
      sql: `id`, // if id is unique within users_register
      type: `number`,
      primaryKey: true
    }
  }
});

cube(`UsersLastActive`, {
  sql: `select * from last_active`,

  measures: {
    lastActiveTime: {
      sql: `time`,
      type: `max`
    }
  },

  dimensions: {
    id: {
      sql: `id`, // if id is unique within last_active
      type: `number`,
      primaryKey: true
    }
  }
});

查询以获得所需的结果:

{
  measures: ['UsersLastActive.lastActiveTime', 'UsersRegister.registerTime'],
  dimensions: ['Users.id']
}

暂无
暂无

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

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