Not a group by function at a cumulative query - sql

I'm making a cumulative query, which shows the evolution of clients in my database. To get these query, I use the year and the week of year they joined in the client database.
I have following query to search for relevant data:
SELECT DD.CAL_YEAR, DD.WEEK_OF_YEAR, SUM(COUNT(DISTINCT FAB.ID)) OVER ( ORDER BY DD.CAL_DATE ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS "Number of account statements"
FROM CLIENT_DATABASE FAB
JOIN DIM_DATE DD ON FAB.BALANCE_DATE_ID = DD.ID
GROUP BY DD.CAL_YEAR, DD.WEEK_OF_YEAR;
But when I compile this query, I get following error:
Error: ORA-00979: not a GROUP BY expression
SQLState: 42000 ErrorCode: 979
How can I fix this?

Since you are grouping by DD.CAL_YEAR, DD.WEEK_OF_YEAR, you can't use DD.CAL_DATE in the order by clause of your cumulative sum function.
It's hard for me to say exactly what you are trying to do without fully understanding your data. But, logically, it does seem like you should be able to simply use DD.CAL_YEAR, DD.WEEK_OF_YEAR in the order by clause instead of DD.CAL_DATE, and still get the results the way you are expecting.
So something like this:
SUM(COUNT(DISTINCT FAB.ID)) OVER ( ORDER BY D.CAL_YEAR, DD.WEEK_OF_YEAR ...

Related

MAX in Select statement not returning the highest value?

I have a question regarding the max-statement in a select -
Without the MAX-statemen i have this select:
SELECT stockID, DATE, close, symbol
FROM ta_stockprice JOIN ta_stock ON ta_stock.id = ta_stockprice.stockID
WHERE stockid = 8648
ORDER BY close
At the end i only want to have the max row for the close-column so i tried:
Why i didn´t get date = "2021-07-02" as output?
(i saw that i allways get "2021-07-01" as output - no matter if i use MAX / MIN / AVG...)
The MAX() turns the query into an aggregation query. With no GROUP BY, it returns one row. But the query is syntactically incorrect, because it mixes aggregated and unaggregated columns.
Once upon a time, MySQL allowed such syntax in violation of the SQL Standard but returned values from arbitrary rows for the unaggreged columns.
Use ORDER BY to do what you want:
SELECT stockID, DATE, close, symbol
FROM ta_stockprice JOIN ta_stock ON ta_stock.id = ta_stockprice.stockID
WHERE stockid = 8648
ORDER BY close DESC
LIMIT 1;

Access: GROUP BY with IIf() condition

I'm trying to run the following Code in Access:
SELECT T_SAP_IST.month, SUM(T_SAP_IST.value), IIf([description]="hours","hours","nonhours") AS descr
FROM T_SAP_IST
GROUP BY month, descr
My goal is to get the sum of all values for every month divided into "hours" and "nonhours". However, I get an error which says "'IIf([description]="hours","hours","nonhours")' is not part of an aggregate function"
How can I fix this? Thanks for your help!
In MS Access, you need to repeat the expression:
SELECT T_SAP_IST.month, SUM(T_SAP_IST.value),
IIf([description]="hours","hours","nonhours") AS descr
FROM T_SAP_IST
GROUP BY month, IIf([description] = "hours", "hours", "nonhours");
You can also use a subquery, so you don't have to repeat the expression. MS Access does not support column aliases in the GROUP BY.

Cumulated sums in sql

I have a problem, I need to do an acomulative sum for each month of each year and I have been searching the internet and the solution is to use the ORDER (ORDER BY), but it gives me an error that there is an error near order and it does not explain me nothing more. The syntax is correct according to the internet, but I do not understand why it does not go. I tried to convert the date to string but it does not let me either. Any solution for this?
This is my consult:
SELECT YEAR(FECHA_IMPUT) AÑO,
MONTH(FECHA_IMPUT) MES,
COD_MAQUINA ,
SUM(CANTIDAD_OK) SUMA,
SUM(CANTIDAD_OK) OVER(ORDER BY DATEPART(mm,FECHA_IMPUT)) AS suma
FROM RTMAQUINA
WHERE COD_MAQUINA='LB_TRASVASE'
GROUP BY COD_MAQUINA, MONTH(FECHA_IMPUT),YEAR(FECHA_IMPUT)
ORDER BY YEAR(FECHA_IMPUT), MONTH(FECHA_IMPUT) ASC
ERROR: incorrect syntax near the order
Result query
ERROR: incorrect syntax near the order
I believe the syntax you want is:
SELECT YEAR(FECHA_IMPUT) AÑO,
MONTH(FECHA_IMPUT) MES,
COD_MAQUINA ,
SUM(CANTIDAD_OK) SUMA,
SUM(SUM(CANTIDAD_OK)) OVER (PARTITION BY YEAR(FECHA_IMPUT) ORDER BY MONTH(FECHA_IMPUT)) AS suma
FROM RTMAQUINA
WHERE COD_MAQUINA = 'LB_TRASVASE'
GROUP BY COD_MAQUINA,
MONTH(FECHA_IMPUT),
YEAR(FECHA_IMPUT)
ORDER BY YEAR(FECHA_IMPUT), MONTH(FECHA_IMPUT) ASC;
Note the nested SUM()s. This syntax looks awkward, but is correct when using window functions with aggregation functions. The inner SUM() is the aggregation. The outer SUM() is for the window function.
Also note the window clause. First, it needs to reference the same expressions used in the GROUP BY -- or aggregation functions. Second, I think you want to partition by year based on how your question is phrased.

Distinct count and group by in HIVE

I am very new to HIVE and have an issue with distinct count and GROUP BY.
I want to calculate maximum temperature from temperature_data table corresponding to those years which have at least 2 entries in the table-
I tried with below query but it is not working
select
SUBSTRING(full_date,7,4) as year,
MAX(temperature) as temperature
from temperature_data
where count(distinct(SUBSTRING(full_date,7,4))) >= 2
GROUP BY SUBSTRING(full_date,7,4);
I am getting an error-
FAILED: SemanticException [Error 10128]: Line 2:0 Not yet supported place for UDAF 'count'
Below is input-
year,zip,temperature
10-01-1990,123112,10
14-02-1991,283901,11
10-03-1990,381920,15
10-01-1991,302918,22
12-02-1990,384902,9
10-01-1991,123112,11
14-02-1990,283901,12
10-03-1991,381920,16
10-01-1990,302918,23
12-02-1991,384902,10
10-01-1993,123112,11
You should use HAVING keyword instead to set a condition on variable you're using for grouping.
Also, you can benefit of using subqueries. See below.
SELECT
year,
MAX(t1.temperature) as temperature
FROM
(select SUBSTRING(full_date,7,4) year, temperature from temperature_data) t1
GROUP BY
year
HAVING
count(t1.year) > 2;
#R.Gold, We can try to simplify the above query without using sub-query as below:
SELECT substring(full_date,7) as year, max(temperature)
FROM your-hive-table
GROUP BY substring(full_date,7)
HAVING COUNT(substring(full_date,7)) >= 2
And, fyi - we can't use aggregate functions with WHERE clause.

SQL select invalid because it is not contained in aggregate function

Here's the problem, I want to display the month, count and avg of one column in a table, but I keep getting an error when I try and group it by the month.
This is the code:
SELECT MONTH(ContractDate) AS Q,
DATENAME(month, ContractDate) AS M,
COUNT(ContractDate) AS C, SUM(ContractPrice) AS S
FROM dashboard
WHERE YEAR(ContractDate) = $year
AND ContractDate IS NOT NULL
AND ContractPrice IS NOT NULL
GROUP BY MONTH(ContractDate)
But this results in the error:
[Microsoft][SQL Server Native Client 10.0][SQL Server]
Column 'dashboard.ContractDate' is invalid in the select
list because it is not contained in either an aggregate
function or the GROUP BY clause.
But if I removed the MONTH() from the group by... it works fine.. But I need to have them grouped by month otherwise I get multiple of the same month not counted as one.
Sorry again, I did search and there is HEAPS of answers, but like I said I'm noob and they didn't really help me because I don't understand why this happens.
You have to have all columns that are not aggregates in the GROUP BY. Either add your DATENAME column into the GROUP BY or remove it from the query altogether.
GROUP BY MONTH(ContractDate) AS Q, DATENAME(month, ContractDate)
Try executing your query after removing DATENAME(month, ContractDate) AS M. I guess this is causing the issue. You are doing a GROUP BY MONTH(ContractDate) but also trying to use ContractDate which is not in the GROUP BY list.