I have SQL view data_start
with 3 columns
CALMONTH, COSTCENTER, Kontonummer
Sample data:
CALMONTH COSTCENTER Kontonummer
-------------------------------
202301 A AA
202302 A AB
202101 B BA
202012 B BB
202204 C CA
202312 C CB
...
How can I keep only the rows per COSTCENTER
group, which CALMONTH
maximum is greater than a specific value.
So for example I only want to keep the COSTCENTER
where at least one CALMONTH
is greater than 202201.
The output should be:
CALMONTH COSTCENTER Kontonummer
-------------------------------
202301 A AA
202302 A AB
202204 C CA
202312 C CB
...
I tried this SQL statement:
SELECT
"CALMONTH", "COSTCENTER", "Kontonummer"
GROUP BY
("COSTCENTER") %>%
SERIES_FILTER(max("CALMONTH") > 202201) %>%,
FROM "data_start"
But this results in an error:
Error Extraneous BY, expecting ‘,’, FROM
The SQL is in the SAP HANA CLOUD and called SQLSCRIPT, which has the syntax from mysql.
2
Answers
This can be done using
group by
andhaving
:First identify
COSTCENTER
havingCALMONTH
maximum is greater than 202201 :Then join this dataset with the table to get the expected output :
Demo on mysql here
You can also make use of CTE Common_table_expression
to achieve same result.