cube icon indicating copy to clipboard operation
cube copied to clipboard

ResultSet.totalRow returns the first row instead of total

Open myself7890 opened this issue 4 years ago • 4 comments

Describe the bug When calling ResultSet.totalRow, I get the first data row instead of a row containing the totals.

To Reproduce Call ResultSet.totalRow().

Expected behavior Considering the name of the method, I expect the result to be a row containing the totals of each measure.

Screenshots image

Minimally reproducible Cube Schema

cube(`Sales`, {
    sql: `SELECT * FROM sales`,
    joins: {
        Stores: {
            relationship: `belongsTo`,
            sql: `${Stores}.id = ${CUBE}.store_id`,
        },
    },
    measures: {
        // Sales
        subtotal: {
            sql: `subtotal`,
            type: `sum`,
            filters: [{sql: `${CUBE}.is_refund = 0`}],
        },
        discount: {
            type: `sum`,
            sql: `discount`,
            filters: [{sql: `${CUBE}.is_refund = 0`}],
        },
        taxes: {
            sql: `taxes`,
            type: `sum`,
            filters: [{sql: `${CUBE}.is_refund = 0`}],
        },
        amount: {
            sql: `amount`,
            type: `sum`,
            filters: [{sql: `${CUBE}.is_refund = 0`}],
        },
        refundsAmount: {
            sql: `amount`,
            type: `sum`,
            filters: [{sql: `${CUBE}.is_refund = 1`}],
        },
        refundsSubtotal: {
            type: `sum`,
            sql: `subtotal`,
            filters: [{sql: `${CUBE}.is_refund = 1`}],
        },
        refundsGiftcard: {
            sql: `giftcard`,
            type: `sum`,
            filters: [{sql: `${CUBE}.is_refund = 1`}],
        },
        // Aggregations
        gross: {
            type: `number`,
            sql: `IFNULL(${subtotal},0) - IFNULL(${giftcard},0)`,
        },
        salesSubtotal: {
            type: `number`,
            sql: `IFNULL(${gross},0) - IFNULL(${discount},0)`
        },
        refundsGross: {
            type: `number`,
            sql: `IFNULL(${refundsSubtotal},0) - IFNULL(${refundsGiftcard},0)`,
        },
    },
    dimensions: {
        id: {
            sql: `id`,
            type: `number`,
            primaryKey: true
        },

        createdAt: {
            sql: `created_at`,
            type: `time`
        },
    },
});

Version: 0.28.52

myself7890 avatar Mar 14 '22 12:03 myself7890

@myself7890 Yep. It's indeed not ideal naming. Originally it was designed to access rows of single-row queries like total queries. So is your issue more around bad naming or rather totals row evaluation?

paveltiunov avatar Mar 28 '22 03:03 paveltiunov

@paveltiunov There is no explanation in the documentation about what that method is doing, so I thought it was doing what it seems to be saying: returning a row containing the totals of all the measures.

Do you know another way to easily do that without having to resort to another query?

myself7890 avatar Mar 28 '22 12:03 myself7890

@myself7890 The only reliable way to do it is to do a separate query. Do you have any issues with that?

paveltiunov avatar Mar 28 '22 23:03 paveltiunov

We have a few places in frontend applications where we need something similar to this

We just take the cube resultSet data and use a util function that we create to do this for us.

We don't sum the totals of measures but it would be similar

PieterVanZyl-Dev avatar Apr 29 '22 20:04 PieterVanZyl-Dev