Hi all!
I'm having an issue with Advanced SQL.
I have the query below that works perfectly with SQL Sandbox, but then when I try to configure an Advanced SQL I get this error, and have tried a lot of things without success...
If you can give some light, I appreciate! :)
WITH RankedPedidos AS (
SELECT DISTINCT
{Habitacao}.[Id],
CONCAT({Morada}.[CP1], '-', {Morada}.[CP2]) AS CodigoPostal,
{Habitacao}.[CodigoLocalConsumo],
{Morada}.[Rua],
{Habitacao}.[NumPolicia],
{Habitacao}.[NumFraccao],
{Habitacao}.[DDLId],
{TipoPedido}.[Designacao] AS TipoDePedido,
{Estado}.[Data] AS DataUltimoEstado,
CASE WHEN {Ilhas}.[CodigoPostal] IS NOT NULL THEN 'Sim' ELSE 'Não' END AS Ilha,
ROW_NUMBER() OVER (
PARTITION BY {Habitacao}.[ID]
ORDER BY
CASE
WHEN {TipoPedido}.[ID] = 7 AND {TipoEstado2}.[ID] = 2 AND {Estado}.[Data] >= DATEADD(DAY, -60, GETDATE()) THEN 1
WHEN {TipoPedido}.[ID] = 8 AND {TipoEstado2}.[ID] = 2 AND {Estado}.[Data] >= DATEADD(DAY, -180, GETDATE()) THEN 2
ELSE 3
END,
{Estado}.[Data] DESC
) AS RowNum
FROM
{Habitacao}
LEFT JOIN
{Pedido} ON {Habitacao}.[ID] = {Pedido}.[HabitacaoID]
{TipoPedido} ON {Pedido}.[TipoPedidoID] = {TipoPedido}.[ID]
{Estado} ON {Pedido}.[ID] = {Estado}.[PedidoID]
{TipoEstado2} ON {Estado}.[TipoEstado2ID] = {TipoEstado2}.[ID]
{Morada} ON {Habitacao}.[MoradaID] = {Morada}.[ID]
{Ilhas} ON CONCAT({Morada}.[CP1], '-', {Morada}.[CP2]) = {Ilhas}.[CodigoPostal] AND {Habitacao}.[NumPolicia] = {Ilhas}.[NumPolicia]
WHERE
(
({Pedido}.[TipoPedidoID] = 7 AND {TipoEstado2}.[ID] = 2 AND {Estado}.[Data] >= DATEADD(DAY, -60, GETDATE()) AND {Habitacao}.[DDLId] = 3)
OR
({Pedido}.[TipoPedidoID] = 8 AND {TipoEstado2}.[ID] = 2 AND {Estado}.[Data] >= DATEADD(DAY, -180, GETDATE()) AND {Habitacao}.[DDLId] = 3)
({Ilhas}.[CodigoPostal] IS NOT NULL AND {Pedido}.[TipoPedidoID] = 7 AND {TipoEstado2}.[ID] = 2 AND {Habitacao}.[DDLId] = 3)
)
SELECT
Id,
CodigoPostal,
CodigoLocalConsumo,
Rua,
NumPolicia,
NumFraccao,
DDLId,
TipoDePedido,
DataUltimoEstado,
Ilha
FROM RankedPedidos
WHERE RowNum = 1
ORDER BY TipoDePedido, DataUltimoEstado ASC;
Ok got it as you can see in screenshot your query returning 9 columns but in your structure having only 8 fields so mapping going wrong
DataUltimoEstado is on 7th place and in query 7th place is DDLId which is integer
Hi Diogo,
the value of the int64 field is the seconds and it is not null, that field is seconds in unix time.
What I recommend (don't be angry with me please), is to build that statement line by line, and I think it will be simpler to you to resolve that issue.
We are here waiting for your feedback
Regards
Hi,
The issue arises because OutSystems Advanced SQL has specific syntax rules and restrictions compared to SQL Sandbox or direct database query execution.
Here’s a simplified and potentially corrected version:
{Morada}.[CP1] + '-' + {Morada}.[CP2] AS CodigoPostal,
WHEN {Ilhas}.[CodigoPostal] IS NOT NULL THEN 'Sim'
ELSE 'Não'
END AS Ilha,
LEFT JOIN {Pedido} ON {Habitacao}.[ID] = {Pedido}.[HabitacaoID]
LEFT JOIN {TipoPedido} ON {Pedido}.[TipoPedidoID] = {TipoPedido}.[ID]
LEFT JOIN {Estado} ON {Pedido}.[ID] = {Estado}.[PedidoID]
LEFT JOIN {TipoEstado2} ON {Estado}.[TipoEstado2ID] = {TipoEstado2}.[ID]
LEFT JOIN {Morada} ON {Habitacao}.[MoradaID] = {Morada}.[ID]
LEFT JOIN {Ilhas} ON {Morada}.[CP1] + '-' + {Morada}.[CP2] = {Ilhas}.[CodigoPostal]
AND {Habitacao}.[NumPolicia] = {Ilhas}.[NumPolicia]
Thank,
Vipin Yadav
HI,The error "Invalid cast from 'Int64' to 'DateTime'" in OutSystems usually arises because the Advanced SQL query is returning a value that the platform expects as a DateTime but is instead interpreted as a different type (e.g., Int64).
Check the Data Types in the Aggregate:
there are couple of possibilities
1. check your input parameters datatype - one of those assignment is creating this problem
2. check your output structure - one of those assignment is creating this problem
3. comparison between 2 data type is wrong - like {Estado}.[Data] >= DATEADD(DAY, -60, GETDATE())
if {Estado}.[Data] is integer than you will get error
can you provide oml or more detail about the issue so it is easy to understand the problem.
what is {Estado}.[Data] datatype ?
Good morning ;)
I'll try to show everything here...
Structure:
Entity:
New short query to test, working fine in SQL Sandbox:
Same new query on Advanced SQL, without any inputs, but with the same error:
And that's all :)
this is missing in screenshot. Is it datetime ?
you have to match the query column sequence with your output structure. Remove the columns from query which are not in your output structure and check.
That's it man! Sorry, I was looking for everything except this :/ Thank you!!
The sandbox didn't need any structure to map it just display your query output in the table.
But OutSystems need a structure and it follows the sequence of the structure to map the query result to the output structure and also check the datatype of structure.
So assume that if you have datetime field in your output structure as a first field but your query output have first column as integer then you will get the error.
Hi @Diogo Barbosa ,
the backend is the same one right? Both statements target the same DB?