Skip to content

Simple SELECT query with aggregation on two cube dimensions not possibleΒ #9523

@hvavhp

Description

@hvavhp

Failed SQL

SELECT 
   SUM(order_items_data.quantity * products_data.retail_price) 
FROM 
   order_items_data 
CROSS JOIN 
   products_data;

This is a fairly simple query, in a very reasonable use case, but failed to execute. It gives the following error:

ERROR:  Error: Expected single cube for dimension-only measure products_data.sum_order_items_, got order_items_data,products_data

Expectation
This query should works. At least it did prior to v.1.2.25

Logical Plan
It probably was introduced by this commit: #9335

Tool
PSQL

Version:
From v.1.2.25

Reproducibility

cubes:
  - name: order_items_data
    sql: >
      SELECT 
        1 as id, 1001 as order_id, 101 as product_id, 2 as quantity, 25.99 as price
      UNION ALL SELECT 
        2 as id, 1001 as order_id, 102 as product_id, 1 as quantity, 19.99 as price
      UNION ALL SELECT 
        3 as id, 1002 as order_id, 103 as product_id, 3 as quantity, 15.50 as price
      UNION ALL SELECT 
        4 as id, 1003 as order_id, 101 as product_id, 1 as quantity, 25.99 as price
      UNION ALL SELECT 
        5 as id, 1004 as order_id, 104 as product_id, 2 as quantity, 45.00 as price

    joins:
      - name: products_data
        relationship: many_to_one
        sql: "{CUBE.product_id} = {products_data.id}"

    dimensions:
      - name: id
        sql: id
        type: number
        primary_key: true
      - name: order_id
        sql: order_id
        type: number
      - name: product_id
        sql: product_id
        type: number
      - name: quantity
        sql: quantity
        type: number
      - name: price
        sql: price
        type: number
    measures:
      - name: total_price
        sql: price * quantity
        type: sum

  - name: products_data
    sql: >
      SELECT 
        101 as id, 'Laptop' as name, 'Electronics' as category, 800.00 as cost, 1299.99 as retail_price
      UNION ALL SELECT 
        102 as id, 'Mouse' as name, 'Electronics' as category, 15.00 as cost, 24.99 as retail_price
      UNION ALL SELECT 
        103 as id, 'Headphones' as name, 'Electronics' as category, 35.00 as cost, 59.99 as retail_price
      UNION ALL SELECT 
        104 as id, 'Monitor' as name, 'Electronics' as category, 150.00 as cost, 249.99 as retail_price
      UNION ALL SELECT 
        105 as id, 'Keyboard' as name, 'Electronics' as category, 45.00 as cost, 89.99 as retail_price

    joins:
      - name: order_items_data
        relationship: one_to_many
        sql: "{CUBE.id} = {order_items_data.product_id}"

    dimensions:
      - name: id
        primary_key: true
        sql: id
        type: number
      - name: name
        sql: name
        type: string
      - name: category
        sql: category
        type: string
      - name: cost
        sql: cost
        type: number
      - name: retail_price
        sql: retail_price
        type: number

    measures:
      - name: profit_margin
        sql: retail_price - cost
        type: sum

Metadata

Metadata

Assignees

No one assigned

    Labels

    api:sqlIssues related to SQL API

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions