Hibernate how to build sql query? - sql

I have the following query in Postgres 11
SELECT DISTINCT ON (account.name) *
FROM sys_accounts AS account
INNER JOIN sys_activities AS activity ON account.guid=activity.account_guid
WHERE activity.datefinish IS NOT NULL
AND (extract(day from NOW() - activity.datefinish)) > 60
AND dc_status_guid='df11a68c-20d3-46e2-840f-2e4548aacf1b'
ORDER BY account.name,activity.datefinish DESC
How can I run that through Hibernate (5.4)
I need get all accounts where last task closed > 60 days ago
Java structure like
Account (oneToMany){
Activity1 :finishdate |stage ....
Activity2 :finishdate |stage ....
......
}
I don't understand how to write this request with hibernate syntax HQL or CRITERIA. With HQL i get error:Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ON near line 1, column 17 [SELECT DISTINCT ON (account.name)* FROM sys_accounts AS account INNER JOIN sys_activities AS activity ON account.guid=activity.account_guid WHERE activity.datefinish IS NOT NULL AND dc_status_guid='df11a68c-20d3-46e2-840f-2e4548aacf1b' ORDER BY account.name,activity.datefinish DESC]

Related

Where clause with dates in hive

The where clause in the below hive query is not working
select
e.num as badge
from dbo.events as e
where TO_DATE(e.event_time_utc) > TO_DATE(select event_date from DL_EDGE_LRF_facilities.card_swipes_lastpulldate)
both event_time_utc and event_date fields are defined as strings and event_time_utc has timestamp values like '2017-09-18 20:10:19.000000' and event_date has only one date value like '2018-01-25'
i am getting an error like "cannot recognize input near 'select' 'event_date' 'from' in function specification " when i run the query, Please help
#user86683; hive does not recognize the syntax since it does not allow in-query in the inequality condition (>). You may try this query and let me know the result.
select e.num as badge
from dbo.events as e, DL_EDGE_LRF_facilities.card_swipes_lastpulldate c
where TO_DATE(e.event_time_utc) > TO_DATE(c.event_date)
You will get a warning but you may ignore it since the table for event_date has only one record.
Warning: Map Join MAPJOIN[10][bigTable=e] in task 'Map 1' is a cross product
Query ID = xxx_20180201102128_aaabb2235-ee69275cbec1
Total jobs = 1
Launching Job 1 out of 1
Status: Running (Executing on YARN cluster with App id application_09fdf345)
Hope this helps. Thanks.

SQL Server query issues

We have a SQL Server 2012 database with our live project including multiple tables and records, Basically We are facing a problem with SQL queries on a table where two same SQL queries. The first SQL query is taking less execution time and second SQL query is getting tremendously slow to execute.
I don't know why it is happening someone could help me to solve this problem?
Our two queries are given below....
First query (taking so much time to execute):
SELECT * FROM (SELECT TOP 10 TrackerResponse.EventName,TrackerResponse.ReceiveTime,ISNull(TrackerResponse.InputStatus,0) AS InputStatus,
TrackerResponse.Latitude,TrackerResponse.Longitude,TrackerResponse.Speed,
TrackerResponse.TrackerID,TrackerResponse.OdoMeter,TrackerResponse.Direction,
UserCar.CarNo FROM TrackerResponse
INNER JOIN UserCar ON (UserCar.TrackerID = TrackerResponse.TrackerID)
WHERE (TrackerResponse.EventName IS NOT NULL AND TrackerResponse.EventName<>'')
AND TrackerResponse.TrackerID = 112 Order By ID DESC) AS Events)
Second query (taking less execution time):
SELECT * FROM (SELECT TOP 10 TrackerResponse.EventName,TrackerResponse.ReceiveTime,ISNull(TrackerResponse.InputStatus,0) AS InputStatus,
TrackerResponse.Latitude,TrackerResponse.Longitude,TrackerResponse.Speed,
TrackerResponse.TrackerID,TrackerResponse.OdoMeter,TrackerResponse.Direction,
UserCar.CarNo FROM TrackerResponse
INNER JOIN UserCar ON (UserCar.TrackerID = TrackerResponse.TrackerID)
WHERE (TrackerResponse.EventName IS NOT NULL AND TrackerResponse.EventName<>'')
AND TrackerResponse.TrackerID = 56 Order By ID DESC) AS Events
I see Both queries are similar with an exception of tracker id..so my best guess would be ,this query might be a victim of Parameter sniffing..
Try using Option(Recompile) like below to see if this is the cause and follow article referred in above link for resolution
SELECT * FROM (SELECT TOP 10 TrackerResponse.EventName,TrackerResponse.ReceiveTime,ISNull(TrackerResponse.InputStatus,0) AS InputStatus,
TrackerResponse.Latitude,TrackerResponse.Longitude,TrackerResponse.Speed,
TrackerResponse.TrackerID,TrackerResponse.OdoMeter,TrackerResponse.Direction,
UserCar.CarNo FROM TrackerResponse
INNER JOIN UserCar ON (UserCar.TrackerID = TrackerResponse.TrackerID)
WHERE (TrackerResponse.EventName IS NOT NULL AND TrackerResponse.EventName<>'')
AND TrackerResponse.TrackerID = 56 Order By ID DESC
option(recompile)
AS Events)

Inner Join between 2 queries resulting in "Invalid Operation"

In an attempt to create a listing of Orders (each with multiple items) that satisfy some criteria, I have attempted to create a typical LEFT JOIN statement.
The attempt looks like this
SELECT
Q1.Order_Number,
OD.Item_Num
FROM
(
SELECT
OS.Order_Number
FROM
[4-Open_Order_Summary] AS OS
WHERE
Date() >= OS.Ship_Date AND
OS.Back_Ordered > 0
)
AS Q1
LEFT JOIN [1-Open_Order_Data] AS OD
ON Q1.Order_Number = OD.Order_Number
Running this query gives me an unexplained "Invalid operation" error. Researching this error with regards to Access SQL has led me to this question on StackOverflow pertaining to multiple JOIN statements of different types, and this question on the SuperUser branch pertaining to FULL OUTER JOIN statements. However I was unable to find questions related to a single LEFT JOIN statement.
In my attempts to resolve this I have done the following;
Changing
ON Q1.Order_Number = OD.Order_Number to
ON Q1.Order_Number LIKE OD.Order_Number
crashes Access
Running
SELECT
Q1.Order_Number,
FROM
(
SELECT
OS.Order_Number
FROM
[4-Open_Order_Summary] AS OS
WHERE
Date() >= OS.Ship_Date AND
OS.Back_Ordered > 0
)
AS Q1
returns the intended order numbers.
Why not try something like the following if you're trying to get Order Numbers from one table, and related Order Details from another?
SELECT
Q1.Order_Number
OD.Item_Num
FROM
[4-Open_Order_Summary] Q1
LEFT JOIN
[1-Open_Order_Data] OD
ON
OD.Order_Number = Q1.Order_Number
WHERE
DATE() >= Q1.Ship_Date
AND Q1.Back_Ordered > 0

How should I create a temporary table when Union is used?

I can't use Dynamic Value bcoz of Error stating
"Lookup Error - SQL Server Database Error: Cannot perform an aggregate function on an expression containing an aggregate or a subquery."
Here is the Scenario :
Query 1
select pr.PRDCT,sum(CASE when pr.DEFINITIONCD='NOP' and pr.PERIOD='D' then pr.PRAMOUNT else 0 END)
as 'NOP D' from PRODUCTWISE_REPORT pr group by pr.PRDCT
Query 2
select DEFINITIONTYPECD from REPORTKPIMAPTXN where DEFINITIONTYPECD='NOP' and REPORTSEQ = (select REPORTSEQ from report_m where REPORTCD='MIS_Product_Wise_Report')
Query 2 returns 'NOP'
so when I put Query 2 in Query 1 for 'NOP', it throws Error
How to resolve this when I've to User Dynamic Query 2 ?
Your second query looks it could be rewritten with a join instead of that subselect. something like this. Of course you are still going to have some issues because your first query has two columns and this has only 1 column. You will have to add another column (can be NULL) to this query before the UNION will actually work.
select r.DEFINITIONTYPECD
from REPORTKPIMAPTXN r
INNER JOIN report_m m on m.REPORTSEQ = r.REPORTSEQ
where DEFINITIONTYPECD = 'NOP'
and r.REPORTCD = 'MIS_Product_Wise_Report'

SQL for COUNT and GROUP BY

This is my first experience posting a question in this site :) so, i´m in trouble. I have a database in access, that i connect with ODBC.
Database have 3 tables:
nombres : (-idnombre, -nombre)
rutas : (-idruta, ruta)
fechas : (-idfecha, idruta, idnombre, fecha)
Sample data :
nombres:(1,nombreA),(2,nombreB)
Rutas:(1,rutaA),(2,RutaB)
fechas:(1,1,1,28/06/2013), (2,2,1,28/06/2013), (3,2,2,28/06/2013),(4,2,2,28/06/2013)
so i need this output ( third field is a count ):
rutaA - nombreA - 1 time
rutaA - nombreB - 0 times
rutaB - nombreA - 1 time
rutaB - nombreB - 2 times
my sql is:
SELECT rutas.ruta, nombres.nombre, Count(fechas.idruta) AS CuentaDeidruta
FROM rutas INNER JOIN
(nombres INNER JOIN fechas ON nombres.idnombre = fechas.idnombre)
ON rutas.idruta = fechas.idruta
GROUP BY rutas.ruta, nombres.nombre;
its ok, but is not showing zero counts. so my output is:
rutaA - nombreA - 1 time
rutaB - nombreA - 1 time
rutaB - nombreB - 2 times
I tried with left join, so i get some errors.
I think you're looking for the Cartesian Product of Rutas and Nombres, thus giving you something like this as in your sample data, there is no correlation between rutaA and nombreB.
SELECT t.ruta, t.nombre, Count(fechas.idruta) AS CuentaDeidruta
FROM (
SELECT rutas.idruta, rutas.ruta, nombres.idnombre, nombres.nombre
FROM rutas, nombres
) t
LEFT JOIN fechas ON
t.idnombre = fechas.idnombre AND fechas.idruta = t.idruta
GROUP BY t.ruta, t.nombre;
SQL Fiddle Demo
This is for SQL Server, but should port over to MS Access pretty closely. Also please note the usage of a LEFT JOIN to ensure you get the 0s you were missing.