Hql Query.nor working - java

I need to write the query according to this logic
LeadRepository.getQualifiedLeadsWithoutClosedWonOrLost to the following query:
select l.id from prospectr360.lead l, prospectr360.lead_action la where l.id = la.lead_id and la.action_id = 6 and l.id not in (select l.id from prospectr360.lead_action la, prospectr360.action a, prospectr360.lead l where la.action_id = a.id and la.lead_id = l.id and reason_id in (61,65))
and I have written
#Query("SELECT lead.id " +
" FROM Lead lead, " +
" LeadAction la, " +
" WHERE lead.id = leadAction.lead.id"+
" AND leadAction.action.id = 6" +
" WHERE NOT EXISTS (SELECT 1 " +
" FROM Lead lead, " +
" Action action, " +
" WHERE action.id = la.action.id" +
" AND la.lead.id = lead.id" +
" AND la.reason.id in (61,65)" +
")")
List<Lead> getQualifiedLeadsWithoutClosedWonOrLost();
}

I resolved it
#Query("SELECT lead " +
" FROM Lead lead, " +
" LeadAction leadAction " +
" WHERE lead.id = leadAction.lead.id"+
" AND leadAction.action.id = 6" +
" AND lead.id NOT IN (SELECT lead1.id " +
" FROM Lead lead1, " +
" LeadAction la ," +
" Action action " +
" WHERE la.action.id = action.id" +
" AND la.lead.id = lead1.id" +
" AND la.reason.id in (61,65)" +
")")
List<Lead> getQualifiedLeadsWithoutClosedWonOrLost();

Related

CASE expression in JPQL

In my SpringBoot project I have CrudRepository with next method:
public interface LoanRequestRepository extends CrudRepository<LoanRequest, UUID> {
#Query(
value = "select " +
"lr.id as id," +
"lr.state as state," +
"lr.iban as iban," +
"lr.amount as amount," +
"lr.rate as rate," +
"lr.term as term," +
"lr.best_offer as best_offer," +
"lr.contract_number as contract_number," +
"lr.created as created," +
"lr.company_id as company_id," +
"lr.closed as closed," +
"lr.loan_colvir_id as loan_colvir_id," +
"lr.contract_date as contract_date," +
"lr.first_payment_date as first_payment_date, " +
"lr.gesv as gesv, " +
"c.id as comp_id," +
"c.name as comp_name," +
"c.bin as comp_bin," +
"c.colvir_id as comp_colvir_id," +
"c.kod as comp_kod," +
"c.type as comp_type," +
"c.location_address as comp_location_address," +
"c.registration_address as comp_registration_address," +
"c.legal_address as comp_legal_address," +
"c.actual_address as comp_actual_address," +
"c.department_code as comp_department_code," +
"c.department_name as comp_department_name," +
"c.resident as comp_resident," +
"c.created as comp_created," +
"c.registration_date as comp_registration_date," +
"c.economic_sector as comp_economic_sector," +
"c.short_name as comp_short_name," +
"c.colvir_reference_id as comp_colvir_reference_id," +
"c.card_system_id as comp_card_system_id," +
"c.initial_registration as comp_initial_registration " +
"from loan_request lr " +
"left join company c on c.id = lr.company_id " +
"where 1=1 " +
"and case when :amount is not null " +
" then lr.amount = :amount " +
" else 1=1 end " +
"and case when cast(:dateFrom as date) is not null " +
" then lr.created >= cast(:dateFrom as date)" +
" else 1=1 end " +
"and case when cast(:dateTo as date) is not null " +
" then lr.created <= cast(:dateTo as date) " +
" else 1=1 end " +
"and case when :bin is not null " +
" then c.bin like concat('%', :bin, '%') " +
" else 1=1 end " +
"and case when :companyName is not null " +
" then lower(c.name) like lower(concat('%', :companyName, '%')) " +
" else 1=1 end " +
" group by lr.id, c.id " +
"order by " +
" CASE WHEN :isAscending = true THEN :orderBy END ASC, " +
" CASE WHEN :isAscending = false THEN :orderBy END DESC " +
"limit :size offset :page;")
Iterable<LoanRequestDto> findAllLoanRequestDtoByFilters(#Param("dateFrom") LocalDate dateFrom,
#Param("dateTo") LocalDate dateTo,
#Param("bin") String bin,
#Param("amount") BigDecimal amount,
#Param("companyName") String companyName,
#Param("page") Integer page,
#Param("size") Integer size,
#Param("orderBy") String orderBy,
#Param("isAscending") Boolean isAscending);
}
All query works well, but it ignores "order by" with case expression and list comes in inordered way. I am sending values as "lr.iban", "c.name" to orderBy parameter, and if translate it to sql, it works. But in JPQL it does not work.
So, where am I wrong? How can I solve it?
Also, if change last part with CASE expression to the next:
order by CASE WHEN :isAscending = true THEN c.name END ASC
It also works, well in JPQL. List comes in ordered way.

ERROR: operator does not exist: character varying = bytea,No operator matches the given name and argument types

I have a JPA UNION query and In GET API swagger three field is there institution(mandatory) ,txBookingLocation(mandatory) customerID(optional) working fine .If i remove 'AND (COALESCE(:customerID.....)' .both the select statement .without removing how it will work ?
ERROR: operator does not exist: character varying = bytea,No operator matches the given name and argument types
#Query(nativeQuery = true,value= "SELECT "
+ " customerid AS customerID, "
+ " stepid AS stepID, "
.....................
+ "institution AS institution, "
+ "limit_groupid AS limitGroupID, "
+ "checkerid AS checkerID, "
+ "checker_timestamp AS checkerTimestamp, "
+ "step_action AS stepAction, "
+ "step_status AS stepStatus, "
+ "tx_booking_location AS txBookingLocation, "
...............
+ "customer_name AS customerName "
+ "FROM txrh_bcaterms where step_status IN ('SAV', 'REJ') " + " AND institution = :institution "
+ " AND (COALESCE(:txBookingLocation, null) is null or (tx_booking_location IN :txBookingLocation)) "
+ " AND (COALESCE(:customerID, null) is null or (customerid = :customerID)) "
+ "UNION "
+ "SELECT "
+ "customerid AS customerID, "
+ "null AS stepID, "
...........
+ "finance_type AS financeType, "
....................
+ "institution AS institution, "
+ "limit_groupid AS limitGroupID, "
+ "null AS checkerID, "
+ "null AS checkerTimestamp, "
+ "null AS stepAction, "
+ " 'RLS' AS stepStatus, "
+ "tx_booking_location AS txBookingLocation, "
............................
+ "customer_name AS customerName "
+ "FROM txrm_bcaterms where (institution, tx_booking_location,customerid) NOT IN "
+ " (select institution, tx_booking_location,customerid from txrh_bcaterms where step_status IN ('SAV', 'REJ', 'PNR') "
+ " AND (COALESCE(:txBookingLocation, null) is null or ( tx_booking_location IN :txBookingLocation))"
+ " AND (COALESCE(:customerID, null) is null or (customerid = :customerID)) "
+ " AND institution = :institution ) "
+ " AND institution = :institution "
+ " AND (COALESCE(:txBookingLocation, null) is null OR tx_booking_location IN :txBookingLocation)"
+ " AND (COALESCE(:customerID, null) is null or customerid = :customerID)" ,
countQuery = " SELECT count(*) FROM ("
....................
+ ")as cnt")
Page<ITXBCATermsHistoryBookingLocationAndInstitutionDTO> historyAndMaster(
#Param("institution") String institution,
#Param("txBookingLocation") List<String> txBookingLocation,
#Param("customerID") String customerID,
Pageable page);
}
ERROR: operator does not exist: character varying = bytea
I am not getting what has went wrong. Any help would be appreciated.

Common table expression, with a starting with a values statement gives error in java

I've this CTE which I'd implement in Java:
It's running on an IBM Iseries 7.3 DB2 machine.
WITH params (from_date, to_date, outlet, product_number)
AS (
values(TO_DATE('01.11.2018', 'DD.MM.YYYY'),
TO_DATE('18.12.2018', 'DD.MM.YYYY'),
'BLK' ,
49 )
),
product
AS (
SELECT DISTINCT cpp.competitor_products_id product
FROM yxdb.competitor_product_prices cpp
INNER JOIN yxdb.competitor_products_comparisons cpc ON cpc.competitor_products_id = cpp.competitor_products_id
AND cpc.deleted = 0
INNER JOIN yxdb.outlets o ON o.outlets_id = cpc.outlets_id
AND o.deleted = 0
INNER JOIN params ON cpp.price_date > params.from_date
AND cpc.product_number = params.product_number
AND o.short_name = params.outlet
WHERE cpp.deleted = 0
)
select * from product;
It's a lot longer, so the params table is used several times.
When implementing it in Java, I replace the hardcoded dates and other parameters in Java as ?1, ?2 etc. I've also tried with named parameters, none works. They all give [SQL0418] Use of parameter marker or NULL not valid.
Java Code snippet:
#RepositoryRestResource
public interface CompetitorPriceDateRepository extends JpaRepository<CompetitorPriceDateEntity, Long> {
#Query(value = "WITH params (from_date, to_date, outlet, product_number) "
+ " AS ( "
+ " values(TO_DATE( :fromDate , 'DD.MM.YYYY'), "
+ " TO_DATE( :toDate , 'DD.MM.YYYY'), "
+ " :outlet , "
+ " :productNumber ) "
+ " ), "
+ " product "
+ " AS ( "
+ " SELECT DISTINCT cpp.competitor_products_id product "
+ " FROM yxdb.competitor_product_prices cpp "
+ " INNER JOIN yxdb.competitor_products_comparisons cpc ON +" cpc.competitor_products_id = cpp.competitor_products_id "
+ " AND cpc.deleted = 0 "
+ " INNER JOIN yxdb.outlets o ON o.outlets_id = cpc.outlets_id "
+ " AND o.deleted = 0 "
+ " INNER JOIN params ON cpp.price_date > params.from_date "
+ " AND cpc.product_number = params.product_number "
+ " AND o.short_name = params.outlet "
+ " WHERE cpp.deleted = 0 "
+ " ) "
+ " select * from product ",nativeQuery = true)
List<CompetitorPriceDateEntity> findAllInterpolatedByDates(
#Param("productNumber") Integer productNumber,
#Param("outlet") String outlet,
#Param("fromDate") String fromDate,
#Param("toDate") String toDate
);
Without the stack trace I dont' have any aidea what's wrong with your query. SQL seems solid.
Try with a named native query
#Entity
#NamedNativeQuery(
name = “competitor.findProducts.byDateOutletProductnumber",
query = "WITH params (from_date, to_date, outlet, product_number) "
+ " AS ( "
+ " values(TO_DATE( :fromDate , 'DD.MM.YYYY'), "
+ " TO_DATE( :toDate , 'DD.MM.YYYY'), "
+ " :outlet , "
+ " :productNumber ) "
+ " ), "
+ " product "
+ " AS ( "
+ " SELECT DISTINCT cpp.competitor_products_id product "
+ " FROM yxdb.competitor_product_prices cpp "
+ " INNER JOIN yxdb.competitor_products_comparisons cpc ON +" cpc.competitor_products_id = cpp.competitor_products_id "
+ " AND cpc.deleted = 0 "
+ " INNER JOIN yxdb.outlets o ON o.outlets_id = cpc.outlets_id "
+ " AND o.deleted = 0 "
+ " INNER JOIN params ON cpp.price_date > params.from_date "
+ " AND cpc.product_number = params.product_number "
+ " AND o.short_name = params.outlet "
+ " WHERE cpp.deleted = 0 "
+ " ) "
+ " select * from product ",
hints = {
#QueryHint(name = "org.hibernate.cacheable", value = "true") })
public class CompetitorPriceDateEntity { ... }
pro bonus 1: Named queries are sort of precompiled, so jpa doesn't need to compile the query into a executable criteria every time it gets called.
pro bonus 2: Query hint cacheable or eclipselink equivalent

sqlite JOIN query for multiple entries

My database has "diary" table and "food" table. In diary table I have following columns: date, food1,food2, ...food10. In food table I have: id, name, allergen1, allergen2, allergen3. So basically you log up to 10 foods a day to diary. Now I'm trying to build a query which would let me find last date that I've eaten food with allergen3. So that's the query I came up with, which you probably know is not working ;-)
Cursor findDateRes = db.rawQuery("SELECT date(" + DIARY_COL_DATE + ") FROM " + DIARY_TABLE
+ " INNER JOIN " + FOOD_TABLE + " food1 ON (food1." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F1 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food2 ON (food2." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F2 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food3 ON (food3." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F3 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food4 ON (food4." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F4 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food5 ON (food5." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F5 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food6 ON (food6." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F6 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food7 ON (food7." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F7 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food8 ON (food8." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F8 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food9 ON (food9." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F9 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food10 ON (food10." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F10 +
") WHERE 1 NOT IN (food1." + FOOD_COL_5 +
",food2." + FOOD_COL_5 +
",food3." + FOOD_COL_5 +
",food4." + FOOD_COL_5 +
",food5." + FOOD_COL_5 +
",food6." + FOOD_COL_5 +
",food7." + FOOD_COL_5 +
",food8." + FOOD_COL_5 +
",food9." + FOOD_COL_5 +
",food10." + FOOD_COL_5 +
") ORDER BY date(" + DIARY_COL_DATE + ") ASC LIMIT 1", null);
"FOOD_COL_1" stands for id and "FOOD_COL_5" stands fro allergen3.
Thank you for looking. Any input will be appreciated.

Datatype result SPARQL query in Jena

I've a problem in results of my sparql query.
This is my query :
String queryBody = "REGISTER QUERY noreasoning AS "
+ "PREFIX ds:<http://www.dati.lombardia.it/resource/nf78-nj6b/> "
+ "SELECT ?idSensore ?valoreMisurazione ?tipologia ?nomeComune "
+ "FROM STREAM <http://streamreasoning.org/streams/sensors> [RANGE 10s STEP 1s] "
+ "FROM <http://streamreasoning.org/roomConnection> "
+ "WHERE { "
+ "?s ds:idsensore ?idSensore. "
+ "?s ds:observes ?valoreMisurazione. "
+ "?s ds:tipologia ?tipologia. "
+ "?s ds:nomecomune ?nomeComune. "
+" FILTER ( datatype(?tipologia) = xds:langString)\n"
+ "} ";
and my result is this:
-------3 results at SystemTime=[1464188665959]--------
"2447"^^null 6 "Temperatura"^^null "Bema"^^null
"8016"^^null -1.3 "Temperatura"^^null "Aprica"^^null
"4066"^^null 7.9 "Temperatura"^^null "Arconate"^^null
What should I change to remove "^^null"?

Categories

Resources