Symfony DQL Postgresql group by isnt working - postgresql

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

Related

Oracle sql missing expression

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.

Sybase SQL - Select Most recent record and line up with the values from previous record

I am trying the following query on data table listed below....Sybase does not allow row_number() function. Any suggestions would be very helpful:
select
a.item_number,
a.item_rate,
a.item_code,
a.effective_dt,
r_prev.effective_dt,
r_prev.item_rate,
r.item_code
from A a LEFT OUTER join
(SELECT item_number, item_rate, item_code,effective_dt
FROM A
) a_prev
ON a.item_number = a_prev.item_number
AND a.rating_eff_dt < a_prev.rating_eff_dt
order BY a.item_number, r.item_rate, r.item_code, a.effective_dt desc, a_prev.rating_eff_dt
You can use
Select Number(*),<column list>
from table
Thank You.

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

PostgreSql Group By and aggreate function error

My problem is, when I run the following query in MySQL, it looks like this
Query;
SELECT
CONCAT(b.tarih, '#', CONCAT(b.enlem, ',', b.boylam), '#', b.aldigi_yol) AS IlkMesaiEnlemBoylamImei,
CONCAT(tson.max_tarih, '#', CONCAT(tson.max_enlem, ',', tson.max_boylam), '#', tson.max_aldigi_yol) AS SonMesaiEnlemBoylamImei,
Max(CAST(b.hiz AS UNSIGNED)) As EnYuksekHiz,
TIME_FORMAT(Sec_TO_TIME(TIMESTAMPDIFF(SECOND, (b.tarih), (tson.max_tarih))), '%H:%i') AS DurmaSuresi
FROM
(Select id as max_id, tarih as max_tarih, enlem as max_enlem, boylam as max_boylam, aldigi_yol as max_aldigi_yol from _213gl2015016424 where id in(
SELECT MAX(id)
FROM _213gl2015016424 where (tarih between DATE('2016-11-30 05:45:00') AND Date('2017-01-13 14:19:06')) AND CAST(hiz AS UNSIGNED) > 0
GROUP BY DATE(tarih))
) tson
LEFT JOIN _213gl2015016424 a ON a.id = tson.max_id
LEFT JOIN _213gl2015016424 b ON DATE(b.tarih) = DATE(a.tarih)
WHERE b.tarih is not null And (b.tarih between DATE('2016-11-30 05:45:00') AND Date('2017-01-13 14:19:06')) AND b.hiz > 0
GROUP BY tson.max_tarih
Output is order by date;
Result query
When I try to run a query in PostgreSQL, I get group by mistake.
Query;
SELECT
CONCAT(b.tarih, '#', CONCAT(b.enlem, ',', b.boylam), '#', b.toplamyol) AS IlkMesaiEnlemBoylamImei,
CONCAT(tson.max_tarih, '#', CONCAT(tson.max_enlem, ',', tson.max_boylam), '#', tson.max_toplamyol) AS SonMesaiEnlemBoylamImei,
Max(CAST(b.hiz AS OID)) As EnYuksekHiz,
to_char(to_timestamp((extract(epoch from (tson.max_tarih)) - extract(epoch from (b.tarih)))) - interval '2 hour','HH24:MI') AS DurmaSuresi
FROM
(Select id as max_id, tarih as max_tarih, enlem as max_enlem, boylam as max_boylam, toplamyol as max_toplamyol from _213GL2016008691 where id in(
SELECT MAX(id)
FROM _213GL2016008691 where (tarih between DATE('2018-02-01 03:31:54') AND DATE('2018-03-01 03:31:54')) AND CAST(hiz AS OID) > 0
GROUP BY DATE(tarih))
) tson
LEFT JOIN _213GL2016008691 a ON a.id = tson.max_id
LEFT JOIN _213GL2016008691 b ON DATE(b.tarih) = DATE(a.tarih)
WHERE b.tarih is not null And (b.tarih between DATE('2018-02-12 03:31:54') AND DATE('2018-02-13 03:31:54')) AND b.hiz > 0
GROUP BY tson.max_tarih
Group by error is : To use the aggregate function, you must add the column "b.tarih" to the GROUP BY list.
When I add it I get the same error for another column.I'm waiting for your help.
You are using a feature of MySQL that is not standard SQL and you can also deactivate.
You are grouping by tson.max_tarih in your query. That means that for all rows that share the same value in that field, you will get only one row as a result of that group.
If you have several different values in the rest of the fields (enlem, boylam, etc...) which one are you trying to get in as the result of the query? That's the question that PostgreSQL is asking you.
MySQL is just returning any value for those fields among the rows in the group. PostgreSQL requires you to actually specify it.
Two typical solutions would be grouping by the rest of the fields (b.tarih, b.enlem) or specifying the value those fields to something like MAX(b.tarih), etc.

ALL query in Ecto

I need to translate the following SQL to Ecto Query DSL.
SELECT otc.*
FROM users u
INNER JOIN one_time_codes otc ON u.id = otc.user_id
LEFT OUTER JOIN one_time_code_invalidations otci ON otci.one_time_code_id = otc.id
WHERE u.id = 3 AND
otc.code = 482693 AND
otci.inserted_at IS NULL AND
otc.inserted_at > all(SELECT otc.inserted_at
FROM one_time_codes otc2
WHERE otc2.user_id = 3) AND
otc.inserted_at > (now() - '180 seconds'::interval);
In the third AND statement of WHERE clause, notice there is an ALL query. Ecto seems to not have corresponding function in Ecto.Query.API.
How to apply such aggregate? What is the correct way to implement this, though it could be implemented by having a lookup on debug logs of the Ecto, do you have another idea (or suggestion)?
Thank you.
Ecto does not allow subqueries in expressions, and you're right that there's no all in Ecto's Query API, but you can use fragment like this:
where: ...
and otc.inserted_at > fragment("all(SELECT otc.inserted_at FROM one_time_codes otc2 WHERE otc2.user_id = ?", 3)
and ...