jpql query with date literal causes IllegalArgumentException - jpql

I am trying to write query with date literal:
SELECT r
FROM Restaurant r
LEFT JOIN r.dishes dh ON dh.date = {d '2019-12-31'}
GROUP BY r
But when running I get an error
java.lang.IllegalArgumentException: org.hibernate.QueryException: unexpected char: '{'
What's wrong?

Try use only date in ISO format
SELECT r
FROM Restaurant r
LEFT JOIN r.dishes dh ON dh.date = '2019-12-31'
GROUP BY r

Related

Attempting to join: error "Conversion failed when converting from a character string to uniqueidentifier."

I am getting the following error when attempting to join on two ID fields:
select top 10 *
from SOURCECODE as s
inner join APPEAL as a on s.APPEALID = a.APPEALID;
error: Conversion failed when converting from a character string to uniqueidentifier.
Any ideas on how to fix?
Do I need to convert the fields?
The s.appealid is nvarchar 36
The a.appealid is uniqueidentifier
SELECT TOP 10 *
FROM SOURCECODE AS s
INNER JOIN APPEAL AS a ON CONVERT(uniqueidentifier, s.APPEALID) = a.APPEALID;

How to get variable by select in spark

I want get variable with sql query:
Dt = spark.sql("""select max(dt) from table""")
Script = """select * from table where dt > """ + dt
Spark.sql(script)
but when I try to substitute a variable in the request I get error:
"Can only concatenate str (not dataframe) to str"
How do I get the variable as a string and not a dataframe?
To get the result in a variable, you can use collect() and extract the value. Here's an example that pulls the max date-month (YYYYMM) from a table and stores it in a variable.
max_mth = spark.sql('select max(mth) from table').collect()[0][0]
print(max_mth)
# 202202
You can either cast the value to string in the sql statement, or use str() on the variable while using to convert the integer value to string.
P.S. - the [0][0] is to select the first row-column

Databricks SQL, error when running update with join

I am trying to run an update on a Delta table and I am getting the following error.
Error in SQL statement: ParseException:
mismatched input 'from' expecting <EOF>(line 3, pos 0)
I really can't figure out why I get this error. Can anyone help me?
update eff
set eff.ACP = ia.COSTVALUE
from views.test_acp_effect eff
left join source_tables_db.ia_master_items ia
on eff.CODE = ia.IMM_CODE
where eff.DXN_Period = (
select td.MY_FISCAL_PERIOD_ABBR
from timedelta td
where current_date() between td.MIN_P_DATE
and td.MAX_P_DATE
)
and eff.CODE = source_tables_db.ia_master_items.IMM_CODE

self taught -syntax error i can find- this query works in my system but no embedded in excel to same database

new to this (very new- and self teaching).....i have a query that draws from multiple tables on my computer system that gets all the appraised values and sales values from a subdivision. in my system, it runs the query fine. but when i try to convert it to run embedded in an excel sheet it gives me error saying no column name for 2 c and 3 c. when i put punctuation around the column names it says there is a syntax error with the alias "as c" at the bottom-- been awake too long--- what am i doing wrong ?:
select distinct pv.prop_id, ac.file_as_name,
'sale_type' , 'deed_date' , 'sale_date' , 'sale_type' , 'sale_price' ,
(pv.land_hstd_val + pv.land_non_hstd_val + pv.ag_market + pv.timber_market)as land_val,
(pv.imprv_hstd_val + pv.imprv_non_hstd_val)as imprv_val,
pv.market, pv.abs_subdv_cd
from property_val pv with (nolock)
inner join prop_supp_assoc psa with (nolock) on
pv.prop_id = psa.prop_id
and pv.prop_val_yr = psa.owner_tax_yr
and pv.sup_num = psa.sup_num
inner join property p with (nolock) on
pv.prop_id = p.prop_id
inner join owner o with (nolock) on
pv.prop_id = o.prop_id
and pv.prop_val_yr = o.owner_tax_yr
and pv.sup_num = o.sup_num
inner join account ac with (nolock) on
o.owner_id = ac.acct_id
left outer join
(select cop.prop_id,
convert(varchar(20), co.deed_dt, 101)as deed_date,
convert(varchar(20), s.sl_dt, 101)as sale_date,
s.sl_price as sale_price, s.sl_type_cd as sale_type
from chg_of_owner_prop_assoc cop with (nolock)
inner join chg_of_owner co with (nolock) on
co.chg_of_owner_id = cop.chg_of_owner_id
inner join sale s with (nolock) on
co.chg_of_owner_id = s.chg_of_owner_id
where cop.seq_num = 0) as c
on c.prop_id = pv.prop_id
where pv.prop_val_yr = 2016
and(pv.prop_inactive_dt is null or udi_parent ='t')
and pv.abs_subdv_cd in('s3579')
order by pv.abs_subdv_cd, pv.prop_id
Is it SQL Server? Try surrounding column names with square brackets instead of quotes.

JPQL JOINS with nested SELECT

Can I do something like this on JPQL?
SELECT NEW com.MyDTO(p.a, p.b, q.c, q.d)
FROM
(SELECT r.* FROM MyDTO1 r ) p
LEFT OUTER JOIN
(SELECT s.* FROM MyDTO2 s ) q
ON p.x = q.y
or similar?
(Above query has mixed with native and JPQL, so don't misunderstand)
I'm having a problem with this part I think.
FROM
(SELECT r.* FROM MyDTO1 r ) p
When I'm trying to execute I'm getting this error.
Exception Description: Syntax error parsing the query [.....], unexpected token [(]
Thank you!
No, you can't. Quote from the documentation:
Note that HQL subqueries can occur only in the select or where
clauses.
Yes you can!
You have to use native queries. Here is an example:
emf = Persistence.createEntityManagerFactory("TEST")
EntityManager em = emf.createEntityManager();
String queryString = "SELECT ID FROM ( SELECT * FROM ADDRESS WHERE ID < 0)";
Query query = em.createNativeQuery(queryString);
List<BigDecimal> result = query.getResultList();

Resources