hql/jpql hibernate with jpa not working for cos and radions function - jpql

following query works fine in mysql query browser but when I move this to either jpql or native query and tried to execute using entity manager not getting any results also not seeing any errors too.
I am using Hibernate core 3.3.0 Entity Manager 3.4.0 with spring IOC for entityManager for injection
select * from location
where 1=1
and latitude is not null and longitude is not null
and (6371 *
acos( cos( radians(12.922253 ) ) *
cos( radians(latitude) ) *
cos( radians( 77.614417 ) - radians(longitude) )
+ sin( radians(12.922253 ) ) *
sin( radians( latitude ) )
) ) < 100.0

I believe "&&" is not part of JPQL. Try replacing it with "and".

Related

jOOQ: Can you force LIMIT to be rendered as ROW_NUMBER() window function?

I see this bit in the jOOQ docs:
https://github.com/jOOQ/jOOQ/blob/d727e6c476e8b1cbed1c91fd3724936c73cd9126/jOOQ/src/main/java/org/jooq/SelectLimitStep.java#L135-L147
/**
* Add a <code>LIMIT</code> clause to the query
* <p>
* If there is no <code>LIMIT</code> or <code>TOP</code> clause in your
* RDBMS, this may be emulated with a <code>ROW_NUMBER()</code> window
* function and nested <code>SELECT</code> statements.
* <p>
* This is the same as calling {#link #limit(Number, Number)} with offset = 0, or
* calling <code>.limit(numberOfRows).offset(0)</code>
*/
I'm wondering if there is a setting to force-enable this option?
There seems to be a setting for the opposite, to convert ROW_NUMBER to LIMIT, but not LIMIT to ROW_NUMBER.
To get around this, I've written the below but if the ability exists in the codebase (and is probably implemented better) I'd like to take advantage of it:
fun wrapQueryInRowNumberSubquery(
stmt: SelectFinalStep<Record>,
limit: Int = 0,
offset: Int = 0
): SelectConditionStep<Record> {
stmt.query.addSelect(
DSL.rowNumber().over()
.partitionBy(DSL.field("*")) // custom logic here
.orderBy(DSL.field("*")) // custom logic here
.`as`("row_num")
)
return DSL.select(DSL.asterisk()).from(stmt)
.where(
DSL.field("row_num").greaterThan(
DSL.inline(offset)
)
)
.and(
DSL.field("row_num").lessThan(
DSL.inline(offset + limit)
)
)
}
There are currently (jOOQ 3.17) no flags to enable / disable individual emulations independently of the SQL dialect.

SQL Oracle Sub-query

I am having a issue getting this Sub-query to run. I am using Toad Data Point -Oracle. I get syntax error. I have tried several different ways with no luck. I am knew to sub-query's
Select *
from FINC.VNDR_ITEM_M as M
where M.ACCT_DOC_NBR = A.ACCT_DOC_NBR
(SELECT A.CLIENT_ID,
A.SRC_SYS_ID,
A.CO_CD,
A.ACCT_NBR,
A.CLR_DT,
A.ASGN_NBR,
A.FISCAL_YR,
A.ACCT_DOC_NBR,
A.LINE_ITEM_NBR,
A.MFR_PART_NBR,
A.POST_DT,
A.DRCR_IND,
A.DOC_CRNCY_AMT,
A.CRNCY_CD,
A.BSL_DT
FROM FINC.VNDR_ITEM_F A
WHERE A.CLR_DT IN (SELECT MAX(B.CLR_DT)
FROM FINC.VNDR_ITEM_F AS B
where (B.ACCT_DOC_NBR = A.ACCT_DOC_NBR and B.FISCAL_YR=A.FISCAL_YR and B.LINE_ITEM_NBR = A.LINE_ITEM_NBR and B.SRC_SYS_ID =A.SRC_SYS_ID and B.POST_DT=A.POST_DT and B.CO_CD=A.CO_CD)
and (B.CO_CD >='1000' and B.CO_CD <= '3000' or B.CO_CD ='7090') and (B.POST_DT Between to_date ('08/01/2018','mm/dd/yyyy')
AND to_date ('08/31/2018', 'mm/dd/yyyy')) and (B.SRC_SYS_ID ='15399') and (B.FISCAL_YR ='2018'))
GROUP BY
A.CLIENT_ID,
A.SRC_SYS_ID,
A.CO_CD,
A.ACCT_NBR,
A.CLR_DT,
A.ASGN_NBR,
A.FISCAL_YR,
A.ACCT_DOC_NBR,
A.LINE_ITEM_NBR,
A.MFR_PART_NBR,
A.POST_DT,
A.DRCR_IND,
A.DOC_CRNCY_AMT,
A.CRNCY_CD,
A.BSL_DT)
Your syntax is broken, you put subquery just at the end. Now it looks like:
select *
from dual as m
where a.dummy = m.dummy
(select dummy from dual)
It is in incorrect place, not joined, not aliased. What you should probably do is:
select *
from dual m
join (select dummy from dual) a on a.dummy = m.dummy
You also have some redundant, unnecessary brackets, but that's minor flaw. Full code (I cannot test it without data access):
select *
from FINC.VNDR_ITEM_M M
join (SELECT A.CLIENT_ID, A.SRC_SYS_ID, A.CO_CD, A.ACCT_NBR, A.CLR_DT, A.ASGN_NBR,
A.FISCAL_YR, A.ACCT_DOC_NBR, A.LINE_ITEM_NBR, A.MFR_PART_NBR, A.POST_DT,
A.DRCR_IND, A.DOC_CRNCY_AMT, A.CRNCY_CD, A.BSL_DT
FROM FINC.VNDR_ITEM_F A
WHERE A.CLR_DT IN (SELECT MAX(B.CLR_DT)
FROM FINC.VNDR_ITEM_F AS B
where B.ACCT_DOC_NBR = A.ACCT_DOC_NBR
and B.FISCAL_YR=A.FISCAL_YR
and B.LINE_ITEM_NBR = A.LINE_ITEM_NBR
and B.SRC_SYS_ID =A.SRC_SYS_ID
and B.POST_DT=A.POST_DT
and B.CO_CD=A.CO_CD
and (('1000'<=B.CO_CD and B.CO_CD<='3000') or B.CO_CD='7090')
and B.POST_DT Between to_date ('08/01/2018', 'mm/dd/yyyy')
AND to_date ('08/31/2018', 'mm/dd/yyyy')
and B.SRC_SYS_ID ='15399' and B.FISCAL_YR ='2018')
GROUP BY A.CLIENT_ID, A.SRC_SYS_ID, A.CO_CD, A.ACCT_NBR, A.CLR_DT, A.ASGN_NBR,
A.FISCAL_YR, A.ACCT_DOC_NBR, A.LINE_ITEM_NBR, A.MFR_PART_NBR, A.POST_DT,
A.DRCR_IND, A.DOC_CRNCY_AMT, A.CRNCY_CD, A.BSL_DT) A
on M.ACCT_DOC_NBR = A.ACCT_DOC_NBR and M.CO_CD=A.CO_CD;
You need to add an alias to the SubSelect (or Derived Table in Standard SQL):
select *
from
( select .......
) AS dt
join ....

is it possible to do geolocation with sails/waterline?

i am working on a geolocation based app allowing user to retrieve adverts nearby his location.
i have developped a version with symfony2/mysql but now i would like to migrate the project to sails but i can't figure out how to do the equivalent of this sql query with sails/waterline:
SELECT
*
,((ACOS(SIN(#orig_lat * PI() / 180) * SIN(`lat` * PI() / 180) + COS(#orig_lat * PI() / 180) * COS(`lat` * PI() / 180) * COS((#orig_long - `long`) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance`
FROM `cities`
WHERE
(
`lat` BETWEEN (#orig_lat - #bounding_distance) AND (#orig_lat + #bounding_distance)
AND `long` BETWEEN (#orig_long - #bounding_distance) AND (#orig_long + #bounding_distance)
)
ORDER BY `distance` ASC
limit 25;
is it even possible to do so with sails/waterline ? (i am using mongodb as driver)

Calculating Quartiles in Analysis Services

I´m using MDX code to calculate quartile, , like in this blog:
https://electrovoid.wordpress.com/2011/06/24/ssas-quartile/
That is what I´m doing:
WITH SET OrderedData AS
ORDER
(
NONEMPTY
(
[Dim Parameter].[id].[id]
*[Dim Result].[Id].[Id].ALLMEMBERS,
[Measures].[Value]
),
[Measures].[Value],
BASC
)
MEMBER [Measures].[RowCount] AS COUNT (OrderedData)
MEMBER [Measures].[i25] AS ( .25 * ( [RowCount] - 1 ) ) + 1
MEMBER [Measures].[i25Lo] AS FIX([i25]) - 1
MEMBER [Measures].[i25Rem] AS ([i25] - FIX([i25]))
MEMBER [Measures].[n25Lo] AS (OrderedData.Item([i25Lo]), [Value])
MEMBER [Measures].[n25Hi] AS (OrderedData.Item([i25Lo] + 1), [Value])
MEMBER [Measures].[Quartile1] AS [n25Lo] + ( [i25Rem] * ( [n25Hi] - [n25Lo] ))
,FORMAT_STRING='Currency'
MEMBER [Measures].[Quartile2] AS MEDIAN(OrderedData, [Value])
,FORMAT_STRING='Currency'
MEMBER [Measures].[i75] AS ( .75 * ( [RowCount] - 1 ) ) + 1
MEMBER [Measures].[i75Lo] AS FIX([i75]) - 1
MEMBER [Measures].[i75Rem] AS ([i75] - FIX([i75]))
MEMBER [Measures].[n75Lo] AS (OrderedData.Item([i75Lo] ),[Value])
MEMBER [Measures].[n75Hi] AS (OrderedData.Item([i75Lo] + 1),[Value])
MEMBER [Measures].[Quartile3] AS [n75Lo] + ( [i75Rem] * ( [n75Hi] - [n75Lo] ))
,FORMAT_STRING='Currency'
MEMBER [Measures].[RIC] As ([Quartile3]-[Quartile1] )
MEMBER [Measures].[Ls] As ([Quartile3]+ ([RIC]*1.5) )
MEMBER [Measures].[Li] As ([Quartile1]- ([RIC] *1.5))
MEMBER [Measures].[MAX] as MAX (Filter(OrderedData ,[value]<=[LS]),[value])
MEMBER [Measures].[Min] as MIn(Filter(OrderedData ,[value]>=[Li]),[value])
MEMBER [Measures].[out] as MAX (Filter(OrderedData ,[value]>[LS]),[value
What I want is to add Dim date, to calculate the quartiles for each month, something like this:
MEMBER [Measures].[out] as MAX (Filter(OrderedData ,[value]>[LS]),[value
SELECT {
[Measures].[Quartile1],[Measures].[Quartile2],[Measures].[Quartile3], [min],
[MAX] , [out] , [Measures].[ValueAVG],[RowCount],[Measures].[Recuento Fact Result]
} ON 0 ,
[Dim Parameter].[Reference].[Reference] *
[Dim Parameter].[Section ES].[Section ES] *
[Id Distribution Date].[DateJ].[Month] ON 1
FROM [Tess Tek DW Dev]
But it didn't work, How i can calculate quartiles of different date ranges in only one mdx query?
You need to work the Date into your WITH statement somehow.
Try first adding your target months, that will be on the rows, into a named set:
WITH
SET [TargetSet] AS
{
[Id Distribution Date].[DateJ].[Month].[Jan-2015],
[Id Distribution Date].[DateJ].[Month].[Feb-2015]
}
Then I'd add another set taking TargetSet into account:
SET [NonEmptyIds] AS
NonEmpty(
[Dim Parameter].[id].[id]
*[Dim Result].[Id].[Id].ALLMEMBERS
,
{[Measures].[Value]} * [TargetSet]
)
Then feed this set into your current set:
SET [OrderedData] AS
ORDER
(
[NonEmptyIds],
[Measures].[Value],
BASC
)
Then try amending the rows snippet to use the TargetSet:
[Dim Parameter].[Reference].[Reference] *
[Dim Parameter].[Section ES].[Section ES] *
[TargetSet] ON 1

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