Cannot figgure out PSQL where issue in complicated clause - postgresql

I have a native query
"SELECT *, point(?2, ?1) <#> point(lng,lat) as distance FROM workers " +
" LEFT JOIN availabilities on availabilities.worker_id = workers.id " +
" WHERE workers.category_id = ?3 " +
" WHERE worker_availability.time = ?4 ORDER BY distance ASC",
nativeQuery = true
)
The error I keep getting is
org.postgresql.util.PSQLException: ERROR: syntax error at or near "WHERE"

You wrote "WHERE" 2 times. You must change this:
" WHERE worker_availability.time = ?4 ORDER BY distance ASC",
to this:
" AND worker_availability.time = ?4 ORDER BY distance ASC",

Related

JPQL: How to rewrite postgres native query to JPQL query that uses filter keyword

Im trying to avoid using native query. I have this query that uses the filter function, how could I rewrite this to not use that and work in regular jpql?
#Query(
"SELECT time_bucket(make_interval(:intervalType), d.time) as groupedDate, " +
"CAST(d.team_Id as varchar) as teamId, CAST(d.service_Id as varchar) as serviceId, CAST(d.work_id as varchar) as workId, " +
"ROUND(CAST(count(d.value) filter ( where d.type = 'A') AS numeric) /" +
" (CAST(count(d.value) filter ( where d.type = 'B') AS numeric)), 4) as total " +
"FROM datapoint d " +
"WHERE d.team_Id = :teamId and d.service_id in :serviceIds and d.work_id = :workspaceId and d.type in ('A', 'B') " +
"AND d.time > :startDate " +
"GROUP BY groupedDate, d.team_Id, d.service_Id, d.workspace_Id " +
"ORDER BY groupedDate DESC",
nativeQuery = true
)
in the FROM statement you have to use the DAO object instead of the table name

unexpected token: :(colon) in Hibernate

I'm trying to do the following code, but i get the unexpected token: : near line 1 error which refer to ord.date_out::date. Here is my code
#Query(value="select new com.ameerarestapi.wrapper.report.SummaryPeriodicSales(sto.name, sum(odi.subtotal_price), sum(odi.qty), ((sum(odi.subtotal_price))/(sum(odi.qty))), ord.date_out::date) "
+ "from OrderDetailItem odi "
+ "left join odi.order as ord "
+ "left join ord.store as sto "
+ "where ord.store.principle = :principle and ord.orderStatus IN :orderstatus and ord.dateOut between :date1 and :date2 and ord.voidStatus = :voidStatus "
+ "group by sto.name, ord.date_out::date ")
List<SummaryPeriodicSales> getReportDaily(#Param("principle") Principle principle,#Param("orderstatus") List<OrderStatus> orderstatus,#Param("date1") Date date1,#Param("date2") Date date2,#Param("voidStatus") byte voidStatus);
I'm using postgre database
Use the standard CAST() operator instead:
CAST(ord.date_out AS date)

An identification variable must be provided for a range variable declaration

I'm trying to use this query in my jpa but it doesn't work:
List<Object[]> query = em.createQuery("SELECT Tstat.idStatistiques, TL.codeLieu, TL.materiel, TL.zone, sum(Tstat.colis) as colis, Tstat.defaut, sum(Tstat.nbreDefaut) as nbreDefaut,"
+ " sum(Tstat.nonLu) as nonLu, sum(Tstat.multiple) as multiple, sum(Tstat.nonRecu) as nonRecu, sum(Tstat.incoherent) as incoherent, sum(Tstat.requete) as requete , "
+ "sum(Tstat.tempsFonctionnement) as tempsFonctionnement, SUM(Tstat.tempsUtilisation) as tempsUtilisation, Tstat.modeFonctionnement FROM "
+ "( SELECT CURRENT_DATE as horodatage, St.idStatistiques, St.colis, St.defaut, St.nbreDefaut, St.nonLu, St.requete, St.multiple, St.nonRecu, St.incoherent, St.tempsFonctionnement, St.tempsUtilisation, St.modeFonctionnement FROM Statistique St )"
+ " UNION "
+ "(SELECT h.horodatage, h.idStatistiques, h.colis, h.defaut, h.nbreDefaut, h.nonLu, h.nonRecu, h.requete, h.multiple, h.incoherent, h.tempsFonctionnement, h.tempsUtilisation, h.modeFonctionnement FROM Statistiqueshisto h )"
+ " Tstat "
+ "LEFT JOIN (SELECT * FROM Lieux) as TL on Tstat.idStatistiques = TL.code_VI WHERE idStatistiques like :A ").setParameter("A", 0040+"%").getResultList();
This gives me error
The expression is invalid, which means it does not follow the JPQL
grammar.

ADO.NET working with SQL and database

I'm getting an exception error saying missing operators can anyone help
string sql = "Select SalesPerson.Name, Item.Description, Orders.Quantity, Orders.OrderDate"
+ "From([Orders]"
+ "Inner Join[SalesPerson] On Orders.SalesPersonID=SalesPerson.SalesPersonID)"
+ "Inner Join[Item] On Orders.ItemNumber=Item.ItemNumber"
+ "Where Orders.CustomerID=#customer Order by Orders.OrderDate DESC";
You need to add some spaces at the end of each of your lines of SQL!
string sql = "SELECT SalesPerson.Name, Item.Description, Orders.Quantity, Orders.OrderDate "
+ "FROM [Orders] "
+ "INNER JOIN [SalesPerson] ON Orders.SalesPersonID = SalesPerson.SalesPersonID "
+ "INNER JOIN [Item] ON Orders.ItemNumber = Item.ItemNumber "
+ "WHERE Orders.CustomerID = #customer "
+ "ORDER BY Orders.OrderDate DESC";
Otherwise, your SQL ends up being
Select ..... Orders.OrderDateFROM([Orders]Inner Join[SalesPerson] .....
and so on - and that's just not valid SQL.
I also removed some unnecessary parenthesis around the JOIN operators - those are only needed for MS Access, but since you're saying you're using ADO.NET, I assume this is not for MS Access and therefore, those parenthesis are not needed

Syntax error in Postgresql but not mySQL

I'm executing this query
String innerQueryWithProductVersion = "select test_suite_name, max(m.date) as date "
+"from master_table_test_runs m "
+"INNER JOIN processed_jenkins_runs pdup "
+"ON m.id=pdup.test_run_id where m.date < "
+"(select max(date) from master_table_test_runs "
+"where product_version =:productVersion) "
+"and m.test_type!='CLOVER' and m.product = :product "
+"and m.test_suite_name in "+missingSuites
+" and m.branch like "+filters.branch
+" and m.deployment_mode like "+filters.deploymentMode
+" and pdup.jenkins_server like "+filters.jenkinsInstance
+" group by m.test_suite_name";
String queryWithProductVersion = "select t.number_tests, "
+"t.number_failure, t.number_skip, t.number_errors, t.test_type, "
+"t.product_version, t.date, t.test_suite_name, "
+"t.branch, p.job_url "
+"from master_table_test_runs t INNER JOIN "
+"(" +innerQueryWithProductVersion+") as x "
+"INNER JOIN processed_jenkins_runs p ON t.id=p.test_run_id "
+"where t.test_suite_name = x.test_suite_name "
+"and t.date = x.date and t.test_suite_name "
+"in "+missingSuites+" and product = :product "
+"and p.jenkins_server like "+filters.jenkinsInstance
+" and t.branch like "+filters.branch
+" and t.deployment_mode like "+filters.deploymentMode+"";
This query its working fine in mysql, but in PostgreSQL its giving syntax errors at "where" and "and"
syntax error at or near 'and'
Can anyone help me figure out the problem?
one of "+missingSuites, "+filters.branch etc is not defined, look at sample:
t=# select true where 'a' like '' and true;
bool
------
(0 rows)
t=# select true where 'a' like /*missed value*/ and true;
ERROR: syntax error at or near "and"
LINE 1: select true where 'a' like /*missed value*/ and true;