Using sum function in sql view in oracle database - sql

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;

Related

Not able to use pivot function in oracle sql develope

this is the table which I need to pivot(age range must be an attribute and percentage as its row)
[1]: https://i.stack.imgur.com/TyQpV.jpg
already coded:
SELECT SECOND_RESPONSE, 25-30, 30-35, 18-25, 45-50, 40-45, 35-40, 55-60 FROM
(SELECT SECOND_RESPONSE FROM SEC_ANALYSIS_AGE)
PIVOT
(
MAX(PERCENTAGE) FOR FIRST_RESPONSE IN (25-30, 30-35, 18-25, 45-50, 40-45, 35-40, 55-60)
) AS PIV;
Output
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Error at Line: 24 Column: 3
I would recommend an include FIRST_RESPONSE, PERCENTAGE columns select statement. Would be this one:
SELECT * FROM
(SELECT SECOND_RESPONSE, FIRST_RESPONSE, PERCENTAGE FROM SEC_ANALYSIS_AGE)
PIVOT
(
MAX(PERCENTAGE) FOR FIRST_RESPONSE IN ('25-30' as "25-30", '30-35' as "30-35", '18-25' as "18-25", '45-50' as "45-50", '40-45' as "40-45", '35-40' as "35-40", '55-60' as "55-60")
)
thanks

Error : Cannot perform an aggregate function on an expression containing an aggregate or a subquery

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.

Why does i say my function doesnt exist even thought the function was created?

I have to create a Function to Calculate invoiceID,InvoiceTotal - PaymentTotal - CreditTotal. So I created a function named fnNetOwed.I have to run it in this syntax:
Select invoiceId, dbo.fnNetOwed(invoiceid) from invoices
Where dbo.fnNetOwed (invoiceid) >0
And get the same results as if I did
Select invoiceID,InvoiceTotal - PaymentTotal - CreditTotal From Invoices
Where InvoiceTotal - PaymentTotal - CreditTotal>0
This is my code for creating it:
CREATE FUNCTION fnNetOwed
(#GetInvoiceId Money)
RETURNS Int
BEGIN
RETURN (SELECT (InvoiceTotal - PaymentTotal - CreditTotal) AS 'OWED'
FROM Invoices
WHERE #GetInvoiceID = Invoices.InvoiceID);
END
GO
This is what i am using to call it/execute:
Select InvoiceId, fnNetOwed(98) from Invoices
Where fnNetOwed(98) >0;
This is the ERROR I get when i run it:
Msg 195, Level 15, State 10, Line 1
'fnNetOwed' is not a recognized built-in function name.
Try to call it using your schema name : SELECT schema.fnNetOwed(98)
SQL Server scalar user defined functions must be called using 2 part names. You will see this error if you attempt to call the functions using a 1 part name. Can you check if that is the problem ?
So if your function name is "function1" and is defined in the dbo schema, then instead of
"select function1()" you should call it as "select dbo.function1()"

ERROR at line 1: ORA-00937: not a single-group group function

On this I am getting an error
ERROR at line 1:
ORA-00937: not a single-group group function.
Can someone help me on this I am not familiar on sql.
select cerps_accnt_code, cost_centre, SUM(TOTAL_AMOUNT_DUE)
from globe_billing_report where tag like '%Duncan%'
Use Group by while using aggregate functions in sql
select cerps_accnt_code, cost_centre, SUM(TOTAL_AMOUNT_DUE)
from globe_billing_report
where tag like '%Duncan%'
group by cerps_accnt_code, cost_centre
http://www.w3schools.com/sql/sql_functions.asp
Try to add group by clause as below
select cerps_accnt_code, cost_centre, SUM(TOTAL_AMOUNT_DUE)
from globe_billing_report
where tag like '%Duncan%'
group by cerps_accnt_code, cost_centre

#1111 - Invalid use of group function error in sql

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.