Oracle sql missing expression - group-by

I have an Oracle SQL query and running the query, it gives ORA-00936: missing expression. When I hover over the red in Oracle Sql Developer, it says "Syntax Error. Partially Recognized Rules, railroad diagrams. I think there's something wrong with my Group By. I think Group by needs to have all query columns in it, but I know the last 3 are min/max/avg, so I don't think it makes sense to add those to the group by separately. What is the proper way to add them to the group by?
select
do.dcode,
ds.SERIALNO,
ds.BASECOMPONENTCODE,
TO_CHAR (strt.DLOCALECRTDT,'MON') as MON,--this looks like 13-OCT-15 05.19.03.000000000 PM
Max (do.METRICVALUE) as MaxCount,
min (do.METRICVALUE) as MinCount,
avg (do.METRICVALUE) as AvgCount
FROM
TECH_DWH.D_DIM_OUTPUTCOUNT_TBL do
join (
Select d1.dcode,d1.organizationid
from K_D_VW d1
where
d1.isactive='Y'
and d1.organizationid = 7500 -- company id
) d on d.dcode=do.dcode
left join
TECH_DWH.D_COMPSTAT_SERIAL_NO_MAP_TBL csm on csm.DCOMPONENTSTATEID = do.DCOMPONENTSTATEID
join TECH_D.D_DIM_SERIAL_NO_TBL ds on ds.serialnoid = csm.serialnoid
left join TECH_DWH.d_dim_medianumber_tbl dm on dm.DCOMPONENTSTATEID = csm.DCOMPONENTSTATEID
left join TECH_DWH.D_DEVICE_COMPSTATE_STRT_TBL strt on strt.DCOMPONENTSTATEID = csm.DCOMPONENTSTATEID
WHERE
instr(upper(ds.basecomponentcode),'PRINT')>0 AND --- return only device components
LENGTH(TRIM(TRANSLATE((do.METRICVALUE), ' +-.0123456789',' '))) is null -- test for only rows with numberic metrivalue's
AND do.dcode like '0046'
AND strt.COMPONENTSTATECODE like '%EP_DEVICE%'
and strt.DLOCALECRTDT >= to_date ( '30-12-2021', 'DD-MM-YYYY' )
and
Group by --red squiggly at "by", but error line number is following line
do.dcode,
ds.SERIALNO,
ds.BASECOMPONENTCODE,
TO_CHAR(strt.DLOCALECRTDT,'MON'),
do.METRICVALUE;

What's obvious, is
and
Group by --red squiggly at "by", but error line number is following line
do.dcode,
What's that AND doing alone? Remove it.

Related

How do I fix the error "select list expression [...] references which is neither grouped nor aggregated" in BigQuery?

I get the error "select list expression Opportunity.id references which is neither grouped nor aggregated" when running the following query in BigQuery:
SELECT
Opportunity.id AS `Opportunity_Id`,
Opportunity.testing_only__c AS `Opportunity_Testing_only`,
MAX(DatedConversionRate.startdate) AS `DatedConversionRate_Start_date`,
FROM
`dataset.Opportunity` Opportunity
LEFT JOIN
`dataset.DatedConversionRate` DatedConversionRate 
ON DatedConversionRate.isocode = Opportunity.currencyisocode
WHERE
DatedConversionRate.startdate < CURRENT_TIMESTAMP()
What is the problem and how can I fix it?
Remove Opportunity.id from select or group by it:
SELECT
Opportunity.id AS `Opportunity_Id`,
MAX(DatedConversionRate.startdate) AS `DatedConversionRate_Start_date`,
FROM
`dataset.Opportunity` Opportunity
LEFT JOIN
`dataset.DatedConversionRate` DatedConversionRate
ON DatedConversionRate.isocode = Opportunity.currencyisocode
WHERE
DatedConversionRate.startdate < CURRENT_TIMESTAMP()
GROUP BY Opportunity.id

column reference id is ambiguous postgres

I'm writing a new query postgresql with alias but i still have the same problem of ambiguous column.
select a.id_application
from (SELECT * FROM t_mission as PM
LEFT JOIN t_mission_raf rm on PM.id_mission= rm.id_rm
LEFT JOIN t_mission_roles AS mr ON PM.id_mission = mr.id_mission
LEFT JOIN t_role AS r ON r.id_role = mr.id_role
LEFT JOIN t_appli AS app ON app.id_application = r.id_application
WHERE PM.type_mission = 1 AND PM.id_mission =24730) as a
Result:
Error msg : SQL Error [42702]: ERROR: column reference "id_application" is ambiguous
Position : 8
Don't SELECT * but exlipcitly select the columns you need. Make sure to only select app.id_application or r.id_application. Or use as for one of those.

ERROR: syntax error at or near "SETS"

I'm getting a syntax error when trying to use the GROUPING SETS function in my Postgres DB.
I've looked at the documentation here and I believe the syntax is correct, but I'm still getting an error.
I believe the syntax should be GROUP BY GROUPING SETS ((COLUMN), (COLUMN), ()) to get the grand total of my SUM() from the the SELECT part of my code. So I put in GROUP BY GROUPING SETS ((amount.date), ( places.place), ());
What am I doing wrong here?
SELECT
EXTRACT(MONTH FROM amount.date),
places.place,
CASE
WHEN places.place = 'A' THEN SUM(amount.amount)
WHEN places.place = 'B' THEN SUM(amount.amount)
WHEN places.place = 'C' THEN SUM(amount.amount)
ELSE SUM((amount.amount) * 2.5)
END AS "Total"
FROM amount
LEFT JOIN places ON places.id = amount.id
WHERE EXTRACT(YEAR FROM amount.date) = 2017
GROUP BY GROUPING SETS ((amount.date), (places.place), ());

Symfony DQL Postgresql group by isnt working

I have just switched from mysql to postgresql and my working dql queries stopped working.
Here is my dql
SELECT p
FROM AppBundle:Photo p
JOIN AppBundle:Like l WITH p = l.photo
WHERE p.isModerated = :isModerated
AND p.isActive = :isActive
AND p.category IN(:categories)
AND p.creationDate <= :begin
AND p.creationDate >= :end
GROUP BY p.id
ORDER BY l.creationDate DESC
Getting the next error
SQLSTATE[42803]: Grouping error: 7 ERROR: column "l1_.creation_date" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... p0_.creation_date >= $4 GROUP BY p0_.id ORDER BY l1_.creati...
As i can understand, it says that group by column should be in SELECT. I dont it to be there. I need to select just p (AppBundle:Photo) and nothing more. What should be edited in dql to get it working properly?
Thank you.
I just replaced ORDER BY l.creationDate DESC to ORDER BY p.creationDate DESC
It will work for me. As i understood, ORDER BY column should be SELECTed

ERROR: missing FROM-clause entry for table "movies"

I am new to SQL and need to query a database to extract certain information before I can import it into another software I am familiar with to analyse the data. This query was sent to me by a friend who I don't have access to at the moment, and I cannot figure out why it gives me the following error:
ERROR: missing FROM-clause entry for table "movies"
LINE 8: FROM (SELECT movies.movieid
Here is the query:
SELECT innerselect.movieid
,innerselect.title
,innerselect.year
,innerselect.imdbid
,innerselect.budget[1] AS budget_currency
,TO_NUMBER(innerselect.budget[2], '999999999999990.00') AS budget_total
,innerselect.businesstext
FROM (SELECT movies.movieid
,movies.title
,movies.year
,movies.imdbid
,business.businesstext
,regexp_matches(business.businesstext, '^BT:[ ](USD)[ ](-?(?!0)(?:\d+|\d{1,3}(?:,\d{3})+))', 'g') AS budget -- creates a PostgreSQL Array which contains the content matched with the RegEx Groups FROM movies LEFT JOIN business ON movies.movieid=business.movieid WHERE movies.movieid > 2753500
) AS innerselect
Any help would be greatly appreciated.
Problem is you put the FROM on the same line as the comment, so the FROM clause was ignored.
SELECT innerselect.movieid
,innerselect.title
,innerselect.year
,innerselect.imdbid
,innerselect.budget[1] AS budget_currency
,TO_NUMBER(innerselect.budget[2], '999999999999990.00') AS budget_total
,innerselect.businesstext
FROM (SELECT movies.movieid
,movies.title
,movies.year
,movies.imdbid
,business.businesstext
,regexp_matches(business.businesstext, '^BT:[ ](USD)[ ](-?(?!0)(?:\d+|\d{1,3}(?:,\d{3})+))', 'g') AS budget -- creates a PostgreSQL Array which contains the content matched with the RegEx Groups
FROM movies LEFT JOIN business ON movies.movieid=business.movieid WHERE movies.movieid > 2753500
) AS innerselect