SQL nested aggregate function as subquery syntax error - sql

I need to perform an average of averages. Figured out how to write the subquery, but the final function throws two errors. Syntax error on line 15 and then on line 1.
The subquery works. I then just need an average of the averages of the products in the same category. What's missing?
SELECT
c."name",
AVG(avgvalue)
FROM
(SELECT
c.name,
p.name,
AVG(a."value") AS avgvalue
FROM
answers a
INNER JOIN
survey_responses sr ON sr.id = a.survey_response_id
AND a.question_id = 13
INNER JOIN
answers category_answer ON category_answer.survey_response_id = sr.id
AND category_answer.question_id = 264
INNER JOIN
answers_categories ac ON category_answer.id = ac.answer_id
INNER JOIN
categories c ON c.id = ac.category_id
INNER JOIN
products p ON p.id = a.product_id
WHERE
c.name IN ('Accounting')
HAVING
count(p.name) > 10) AS ProductAverages
GROUP BY c.NAME

Remove ; after the HAVING clause in the temporary table
HAVING count(p.name)>10

Related

SQL statement with group by - list of passengers

with the following statement I am trying to select the list of passengers with their total weight of luggage. I keep receiving this error ORA-00979: not a GROUP BY expression [SQL State=42000, DB Errorcode=979]. If I add all the expressions in the select clause, I get rid of the error, however the statement lists passengers multiple times if they have more than just one item of luggage.
Any idea how to fix it? Thank you.
select f.flugnummer, p.nachname, pl.sitzplatznummer, l.bezeichnung, r.reisepassnr, sum(g.gewicht) as Luggage
from passagierliste pl join flug f on f.flugID = pl.flugID
join gepaeck g on pl.personID = g.personID
join person p on pl.personID = p.personID
join reisepass r on p.personID = r.personID
join land l on r.landID = l.landID
group by nachname;
Use below query,
select f.flugnummer, p.nachname, pl.sitzplatznummer, l.bezeichnung, r.reisepassnr,
sum(g.gewicht) as Luggage
from passagierliste pl join flug f on f.flugID = pl.flugID
join gepaeck g on pl.personID = g.personID
join person p on pl.personID = p.personID
join reisepass r on p.personID = r.personID
join land l on r.landID = l.landID
group by f.flugnummer, p.nachname, pl.sitzplatznummer, l.bezeichnung, r.reisepassnr;
Whenever you are aggregating, you have to use all the columns you select in group by

Trouble with aggregate in Where clause, Selecting Max(x) When Max(x) != 3

I am trying to reconfigure the below sql to only pull records when the Max(Field) != 3 but keep getting an error (detailed) below.
This is the code before adding the Where Max(field) != 3
SELECT P.Code,
MAX(PW.v1) AS V1
FROM SW
INNER JOIN S ON SW.S_Id = S.Id
INNER JOIN PW ON SW.PW_Id = PW.Id
INNER JOIN PON S.P_Id = P.id
WHERE S.P_Id = P.id
GROUP BY P.Code
My Attempt
SELECT P.Code,
MAX(PW.v1) AS V1
FROM SW
INNER JOIN S ON SW.S_Id = S.Id
INNER JOIN PW ON SW.PW_Id = PW.Id
INNER JOIN PON S.P_Id = P.id
WHERE S.P_Id = P.id
AND (SELECT MAX(PW.v1)
FROM SW AS SW2
WHERE SW.PWId = SW2.PW_Id) != 3
GROUP BY P.Code
This is the error I get and not sure what to do:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Traditional method of filtering on results of an aggregate can be achieved by using HAVING clause. I also removed the unnecessary WHERE clause as you already joined those 2 tables on that column. Here is the query:
SELECT P.Code
,MAX(PW.v1) AS V1
FROM SW
INNER JOIN S
ON SW.S_Id = S.Id
INNER JOIN PW
ON SW.PW_Id = PW.Id
INNER JOIN P
ON S.P_Id = P.id
GROUP BY P.Code
HAVING MAX(PW.v1)!=3;

getting the error in the below oracle query of inner join

I am executing the below query in oracle but I am getting an error on the execution of the below query , Please advise how to overcome from this specially the error come when i add the last clause of where condition in the query
SELECT t.product_name FROM JOBCODE_PROJECT_TYPE_MAPPING p
INNER JOIN AOBCODE_UCT_MAPPING h
ON p.ID = h.jobcode_id
INNER JOIN WISK_UCTS t
ON h.risk_product_id = t.risk_product_id AND p.id = h.jobcode_id;
where p.sp_job_code= 'Add';
Remove second duplicate condition AND p.id = h.jobcode_id; which is irrelevant in the join context:
SELECT t.product_name FROM JOBCODE_PROJECT_TYPE_MAPPING p
INNER JOIN AOBCODE_UCT_MAPPING h
ON p.ID = h.jobcode_id
INNER JOIN WISK_UCTS t
ON h.risk_product_id = t.risk_product_id
where p.sp_job_code= 'Add';

SQL using SUM and INNER JOIN issue

I get this error when I try to execute the query shown below
Column 'dbo.Stock_Purchase.Supplier_ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Query:
SELECT
dbo.Stock_Purchase.*, dbo.Stock_Purchase_Details.*,
dbo.Supplier.*,
SUM(Stock_Purchase_Details.Discount) AS totaldis
FROM
dbo.Stock_Purchase
INNER JOIN
dbo.Stock_Purchase_Details ON dbo.Stock_Purchase.Purchase_ID = dbo.Stock_Purchase_Details.Purchase_ID
INNER JOIN
dbo.Supplier ON dbo.Stock_Purchase.Supplier_ID = dbo.Supplier.Supplier_ID
GROUP BY
Stock_Purchase.Purchase_ID
You can only include the column in the GROUP BY as a "bare" column in the SELECT. So:
SELECT p.Purchase_ID, sum(pd.Discount) as totaldis
FROM dbo.Stock_Purchase p INNER JOIN
dbo.Stock_Purchase_Details pd
ON p.Purchase_ID = pd.Purchase_ID INNER JOIN
dbo.Supplier s
ON p.Supplier_ID = s.Supplier_ID
GROUP BY p.Purchase_ID ;
Also notice how table aliases make the query easier to read, write, and understand.
If you do want all the details, you can use window functions:
SELECT p.*, pd.*, s.*,
SUM(pd.Discount) OVER (PARTITION BY p.Purchase_ID) as totaldis
FROM dbo.Stock_Purchase p INNER JOIN
dbo.Stock_Purchase_Details pd
ON p.Purchase_ID = pd.Purchase_ID INNER JOIN
dbo.Supplier s
ON p.Supplier_ID = s.Supplier_ID;

sql query sum bringing back different results

I have the following two queries below, the Total is coming back different, but I am adding the sums in each of the query the same way. Why is the total coming back different?
select [Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
where co.Description = 'MyCounty'
Group By co.Description
select [Number Of DLL Children] = SUM(cd.NumberOfLanguageSpeakers),
[Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
where co.Description = 'MyCounty'
Group by co.Description
Just a quick glance over the two querties, I would presume that:
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
in the second query is excluding results that are in the first query, therefor the aggregated values will be different.
Your join to the Classrooms table is joining with an extra table in the 2nd query.
Query 1:
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
Query 2:
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
...
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
My bet is that the ClassroomDLL table has less data in it, or has rows with a null for one of the join criteria columns, either of which could exclude rows from the results and throw your aggregate totals off.