How to use pre-aggregated data from one cube to another cube in CubsJS

41 Views Asked by At

I have a SQL which is very slow and the result of this SQL does not change frequency. So, I have created a cube with this SQL and trying to pre aggregate that using original_sql pre-aggregation type.

cube(`Product`, {  
  data_source: `default`,  
  
  sql: `SQL_IN_PRODUCT_CUBE`,  
  
  dimensions: {  
    product_id: {  
      sql: `product_id`,  
      type: `string`,  
      primary_key: true  
    },  
  
    amount: {  
      sql: `amount`,  
      type: `number`,  
      format: `currency`  
    },   
  },  
  
  pre_aggregations: {  
    base: {  
      type: `original_sql`,  
      external: false,  
    },  
  
    main: {  
      dimensions: [  
          Product.product_id,  
          Product.amount,  
      ],  
      use_original_sql_pre_aggregations: true,  
      refresh_key: {  
        every: `1 day`,  
      },  
    },  
  },  
});

The data generated by this SQL SQL_IN_PRODUCT_CUBE will be queried again by another SQL in another cube named ProductSnapshot like this:

cube(`ProductSnapshot`, {  
  data_source: `default`,  
  
  sql: `SELECT product_count_at_month_start, ... FROM SQL_IN_PRODUCT_CUBE WHERE ...`,  
  
  dimensions: {  
    product_id: {  
      sql: `product_id`,  
      type: `string`,  
      primary_key: true  
    },  
  
    product_count_at_month_start: {  
      type: `count`,  
    },   
  },   
});

The result of SQL_IN_PRODUCT_CUBE is constant, so I was trying to figure out if I can use the pre-aggregated data from Product cube in the ProductSnapshot cube while making a SQL query. Can anyone please help me understand how to achieve this? Or if this is achievable?

0

There are 0 best solutions below