I have this code with which I am trying not to aggregate on two columns HistoryQuantity and TOTFCST. I keep getting this error
Column 'SCPOMGR.HISTWIDE_CHAIN.HistoryQuantity' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
I don't think I should group by numeric columns.
Is there any way around this?
-- WAPE (chain)
SET ARITHABORT OFF
SET ANSI_WARNINGS OFF
SELECT
b.[LOC], b.[DMDUNIT],
ABS(b.HistoryQuantity - a.TOTFCST) AS 'Abs Error',
SUM(b.HistoryQuantity) AS 'Sum of Actuals',
SUM(ABS((b.HistoryQuantity - a.TOTFCST))) AS 'Sum of Abs Error',
SUM(ABS((b.HistoryQuantity - a.TOTFCST))) / SUM(b.HistoryQuantity) AS 'WAPE'
FROM
SCPOMGR.FCSTPERFSTATIC a
JOIN
SCPOMGR.HISTWIDE_CHAIN b ON a.[STARTDATE] = b.[DMDPostDate]
AND a.[DMDUNIT] = b.[DMDUNIT]
AND a.[LOC] = b.[LOC]
GROUP BY
b.[LOC], b.[DMDUNIT], b.[DMDPostDate]
ORDER BY
b.[LOC]
ABS() is not an aggregate function and cannot be used without, for example, passing aggregate values to it
use any like this
ABS(Sum(b.HistoryQuantity) - Sum(a.TOTFCST))
Related
I've the following subquery in an sql query:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, NOMBRE AS NOMBREUNIDAD FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY ID_PLAN, ID_CURSO, NEDICION) ASIS
Problem I have I believe lies in that both table ALUMNOS and UNIDADES have a column named 'NOMBRE' so if I attempt to execute the query I obtain:
00000 - "column ambiguously defined"
To avoid that I thought about changing NOMBRE AS NOMBREUNIDAD to:
UNIDADES.NOMBRE AS NOMBREUNIDAD
But if I do that I get a:
00000 - "not a GROUP BY expression"
So, I don't know what to do so that subquery executes properly.
What should I change to properly execute query without changing the column name?
Aliases are pretty useful, if you use them. The simplify queries and make them easier to read and maintain. I'd suggest you to do so, as it'll also help query to work because Oracle doesn't know which table you actually meant when you selected those 4 columns - which tables do they belong to?
This is just a guess as I don't know your tables so you'll have to fix it yourself. Also, I literally JOINed tables; try to avoid comma-separating them in FROM clause and doing join in WHERE clause as it is supposed to filter data.
GROUP BY, as already commented, is probably useless. If you wanted to fetch distinct set of values, then use appropriate keyword: distinct.
SELECT DISTINCT n.id_plan,
s.id_curso,
u.nedicion,
u.nombre
FROM asisten n
JOIN alumnos s ON n.cod = s.cod
JOIN unidades u
ON u.idestructura = s.idestructura
AND u.cdundorg = s.cdundorg
WHERE UPPER (TRANSLATE (u.nombre, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
I managed to solve my problem:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, UNIDADES.NOMBRE AS NOMBREUNIDAD
FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY UNIDADES.NOMBRE,ID_PLAN, ID_CURSO, NEDICION
)
I tried to write this in one of my SQL stored procedure. Just a simple logic to just check if the #kPortalOrigin exists in the table [cli].[tbl_S_PortalOrigin]:
IF (COUNT ((SELECT kPortalOrigin
FROM [cli].[tbl_S_PortalOrigin]
WHERE kPortalOrigin = #kPortalOrigin)) = 0)
RAISERROR('Portal Origin is invalid!', 16, 1)
However, I get this error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery
Any help? Thanks. And I would not mind an explanation why this error occurs.
if EXISTs ((select kPortalOrigin from [cli].[tbl_S_PortalOrigin] where kPortalOrigin = #kPortalOrigin)) RAISERROR('Portal Origin is invalid!',16,1)
this will works.
I am trying to create a view using some o group function like sum , avg etc. it is giving error.
create view sample_view as
select A,B,C,D,E,
(a + b+c+d+e)/5 as Mean_value,
GREATEST(a,b,c,d,e)-LEAST(a,b,c,d,e) as range_value,
avg(mean_value)
from samples;
I am getting below error.
Error starting at line : 37 in command -
create view sample_view as select A,B,C,D,E, (a + b+c+d+e)/5 as Mean_value, GREATEST(a,b,c,d,e)-LEAST(a,b,c,d,e) as range_value, avg(mean_value) from samples
Error report -
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
*Cause:
*Action:
Is there anyway to create view in oracle database using group function. I also tried sum() and count(). all those also giving error.
Root Cause for your problem is:
You tried to execute a SELECT statement that included a GROUP BY function (ie: AVG Function,MIN Function, MAX Function, SUM Function, COUNT Function), but was missing the GROUP BY clause.Try to add Group BY clause and run the query.
You can try the below - since avg() is an aggregate function you can not add this with other columns without adding group by the clause -
create view sample_view as
select A,B,C,D,E, (a + b+c+d+e)/5 as Mean_value,
GREATEST(a,b,c,d,e)-LEAST(a,b,c,d,e) as range_value,
avg((a + b+c+d+e)/5) over()
from samples;
I need to have an other value instead of FULL and NONE but I must do it in the select.
SELECT Calendar_Month,Series,Cnt
FROM T_Cust_Eom_n
where TYP='REB'
and Series='FULL'
or TYP='REB'
and Series='NONE'
order by Series desc,Calendar_Month
Can I do this with an as anyhow?
Here is what you want:
SELECT Calendar_Month,
CASE WHEN Series = 'FULL'
THEN 'YOUR_TEXT_FOR_FULL'
WHEN Series = 'NONE'
THEN 'YOUR_TEXT_FOR_NONE'
END
AS Series
,Cnt
FROM T_Cust_Eom_n
WHERE (TYP='REB'and Series='FULL') OR (TYP='REB' and Series='NONE') ORDER BY Series DESC,Calendar_Month
Problem:
You should use () when you have multiple conditions in WHERE clause.
Solution:
Improved your query with IN. You can use any other values inside the ():
SELECT Calendar_Month,Series,Cnt
FROM T_Cust_Eom_n
where TYP='REB'
and Series IN ('FULL','NONE', 'ANOTHER_VALUE')
order by Series desc,Calendar_Month
Does anyone know a solution for this error:
#1111 - Invalid use of group function
This is my SQL:
SELECT leerlingen.leerlingnummer, voornaam, tussenvoegsel, achternaam, klas, leerlingen.bestemming
FROM betalingen, leerlingen, bestemmingen
WHERE leerlingen.leerlingnummer = betalingen.leerlingnummer AND SUM( betalingen.bedrag ) > bestemmingen.bedrag
GROUP BY leerlingen.leerlingnummer
You can't reference the results of an aggregate function (SUM) in predicated query (WHERE), you will have to specify the aggregate in the select, then use a "Having" clause to filter that set.