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;
Related
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
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",
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.
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
I am trying to search the data in two tables by getting the last four numbers of a SSN as input from the user.
I have a JDBC query that is as follows:
`String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE"
+"FROM" +" G.M_NAME_A N, G.M_ID_A M"
+"WHERE" + "N.NUMBER = M.NUMBER"
+"AND" + "M.SSN like 'l4ssn' ";'
It fails with either "FROM Keyword not found where expected" or "Invalid Column index".
Please help me format the query.
yes, it should fail. Check your SQL, you're missing a space in front of "FROM", and ditto most anywhere you're concatenating strings.
1、Change the SQL as follows. Space is added around FROM, WHERE and AND,If you follow what you write sql, sql stitching is so out:
String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATEFROM G.M_NAME_A N, G.M_ID_A MWHEREN.NUMBER = M.NUMBERAND M.SSN like 'l4ssn' ";
String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE"
+" FROM G.M_NAME_A N, G.M_ID_A M"
+" WHERE N.NUMBER = M.NUMBER"
-- modify +" AND" + "M.SSN like 'l4ssn' ";'
+" AND M.SSN like '%l4ssn%' ";
Change the SQL as follows. Space is added around FROM, WHERE and AND
String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE"
+" FROM " +" G.M_NAME_A N, G.M_ID_A M"
+" WHERE " + "N.NUMBER = M.NUMBER"
+" AND " + "M.SSN like 'l4ssn' ";
Your query will look for M.SSN which is 14ssn it's just like saying M.SSN = '14ssn'. If you want to use query something similar to 14ssn use M.SSN like '%14ssn%' instead
Just on side note, use prepared statement instead of using the query parameter in your sql string.
http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
Try this query
String sql=" SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE "+
" FROM G.M_NAME_A N,G.M_ID_A M "+
" WHERE N.NUMBER = M.NUMBER "+
" AND M.SSN like 'l4ssn' ";
The question is resolved using the following query and PreparedStatement:
String l4ssn=("%"+l4ssn);
String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE"
+" FROM " +" G.M_NAME_A N, G.M_ID_A M"
+" WHERE " + "N.NUMBER = M.NUMBER"
+" AND " + "M.SSN like ?";
PreparedStatement pstmt = new PreparedStatement(sql);
pstmt.setString(1,l4ssn);