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

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

Related

Using sum function in sql view in oracle database

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;

Hive Expression not in GROUP BY key 'pat_dtl_start_dt' (state=42000,code=10025)

SQL Code:
CREATE TABLE dev.new STORED AS orc tblproperties("orc.compress" = "SNAPPY") AS
SELECT pat_dtl_start_dt, individual_id, bdy_lctn_cd, prcdr_cd, trtmnt_cd, SUM(allw_amt)
FROM dev.old
GROUP BY pat_dtl_start_dt, individual_id, bdy_lctn_cd, prcdr_cd, trtmnt_cd;
Error Message:
Error: Error while compiling statement: FAILED: SemanticException [Error 10025]: Line 2:7 Expression not in GROUP BY key 'pat_dtl_start_dt' (state=42000,code=10025)
I think I already include all selected columns in the group by key. Don't know why this still happen.
Your GROUP BY looks correct . . . enough. I don't see why it is causing that error.
However, you probably don't want allw_amt in it. I am guessing that this does what you want:
SELECT pat_dtl_start_dt, individual_id, bdy_lctn_cd, prcdr_cd, trtmnt_cd,
SUM(allw_amt)
FROM dev.old
GROUP BY pat_dtl_start_dt, individual_id, bdy_lctn_cd, prcdr_cd, trtmnt_cd;

Syntax error in query expression using sub query

SELECT
(
SELECT Sum(tbl_allTransactions.transAmount) AS SumOftransAmount
FROM tbl_allTransactions
WHERE (((tbl_allTransactions.[transType])='Expense')) OR (((tbl_allTransactions.[transType])='Budget') AND ((tbl_allTransactions.[transMonth])=[#transMonth]))
GROUP BY tbl_allTransactions.transMonth
,
SELECT Sum(tbl_allTransactions.transAmount) AS SumOftransAmount
FROM tbl_allTransactions
WHERE (((tbl_allTransactions.[transType])='Expense')) OR (((tbl_allTransactions.[transType])='Budget') AND ((tbl_allTransactions.[transMonth])=[#transMonth]))
GROUP BY tbl_allTransactions.transMonth
)
FROM tbl_allTransactions WHERE (((tbl_allTransactions.[userID])=[#userID]))
I am getting following error:-
System.Data.OleDb.OleDbException (0x80040E14) Syntax error in query
expression '(SELECT Sum(tbl_allTransactions.transAmount) AS
SumOftransAmount FROM tbl_allTransactions WHERE
(((tbl_allTransactions.[transType])='Expense')) OR
(((tbl_allTransactions.[transType])='Budget') AND
((tbl_allTransactions.[transMonth])=[#transMonth])) GROUP '. at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResulthr) at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS
dbParams, Object& executeResult) at
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Obje
The original code was missing a closing parenthesis on the first subquery. The code was also simplified by consolidating the transType conditional from 2 statements to 1. Extra brackets were removed to enhance readability. Also column aliases were added to output of each subquery. Since both queries render the same data, the second column was called "SumOftransAmount2"
SELECT
(SELECT Sum(tbl_allTransactions.transAmount)
FROM tbl_allTransactions
WHERE tbl_allTransactions.transType in ('Expense', 'Budget') AND tbl_allTransactions.transMonth = #transMonth
GROUP BY tbl_allTransactions.transMonth) as SumOftransAmount
,
(SELECT Sum(tbl_allTransactions.transAmount)
FROM tbl_allTransactions
WHERE tbl_allTransactions.transType in ('Expense', 'Budget') AND tbl_allTransactions.transMonth = #transMonth
GROUP BY tbl_allTransactions.transMonth) as SumOftransAmount2
FROM tbl_allTransactions
WHERE tbl_allTransactions.userID=#userID

Pivot query causing "incorrect syntax" error in the PIVOT clause

I am getting the error, "incorrect syntax near 'Basic'" when I try to execute the following query:
WITH BaseQuery AS (
SELECT Region,Essbase_Channel,Product,COUNT(New_reconnects)
FROM NDW.dbo.SOS_Detail SOS
WHERE SOS.EntityID IN ('000310','000700','000815','000854')
AND Division ='NORTHEAST DIVISION' AND Month_Name ='MAR'
GROUP BY Month_Name,Product,Region,Essbase_Channel,EntityID,Division
)
SELECT * FROM BaseQuery
PIVOT (COUNT(New_reconnects) FOR Product IN ('BASIC','HSI','CDV','H1','X1')) AS PVT
ORDER BY Product,Region,Essbase_Channel
My goal would be to have the first column based on Essbase_Channel, the pivot columns to be Product values: BASIC, HSI, CDV, H1 and the values in the pivot to be COUNT(New_reconnects).
How should I change the syntax of the query to avoid this error?
Change to:
FOR Product IN ([BASIC],[HSI],[CDV],[H1],[X1])

#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.