Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using LAG in SQL returns 'unqualified name' error #8873

Open
lara-bellatin opened this issue Oct 28, 2024 · 1 comment
Open

Using LAG in SQL returns 'unqualified name' error #8873

lara-bellatin opened this issue Oct 28, 2024 · 1 comment
Assignees
Labels
api:sql Issues related to SQL API enhancement New feature proposal

Comments

@lara-bellatin
Copy link

lara-bellatin commented Oct 28, 2024

Describe the bug
Using LAG to compare results with previous time periods returns the following error:

Query 1 ERROR at Line 1: : ERROR:  Error during rewrite: Error during planning: No field with unqualified name 'LAG(SUM(source.importeVenta),Int64(1)) PARTITION BY [#source.cliente] ORDER BY [#source.mes ASC NULLS LAST]'. Valid fields are 'LAG(SUM(source.importeVenta),Int64(1)) PARTITION BY [#source.cliente AS cliente] ORDER BY [#source.mes AS mes ASC NULLS LAST]', 'source.mes', 'source.cliente', 'SUM(source.importeVenta)'.. Please check logs for additional information.
QUERY: SELECT "source"."mes" AS "mes", "source"."cliente" AS "cliente", SUM("source"."importeVenta") AS "sum", LAG(SUM("source"."importeVenta"), 1) OVER (PARTITION BY "source"."cliente" ORDER BY "source"."mes" ASC) AS "Sum of ImporteVenta (anterior año)", (CAST(SUM("source"."importeVenta") AS FLOAT) / NULLIF(LAG(SUM("source"."importeVenta"), 1) OVER (PARTITION BY "source"."cliente" ORDER BY "source"."mes" ASC), 0)) - 1 AS "Sum of ImporteVenta (% vs anterior año)" FROM (SELECT DATE_TRUNC('year', "Venta"."mesFactura") AS "mes", "Cliente"."nombre" AS "cliente", "Venta"."importeVenta" AS "importeVenta" FROM "Venta" LEFT JOIN "Cliente" ON "Venta"."__cubeJoinField" = "Cliente"."__cubeJoinField") AS "source" GROUP BY "source"."mes", "source"."cliente" ORDER BY "source"."mes" ASC, "source"."cliente" ASC

To Reproduce
SQL Query used:

SELECT
  "source"."mes" AS "mes",
  "source"."cliente" AS "cliente",
  SUM("source"."importeVenta") AS "sum",
  LAG(SUM("source"."importeVenta"), 1) OVER (
    PARTITION BY "source"."cliente"
   
ORDER BY
      "source"."mes" ASC
  ) AS "Sum of ImporteVenta (anterior año)",
  (
    CAST(SUM("source"."importeVenta") AS float) / NULLIF(
      LAG(SUM("source"."importeVenta"), 1) OVER (
        PARTITION BY "source"."cliente"
        ORDER BY
          "source"."mes" ASC
      ),
      0
    )
  ) - 1 AS "Sum of ImporteVenta (% vs anterior año)"
FROM
  (
    SELECT
      DATE_TRUNC('year', "Venta"."mesFactura") AS "mes",
      "Cliente"."nombre" AS "cliente",
      "Venta"."importeVenta" AS "importeVenta"
    FROM
      "Venta"
     
LEFT JOIN "Cliente" ON "Venta"."__cubeJoinField" = "Cliente"."__cubeJoinField"
  ) AS "source"
GROUP BY
  "source"."mes",
  "source"."cliente"
ORDER BY
  "source"."mes" ASC,
  "source"."cliente" ASC

Expected behavior
I expected to see the total sales for each client in a given month, their total sales last month and the percentage change
importeVenta is a simple number-type property and the results do show up when I don't include the lag part of the query

Version:
0.36.6

@igorlukanin igorlukanin added the api:sql Issues related to SQL API label Nov 1, 2024
@igorlukanin igorlukanin added the enhancement New feature proposal label Nov 1, 2024
@igorlukanin
Copy link
Member

Hi @lara-bellatin 👋

I believe any window functions, including LAG, are not currently supported by the SQL API.

I feel like calculations like this should be moved to your data model so that Cube generates SQL for you and the "client-side" queries are free of window functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api:sql Issues related to SQL API enhancement New feature proposal
Projects
None yet
Development

No branches or pull requests

3 participants