Crystal Report - Query in formula - crystal-reports

I have formula,
stringvar q1;
stringvar query;
q1 := "SELECT SHOW_CERT FROM C_SPEC_DELTA_REPORT_VIEW X WHERE X.VERSION = (select CONSTANT_VALUE from LIMS_CONSTANTS where name ='C_PRODUCT_VERSION1_DELTA') AND X.GRADE = ' " + {Command.GRADE} + " ' AND X.SAMPLING_POINT = ' " + {Command.SAMPLING_POINT} + " ' AND X.COMPONENT = ' " + {Command.COMPONENT} + " ')"
query := q1;
SQL( query )
Here, in the {command.COMPONENT} field, it the value in more than 30 Characters, my report execution is failing.
Is there is any length constraint? If Yes, how to extend it.
The length of the COMPONENT field is 160 characters in the DB.
Need your help!!!

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

JpaSystemException: No Dialect mapping for JDBC type: 1111

I have viewed some of the related question but the solutions on them are bit older. Most of them are using EntityManager. I have written the following native query in JpaRepository and when I get geojson as a String I'm getting the error mentioned in the title.
Here is my query
#Query(value = "SELECT\n"
+ " json_build_object(\n"
+ " 'type', 'FeatureCollection',\n"
+ " 'features', json_agg(\n"
+ " json_build_object(\n"
+ " 'type', 'Feature',\n"
+ " 'geometry', ST_AsGeoJSON(a.check_in_geom)\\:\\:json,\n"
+ " 'properties', json_build_object(\n"
+ " 'username', a.username,\n"
+ " 'users', (\n"
+ " -- Generate json array of \"users\"\n"
+ " SELECT array_to_json(array_agg(u.*)) \n"
+ " FROM users u \n"
+ " WHERE u.username = a.username\n"
+ " GROUP BY u.username\n"
+ " )\n"
+ " )\n"
+ " )\n"
+ " )\n"
+ " ) Json\n"
+ "FROM\n"
+ " attendances a, users u\n"
+ " WHERE a.username = u.username AND u.designation = 'Manager' AND date_trunc('day', a.created_at)\\:\\:DATE = '2022-04-04'"
+ " ;", nativeQuery = true)
String getAttendanceGeoJsonByDesignationAndDate(String designation, String dateStr);
For now I'm passing designation and date as static values.
When I run this query on Postgresql it runs successfully and return the expected geojson. But spring boot does not allow the result to be as String or it throws exception while running the query.
Please let me know how can I fix it. Also if I need to provide more detail, do let me know.

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.

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;

How to format manipulated DateTime fields in dynamic TSQL

I have a query that looks basically like this, wrapped in a dynamic query to accommodate table names that can change. I got the date functions in the middle converted but it doesn't like the LoadedDateTime and CallPlacedTime at the end. I've tried every conversion and combination of quoting those lines that I can think of. How can I accomplish this?
DECLARE #sql_TotalDialsNewLeads nvarchar(1000) = N'
SELECT COUNT(*)
FROM ' + #tbl_CH + ' ch, ' + #tbl_CL + ' cl, ' + #tbl_DA + ' da
WHERE ch.IDENTITY = cl.IDENTITY
AND cl.CRMID = da.CRMID
AND CallPlacedTime BETWEEN ''' + CONVERT(varchar(30),DATEADD(HOUR,-#TimezoneOffset,#StartDate),126) + '''
AND ''' + CONVERT(varchar(30),DATEADD(HOUR,-#TimezoneOffset,#EndDate),126) + '''
AND Product = ''' + #Product + '''
AND Country = ''' + #Country + '''
AND DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,LoadedDateTime))
= DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,CallPlacedTime))'
EXECUTE(#sql_TotalDialsNewLeads);
Thanks,
Sean
Try to use sp_executesql with parameters instead of making string to execute manually:
...
AND CallPlacedTime BETWEEN DATEADD(HOUR,-#TimezoneOffset,#StartDate)
AND DATEADD(HOUR,-#TimezoneOffset,#EndDate)
...
and execute it with the following statement:
exec sp_executesql #sql_TotalDialsNewLeads,
N'#StartDate datetime, #EndDate datetime',
#StartDate,#EndDate;
Here is a MSDN guide. Also you can do it for other parameters in your SQL statement.
Instead of using the conversion outside use it inside the dynamic query.
something like this. (Not tested)
DECLARE #sql_TotalDialsNewLeads nvarchar(1000) = N'
SELECT COUNT(*)
FROM ' + #tbl_CH + ' ch, ' + #tbl_CL + ' cl, ' + #tbl_DA + ' da
WHERE ch.IDENTITY = cl.IDENTITY
AND cl.CRMID = da.CRMID
AND CallPlacedTime BETWEEN CONVERT(varchar(30),DATEADD(HOUR,'''+ -#TimezoneOffset +''','''+ #StartDate +'''),126)
AND CONVERT(varchar(30),DATEADD(HOUR,'''+ -#TimezoneOffset +''','''+ #EndDate +'''),126)
AND Product = ''' + #Product + '''
AND Country = ''' + #Country + '''
AND DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,LoadedDateTime))
= DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,CallPlacedTime))'
EXECUTE(#sql_TotalDialsNewLeads);