Hi i have a long query for a spring, java 8, hibernate, mysql application, which works as expected, except the last part, which sums the reserved items in store:
SELECT new example.package.RaktarKimutatasDTO(rk.cikkId.cikkName,
CONCAT(SUM(rk.mennyisegMe1),' ', rk.me1.unitCode, ' / ', SUM(rk.mennyisegMe2),' ', rk.me2.unitCode, ' / ', SUM(rk.mennyisegMe3),' ', rk.me3.unitCode),
CONCAT(AVG(rk.listaar), ' Ft'),
CONCAT(rk.mennyisegMe1*rk.listaar, ' Ft'),
rk.raktarId.raktarNeve, rk.cikkId.vtsz.vatCode,
SUM(CASE WHEN rk.vevoiEntity IS NOT NULL THEN rk.mennyisegMe1 ELSE 0.0 END))
FROM RaktarkeszletEntity rk
WHERE rk.raktarId.identifier IN ?1 AND rk.createDate<?2 AND (rk.bejovoszamlaEntity LIKE 'RB%' OR (rk.bejovoszamlaEntity LIKE 'BM%' AND rk.bevetel = TRUE))
GROUP BY rk.cikkId
The problem is, the SUM(CASE... ) part is always returns 0.
While if i use the (nearly) same query in in plain SQL, from Mysql workbench, it works as expected, sums the quantities for items which have "vevoiEntity" which is not null (so the reserved quantity).
vevoiEntity field in DB has int type, which can be null too.
The fields, called entity (except of course which used at FROM) are not entities, but simple integer. (previous developer had problems with naming too).
I've tried using more parentheses, tried " <> NULL" tried "vevoiEntity>0", etc but nothing helped, the case is always false, every other parts are perfect.
Why does it works differently in native SQL, and JPQL? I set hibernate.show_sql=true, and i saw that the generated query for the SUM CASE part, is just like the query that i used in workbench, so the translation seems to be ok.
plain SQL, (already working parts are left out) which works:
SELECT rk.cikk_id, SUM(CASE WHEN rk.vevoi_tetel_id IS NOT NULL THEN rk.mennyiseg_me_1 ELSE 0 END) as reserved FROM nast_raktarkeszlet rk GROUP BY rk.cikk_id;
or:
SELECT rk.cikk_id, CONCAT(SUM(rk.mennyiseg_me_1),' ', rk.me_1, ' / ', SUM(rk.mennyiseg_me_2),' ', rk.me_2, ' / ', SUM(rk.mennyiseg_me_3),' ', rk.me_3), CONCAT(AVG(rk.listaar), ' Ft'), CONCAT(rk.mennyiseg_me_1*rk.listaar, ' Ft'), rk.raktar_id, rk.cikk_id, SUM(CASE WHEN IFNULL(rk.vevoi_tetel_id, 0) >0 THEN rk.mennyiseg_me_1 ELSE 0 END) FROM nast_raktarkeszlet rk GROUP BY rk.cikk_id
generated query:
select
basecikkek1_.cikk_name as col_0_0_,
concat(sum(rak0_.mennyiseg_me_1),
' ',
men2_.unit_code,
' / ',
SUM(rak0_.mennyiseg_me_2),
' ',
men3_.unit_code,
' / ',
SUM(rak0_.mennyiseg_me_3),
' ',
men4_.unit_code) as col_1_0_,
concat(avg(rak0_.listaar),
' Ft') as col_2_0_,
concat(rak0_.mennyiseg_me_1*rak0_.listaar,
' Ft') as col_3_0_,
rak5_.raktar_name as col_4_0_,
vts7_.vat_code as col_5_0_,
sum(case
when rak0_.szallitolevel_tetel_id is not null
or rak0_.kimeno_tetel_id is not null
or rak0_.vevoi_tetel_id<>0 then rak0_.mennyiseg_me_1
else 0
end) as col_6_0_
from
nast_raktarkeszlet rak0_ cross
join
nast_cikkek basecikkek1_ cross
join
nast_vtsz vts7_ cross
join
nast_units men2_ cross
join
nast_units men3_ cross
join
nast_units men4_ cross
join
nast_raktarak rak5_
where
rak0_.cikk_id=basecikkek1_.id
and basecikkek1_.vtsz=vts7_.id
and rak0_.me_1=men2_.id
and rak0_.me_2=men3_.id
and rak0_.me_3=men4_.id
and rak0_.raktar_id=rak5_.id
and (
rak0_.raktar_id in (
? , ? , ? , ?
)
)
and rak0_.create_date<?
and (
rak0_.parentid like 'RB%'
or (
rak0_.parentid like 'BM%'
)
and rak0_.bevetel=1
)
group by
rak0_.cikk_id
OMG my bad. The WHERE conditions are exclude the records that have non null "vevoientity"
Related
I am trying to run the following SQL script using Java and am getting issues with no resultset from JDBCTemplate. I thought about reducing it using functions/stored procedures and would like some help with it:
SQL - first part:
SET NOCOUNT ON
IF OBJECT_ID('tempdb.dbo.#tempSearch', 'U') IS NOT NULL
DROP TABLE #tempSearch;
CREATE TABLE #tempSearch
(
ID INT,
Value VARCHAR(255)
)
INSERT INTO #tempSearch
VALUES (1, 'Variable1'), (2, 'Variabl2');
Second part:
WITH cte AS
(
SELECT
RoleID,
',' + REPLACE(REPLACE(GroupNames, ',', ',,'), ' ', '') + ',' GroupNames
FROM
UserGroup_Role_Mapping
), cte2 AS
(
SELECT
cte.RoleID,
REPLACE(cte.GroupNames, ',' + Value + ',', '') AS GroupNames,
s.ID, s.Value
FROM
cte
JOIN
#tempSearch s ON ID = 1
UNION ALL
SELECT
cte2.RoleID,
REPLACE(cte2.GroupNames, ',' + s.Value + ',', '') AS l,
s.ID, s.Value
FROM
cte2
JOIN
#tempSearch s ON s.ID = cte2.ID + 1
)
SELECT
a.Role, a.Sort_Order,
a.Parent, a.Parent_ID, a.Parent_URL,
a.Child, a.Child_ID,a.Child_URL
FROM
Config_View a
WHERE
a.Role IN (SELECT Name
FROM
(SELECT DISTINCT RoleID FROM cte2 WHERE LEN(GroupNames) = 0) tempRoles
JOIN
User_Role ON tempRoles.RoleID = User_Role.ID
)
DROP TABLE #tempSearch
I was thinking first part can be done in a stored procedure. I did read here (stored procedure with variable number of parameters) about making a table from a list of variables but am not sure how to do set those variables in a loop like i am doing from above (1,Variable1 etc.).
I think the second part can be by itself?
So my updated query might be:
Call stored procedure (variable1, ..., variablex);
SQL part 2?
If anyone can help that would be great!
It's possible to do this in two seperate batches, but only if you can ensure that the first batch runs in session scope, and not in a nested batch ( eg via sp_executesql ). Temp tables created in nested batches, like stored procedures or prepared statements are automatically destroyed at the end of the nested batch. So it depends on how you call it. My guess is that a PreparedStatement won't work.
The right way to do this is probably to use a stored procedure with a table-valued parameter, or a JSON (for SQL 2016+), or XML parameter and parses it in the stored procedure body. See https://learn.microsoft.com/en-us/sql/connect/jdbc/using-table-valued-parameters?view=sql-server-2017
You can also use a TSQL batch instead of a stored procedure and bind a Table-Valued Parameter, or a NVarchar(max) parameter containing JSON.
With a TVP you could simply use a batch like:
with s as (
select * from ? --bind a table-valued parameter here
), cte as (
select RoleID,','+replace(replace(GroupNames,',',',,'),' ','')+',' GroupNames from UserGroup_Role_Mapping
)
,cte2 as(
select cte.RoleID, replace(cte.GroupNames,','+Value+',','') as GroupNames, s.ID, s.Value
from cte
join s on ID=1
union all
select cte2.RoleID, replace(cte2.GroupNames,','+s.Value+',','') as l, s.ID ,s.Value
from cte2
join s on s.ID=cte2.ID+1
)
SELECT a.Role, a.Sort_Order, a.Parent, a.Parent_ID, a.Parent_URL, a.Child, a.Child_ID,a.Child_URL
FROM Config_View a
WHERE a.Role IN (
Select Name from (
Select distinct RoleID from cte2 where len(GroupNames)=0
) tempRoles
join User_Role
on tempRoles.RoleID = User_Role.ID
)
That would be the value of the string variable sql, and then call it something like:
SQLServerPreparedStatement pStmt = (SQLServerPreparedStatement) connection.prepareStatement(sql);
pStmt.setStructured(1, "dbo.CategoryTableType", sourceTVPObject);
ResultSet rs = stmt.executeQuery();
I have several SQL queries like this (about 10.000):
SELECT (COLUMN BODY)
FROM (TABLES BODY)
WHERE...
The where part is optional.
I need to extract the "COLUMN BODY" AND "TABLES BODY" from the query, but those are dynamic (I have found SQL functions and subqueries a lot)
Think as getting an vector with the divided query or something like that:
String result = [SELECT, "COLUMN BODY", FROM, "TABLES BODY", WHERE, ... ]
The purpose, I'm working in an "SQL translator" and I need extract those parts from that dynamic queries. I have tried string splitting and other but the result is so frustrating, so I'm looking for an alternative to achieve that. I'm working on a java project.
What is the best way to get that?
Edit
Those are examples of the queries:
SELECT COMPANIA, ANO, TIPO_COBRO, CODIGO, DESCRIPCION FROM SF_DESCRIPCIONES_ESTAND
SELECT AUT_FAMILIARES.COMPANIA, AUT_FAMILIARES.DCTO_EMPLEADO, AUT_FAMILIARES.SUCURSAL_EMPLEADO, AUT_FAMILIARES.IDENTIFICACION, AUT_FAMILIARES.SUCURSAL, AUT_FAMILIARES.CONSECUTIVO, AUT_FAMILIARES.DCTO_IDENTIDAD, AUT_FAMILIARES.PARENTESCO, AUT_FAMILIARES.NOMBRE, AUT_FAMILIARES.FECHANCTO, AUT_FAMILIARES.SEXO, AUT_FAMILIARES.OBSERVACIONES, AUT_FAMILIARES.POLIZA, AUT_FAMILIARES.SALUD, AUT_FAMILIARES.PORCENTAJE, AUT_FAMILIARES.OCUPACION, AUT_FAMILIARES.TELEFONO, AUT_FAMILIARES.POS, AUT_FAMILIARES.AUXILIO_EDUCATIVO, AUT_FAMILIARES.APELLIDO1, AUT_FAMILIARES.APELLIDO2, AUT_FAMILIARES.DIRECCION, AUT_FAMILIARES.ESTADO_ACTUAL, AUT_FAMILIARES.DCTO_IDENTIDAD_A, AUT_FAMILIARES.PARENTESCO_A, AUT_FAMILIARES.NOMBRE_A, AUT_FAMILIARES.SEXO_A, AUT_FAMILIARES.OBSERVACIONES_A, AUT_FAMILIARES.POLIZA_A, AUT_FAMILIARES.SALUD_A, AUT_FAMILIARES.PORCENTAJE_A, AUT_FAMILIARES.OCUPACION_A, AUT_FAMILIARES.TELEFONO_A, AUT_FAMILIARES.POS_A, AUT_FAMILIARES.AUXILIO_EDUCATIVO_A, AUT_FAMILIARES.APELLIDO1_A, AUT_FAMILIARES.APELLIDO2_A, AUT_FAMILIARES.DIRECCION_A, AUT_FAMILIARES.ESTADO_ACTUAL_A, AUT_FAMILIARES.ESTADO, AUT_FAMILIARES.DESTINATARIO, AUT_FAMILIARES.SUCURSAL_DESTINATARIO, AUT_FAMILIARES.TIPO_SOLICITUD, AUT_FAMILIARES.FECHA_APROBACION, AUT_FAMILIARES.ENVIADO, AUT_FAMILIARES.ACTUALIZADO, AUT_FAMILIARES.RUTA, AUT_FAMILIARES.CREATED_BY, AUT_FAMILIARES.DATE_CREATED, AUT_FAMILIARES.MODIFIED_BY, AUT_FAMILIARES.DATE_MODIFIED, AUT_FAMILIARES.N_IDENTIFICACION, AUT_FAMILIARES.EDAD, AUT_FAMILIARES.EDAD_A FROM AUT_FAMILIARES
SELECT DISTINCT CARGOS.ID_DE_CARGO, CARGOS.NOMBRE_DEL_CARGO FROM EV_EVALUADOR_EVALUADO EVAL INNER JOIN CARGOS ON EVAL.COMPANIA = CARGOS.COMPANIA AND EVAL.CARGO_EVALUADO = CARGOS.ID_DE_CARGO WHERE EVAL.COMPANIA = :COMPANIA AND EVAL.CLASE_EVALUACION = :CLASEEVALUACION AND CARGOS.ID_DE_CARGO >= :CODIGO ORDER BY CARGOS.ID_DE_CARGO
SELECT ROWNUM CODIGO, DECODE (ROWNUM, 1,'Activo',2,'Pensionado',3,'Retirado',4,'Suspensión Preventiva',5,'Activarlo para Ajustes Liquidación',6,'Comisión',7,'Encargos')NOMBRE FROM DUAL CONNECT BY ROWNUM < 8
SELECT * FROM ( SELECT A.*, ROWNUM RNUM FROM(SELECT * FROM (SELECT INVENTARIO.CODIGOELEMENTO, INVENTARIO.NOMBRELARGO FROM INVENTARIO WHERE INVENTARIO.COMPANIA = :COMPANIA AND INVENTARIO.TIPO NOT IN ('C') ORDER BY INVENTARIO.COMPANIA, INVENTARIO.CODIGOELEMENTO) WHERE (CASE WHEN CODIGOELEMENTO IS NULL THEN ' ' ELSE UPPER(CODIGOELEMENTO) END) LIKE UPPER(:CODIGOELEMENTO || '%') AND (CASE WHEN NOMBRELARGO IS NULL THEN ' ' ELSE UPPER(NOMBRELARGO) END) LIKE UPPER( '%' || :NOMBRELARGO || '%') ) A WHERE ROWNUM <= (:PAGINICIO + :PAGTAMANIO)) WHERE RNUM > :PAGINICIO
I am trying to update a simple row using Query Annotation with JPA to Oracle 11g
This is my code:
#Transactional()
#Modifying
#Query(value="UPDATE Irregularities SET IRREST_ID = 0 WHERE IRREGS_ID = 1006", nativeQuery = true)
int updateState();
This was working fine but for some reason is not working anymore. No error, just hangs.
If I try to run the same query with Oracle SQL Developer in the same BDD works fine.
Could be a lock table problem? Why works in SQL Developer but not in Springboot?
Thank you very much for any help that you can provide.
You can find locks with that query (run it as sysdba)
select
(select username || ' - ' || osuser || ' - ' || machine from v$session where sid=a.sid) blocker,
a.sid || ', ' ||
(select serial# from v$session where sid=a.sid) sid_serial,
' is blocking ',
(select username || ' - ' || osuser || ' - ' || machine from v$session where sid=b.sid) blockee,
b.sid || ', ' ||
(select serial# from v$session where sid=b.sid) sid_serial
from v$lock a, v$lock b
where a.block = 1
and b.request > 0
and a.id1 = b.id1
and a.id2 = b.id2;
I'm using SQL Case in my select and in group by clause and I'm working in JAVA. Whenever I execute my java program it says:
Column 'dbo.JOHN_Dashboard.Log_Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
My Query is:
SELECT EP.Site_Code AS [Site_Code], DB.[Site] AS [Site], DB.[Utility] AS [Utility],
CASE ? WHEN 'Raw' THEN dateadd(mi,datediff(mi,0,DB.[log_date]),0)
WHEN 'Hour' THEN dateadd(hh,datediff(hh,0,DB.[log_date]),0)
WHEN 'Day' THEN dateadd(dd,datediff(dd,0,DB.[log_date]),0)
WHEN 'Week' THEN dateadd(wk,datediff(wk,0,DB.[log_date]),0)
WHEN 'Month' THEN dateadd(mm,datediff(mm,0,DB.[log_date]),0)
WHEN 'Year' THEN dateadd(yy,datediff(yy,0,DB.[log_date]),0)
ELSE DB.[log_date]
END AS [log_date],
SUM(CASE WHEN DB.[value] >= 0 THEN DB.[value] ELSE 0 END) AS [value],
SUM(CASE WHEN DB.[Cost] >=0 THEN DB.[cost] ELSE 0 END) AS [Cost],
SUM(CASE WHEN DB.[CO2] >=0 THEN DB.[CO2] ELSE 0 END) AS [CO],
MT.[Meter_type_name] AS [Meter Type],
MN.[Meter_Name] AS [Meter Name],
U.[Unit_Name] AS [Units],
EP.EnergyPoint_ID AS [Meter_ID],
EP.Parent_ID AS [Parent],
EP.Meter_Description AS [Meter_Description]
FROM [dbo].[JOHN_Dashboard] DB
INNER JOIN [dbo].[EnergyPoints] EP ON DB.[EnergyPoint_ID] = EP.[EnergyPoint_ID]
INNER JOIN [dbo].[Meter_Types] MT ON MT.[Meter_Type_ID] = EP.[Meter_Type_ID]
INNER JOIN [dbo].[Meter_Names] MN ON MN.[Meter_Name_ID] = EP.[Meter_Name_ID]
INNER JOIN [dbo].[Units] U ON U.[Unit_ID] = EP.[Unit_id]
WHERE [log_date] >= ? AND [Log_Date] < DATEADD(DAY, 1, ?)
AND ( ? IS NULL OR EP.Energypoint_ID = ?)
GROUP BY EP.Site_Code, DB.[Site], DB.[Utility], MT.[Meter_type_name],
MN.[Meter_Name], U.[Unit_Name], EP.[EnergyPoint_ID],
EP.[Parent_ID], EP.[Meter_Description],
CASE ? WHEN 'Raw' THEN dateadd(mi,datediff(mi,0,DB.[log_date]),0)
WHEN 'Hour' THEN dateadd(hh,datediff(hh,0,DB.[log_date]),0)
WHEN 'Day' THEN dateadd(dd,datediff(dd,0,DB.[log_date]),0)
WHEN 'Week' THEN dateadd(wk,datediff(wk,0,DB.[log_date]),0)
WHEN 'Month' THEN dateadd(mm,datediff(mm,0,DB.[log_date]),0)
WHEN 'Year' THEN dateadd(yy,datediff(yy,0,DB.[log_date]),0)
ELSE DB.[log_date] END ;
The parameters i'm passing are:
'Week'
'2016-05-16'
'2016-05-22'
6044
6044
'Week'
Note: This query runs without error in SQL Management Studio.
As requested here is a reworked version of your code using a sub-query before grouping. Since I don't have your database I can't guarantee that I have everything exactly right but give this a try.
I recommend always using a sub-query when your group by has complicated logic that will be repeated in the select. Some people would probably drop the second criteria and just say whenever the group by has complicated logic.
SELECT sub.Site_Code, sub.[Site], sub.[Utility], sub.[Meter Type],
sub.[log_date],
SUM(sub.[value]) as [value],
SUM(sub.[Cost]) as [cost],
SUM(sub.[CO]) as [CO],
sub.[Meter Name], sub.[Units], sub.[Meter_ID],
sub.[Parent], sub.[Meter_Description]
FROM (
SELECT EP.Site_Code AS [Site_Code], DB.[Site] AS [Site], DB.[Utility] AS [Utility],
CASE ? WHEN 'Raw' THEN dateadd(mi,datediff(mi,0,DB.[log_date]),0)
WHEN 'Hour' THEN dateadd(hh,datediff(hh,0,DB.[log_date]),0)
WHEN 'Day' THEN dateadd(dd,datediff(dd,0,DB.[log_date]),0)
WHEN 'Week' THEN dateadd(wk,datediff(wk,0,DB.[log_date]),0)
WHEN 'Month' THEN dateadd(mm,datediff(mm,0,DB.[log_date]),0)
WHEN 'Year' THEN dateadd(yy,datediff(yy,0,DB.[log_date]),0)
ELSE DB.[log_date]
END AS [log_date],
CASE WHEN DB.[value] >= 0 THEN DB.[value] ELSE 0 END AS [value],
CASE WHEN DB.[Cost] >=0 THEN DB.[cost] ELSE 0 END AS [Cost],
CASE WHEN DB.[CO2] >=0 THEN DB.[CO2] ELSE 0 END AS [CO],
MT.[Meter_type_name] AS [Meter Type],
MN.[Meter_Name] AS [Meter Name],
U.[Unit_Name] AS [Units],
EP.EnergyPoint_ID AS [Meter_ID],
EP.Parent_ID AS [Parent],
EP.Meter_Description AS [Meter_Description]
FROM [dbo].[JOHN_Dashboard] DB
INNER JOIN [dbo].[EnergyPoints] EP ON DB.[EnergyPoint_ID] = EP.[EnergyPoint_ID]
INNER JOIN [dbo].[Meter_Types] MT ON MT.[Meter_Type_ID] = EP.[Meter_Type_ID]
INNER JOIN [dbo].[Meter_Names] MN ON MN.[Meter_Name_ID] = EP.[Meter_Name_ID]
INNER JOIN [dbo].[Units] U ON U.[Unit_ID] = EP.[Unit_id]
WHERE [log_date] >= ? AND [Log_Date] < DATEADD(DAY, 1, ?)
AND ( ? IS NULL OR EP.Energypoint_ID = ?)
) sub
GROUP BY sub.Site_Code, sub.[Site], sub.[Utility], sub.[Meter Type],
sub.[Meter Name], sub.[Units], sub.[Meter_ID],
sub.[Parent], sub.[Meter_Description], sub.[log_date];
I'm facing problems with Primary Key(PK) on my Android x Web Server system, given that the user can create new Objects on the client-side and Off-line.
These objects will be sent to server when the user decides to.
This synchronization caused lots of Foreign Key(FK) and PK issues, because my database uses Auto-increment PK.
To solve these integrity issues, after some search, I've decided that the simplest way was to use UUID as my PK in both Android Client and Java Server.
David Bélanger's answer about UUID and index helped a lot.
Now I have 1 question and 1 problem:
Question: Is this an acceptable solution to my problem?. If its not.. could give me an alternative? (Realtime database seems to be a hard work to do with my system)
Problem: Given that I already have a production database and I can't start over, how can I change the PK from integer unsigned to binary(16).
My final select statement needs to be like that this:
select id from table where id = hex('any ID')
Because I'll start inserting data like this:
insert into table(id) values (unhex(replace(uuid(), '-', '')));
I tried update id using
* unhex(id)
* conv(id, 10, 16)
What am I missing?
EDIT:
Using thease scripts, I was able to recreate all IDs and Foreign keys
select concat ('alter table ', TABLE_NAME, ' drop foreign key ', CONSTRAINT_NAME, ';') from information_schema.KEY_COLUMN_USAGE where table_schema = 'my_table_name' and CONSTRAINT_NAME like 'fk%'
select concat('alter table ', TABLE_NAME, ' modify ', COLUMN_NAME, ' binary(16) ', IF (IS_NULLABLE = 'NO', 'not null', ''), ';') from information_schema.columns where TABLE_SCHEMA = 'my_table_name' and COLUMN_KEY in ('PRI', 'MUL') and COLUMN_NAME like 'id%'
select concat ('alter table ', TABLE_NAME, ' add constraint ', CONSTRAINT_NAME, ' foreign key (', COLUMN_NAME, ') references ', REFERENCED_TABLE_NAME, ' (', REFERENCED_COLUMN_NAME, ') on update cascade;') from information_schema.KEY_COLUMN_USAGE where table_schema = 'my_table_name' and CONSTRAINT_NAME like 'fk%'
select concat('update ', TABLE_NAME, ' set ', COLUMN_NAME, ' = unhex(replace(uuid(), ''-'', ''''));') from information_schema.COLUMNS where TABLE_SCHEMA = 'my_table_name' and md5(COLUMN_NAME) = md5('id')
select concat ('alter table ', TABLE_NAME, ' drop foreign key ', CONSTRAINT_NAME, ';') from information_schema.KEY_COLUMN_USAGE where table_schema = 'my_table_name' and CONSTRAINT_NAME like 'fk%'
select concat ('alter table ', TABLE_NAME, ' add constraint ', CONSTRAINT_NAME, ' foreign key (', COLUMN_NAME, ') references ', REFERENCED_TABLE_NAME, ' (', REFERENCED_COLUMN_NAME, ');') from information_schema.KEY_COLUMN_USAGE where table_schema = 'my_table_name' and CONSTRAINT_NAME like 'fk%'
Problem solved.