Cannot enclose a query with an outer query in Firebird 2.5 - sql

My inner query is returning results but I get this error
SQL Error [336397221] [42000]: Dynamic SQL Error; SQL error code =
-104; Invalid command; column NOME was specified multiple times for derived table EX [SQLState:42000, ISC error code:336397221]
Here is my query
select * from ( --this gives error
SELECT t.placa, m.nome, GA.ID_AUDESP, GA.NOME AS GRUPO_AUDESP,
T.VALOR as valor,
T.DT_AQUISICAO,
PS.NOME
FROM PATRIMONIO_TOMBAMENTO T
LEFT JOIN PATRIMONIO_GRUPO_AUDESP GA ON GA.ID_GRUPO_AUDESP = T.ID_GRUPO_AUDESP
LEFT JOIN ESTOQUE_MATERIAL M ON M.ID_MATERIAL = T.ID_MATERIAL
LEFT JOIN PATRIMONIO_SETOR PS ON (T.ID_SETOR = PS.ID_SETOR)
WHERE T.ID_ORGAO = '030000'
AND (T.SITUACAO IN('A') or ( T.SITUACAO = 'B' AND T.DT_BAIXA >'2022-01-31'))
AND (T.DT_REATIVADO IS NULL OR T.DT_REATIVADO<= '2022-01-31' or (T.DT_BAIXA >'2022-01-31'))
AND T.dt_cadastro <= '2022-01-31'
) ex

Your derived table has two columns with the name/alias NOME (m.nome and PS.NOME), and as the error indicates, this is not allowed as a name should occur only once. You need to alias one or both columns so they have unique names, or exclude one of them if it would have the same value. The same was already done for the third occurrence of NOME, GA.NOME.
For example:
select * from (
SELECT t.placa,
m.nome as m_nome,
GA.ID_AUDESP,
GA.NOME AS GRUPO_AUDESP,
T.VALOR as valor,
T.DT_AQUISICAO,
PS.NOME as ps_nome
FROM PATRIMONIO_TOMBAMENTO T
-- ...

Related

ORA-00918: column ambiguous defined when joining subquery table

The following code works:
SELECT b.AAZ002
FROM
AE02 b
INNER JOIN t_aa10 aa10
ON (aa10.AAA100 = 'AA5515' AND b.AA5515 = aa10.AAA102)
But when I change b to a subquery table, it reports ambigious column error:
SELECT b.AAZ002
FROM
(
select count(1) count1, a.BAZ379 , max(a.AB0111) AB0111, max(a.AA5515) AA5515, max(a.AAE011) AAE011
, max(a.AAE035) AAE035, max(a.AAE036) AAE036, max(a.AAE012) AAE012, max(a.AAE012) AAE012
, max(a.ZA0100) ZA0100
from AE02 a
where a.BAE028 = '1'
group by a.BAZ379
) b
INNER JOIN t_aa10 aa10
ON (aa10.AAA100 = 'AA5515' AND b.AA5515 = aa10.AAA102)
Dbeaver reported these 3 errors:
Description Resource Path Location Type
SQL Error [918] [42000]: ORA-00918: column ambiguously defined
Script-8.sql /General/Scripts Unknown Database Script Problem
SQL Error [933] [42000]: ORA-00933: SQL command not properly ended
Script-8.sql /General/Scripts Unknown Database Script Problem
SQL Error [979] [42000]: ORA-00979: not a GROUP BY expression
Script-8.sql /General/Scripts Unknown Database Script Problem
I have prefix on every column, it should not be ambiguous. What could be the cause?
I have corrected the sql after astentx and YoYo pointed out errors. Here is the corrected sql:
SELECT b.AAZ002
FROM
(
select count(1) count1, a.BAZ379 , max(a.AB0111) AB0111, max(a.AA5515) AA5515, max(a.AAE011) AAE011
, max(a.AAE035) AAE035, max(a.AAE036) AAE036, max(a.AAE012) AAE012, max(AAZ002) AAZ002
, max(a.ZA0100) ZA0100
from AE02 a
where a.BAE028 = '1'
group by a.BAZ379
) b
INNER JOIN t_aa10 aa10
ON (aa10.AAA100 = 'AA5515' AND b.AA5515 = aa10.AAA102)
INNER JOIN AB01 ab
ON ab.ZA0100 = b.ZA0100

Access nested select in field listing

I have been provided a query to search for some data in the db that looks like this:
select nvl(to_char(a.empresa), 'SEM REMESSA') empresa,
c.conta,
nvl(sum(a.valor_total), 0) as folha,
(select count(distinct(l2.creditado))
from lancamentos l2
inner join vw_arquivos a2
on l2.arquivo = a2.arquivo
where a2.empresa = a.empresa) as funcionarios
from conta_table c
left join vw_arquivos a
on c.conta = a.conta
and a.situacao = 6
group by a.empresa, c.conta
I have to get the value of the operation folha / funcionarios, but if I put this in the field listing I get the error:
ORA-00904: "FUNCIONARIOS": invalid identifier
00904. 00000 - "%s: invalid identifier"
And when I replace my reference to funcionarios with the same nested select, I get the same error on reference folha.
I know I could just replace both references with their respective calculations, and it works. Like this:
nvl(sum(a.valor_total), 0) /
(select count(distinct(l2.creditado))
from lancamentos l2
inner join vw_arquivos a2
on l2.arquivo = a2.arquivo
where a2.empresa = a.empresa) as division
But this looks not so optimized and not like the best way to do it. So my question is...
Is there any way to select these values with this last division operation, but more optimized than repeating the folha and funcionarios calculations? Maybe a way to reference these already calculated values.
folha and funcionarios cannot be referenced in the same query because they are aliases. You have to make the entire query as subquery, and get the division, like so:
select empresa, conta, folha/funcionarios folha_div_funcionarios
from (
select nvl(to_char(a.empresa), 'SEM REMESSA') empresa,
c.conta,
nvl(sum(a.valor_total), 0) as folha,
(select count(distinct(l2.creditado))
from lancamentos l2
inner join vw_arquivos a2
on l2.arquivo = a2.arquivo
where a2.empresa = a.empresa) as funcionarios
from conta_table c
left join vw_arquivos a
on c.conta = a.conta
and a.situacao = 6
group by a.empresa, c.conta
)

Query fail Oracle but working in SQL Server

I have a query that runs perfectly n SQL server but in Oracle, I get the following error:
ORA-00923: FROM keyword not found where expected
SELECT
MyApps.AppConfigurationId,
MyApps.GUID,
MyApps.AppName,
MyApps.BinaryData,
MyApps.Color1,
MyApps.UserCreatorName,
MyApps.CreatedAt,
MyApps.UserUpdaterName,
MyApps.UpdatedAt,
MyApps.UserPublisherName,
MyApps.LastPublishedAt,
MyApps.AppConfigStateId,
MyApps.AppConfigStateLabel
FROM
(
SELECT
ROW_NUMBER() OVER (
PARTITION BY "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID"
ORDER BY LastestPublishInfo."PUBLISHEDAT" DESC
) AS RowNum,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" AS AppConfigurationId,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."GUID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."NAME" AS AppName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."BINARYDATA",
"OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."COLOR1",
UserCreator."NAME" AS UserCreatorName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDAT",
UserUpdater."NAME" AS UserUpdaterName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDAT",
LastestPublishInfo.PublisherName AS UserPublisherName ,
LastestPublishInfo."PUBLISHEDAT" AS LastPublishedAt,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."LABEL" AS AppConfigStateLabel
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266" JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."ID" AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."PARENTAPPCONFIGID" IS NULL AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ISACTIVE" = 1
)
JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserCreator
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserUpdater
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."COLORPALETTEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."ID")
LEFT JOIN
(
SELECT "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."APPCONFIGURATIONID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT",
UserPublisher."NAME" AS PublisherName
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16" LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserPublisher
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDBY" = UserPublisher."ID")
WHERE "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT" IS NOT NULL
AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."ISACTIVE" = 0
) LastestPublishInfo
ON (LastestPublishInfo.AppConfigurationId = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."APPCONFIGURATIONID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" )
) MyApps
WHERE MyApps.RowNum = 1
ORDER BY
MyApps.UpdatedAt DESC
I already tried to check syntax follow some articles but I'm not able to solve it.
Added " around de the aliases, tried to replace attribute names in the subquery not to match the parent but nothing worked.
Hope you could help me with this since I went to the point I have no clue about what to try next!
Cheers
Using the following web: https://rextester.com/l/oracle_online_compiler
I tryed your SQL and got the same error as you.
I then started simplifing it up to:
SELECT 1 AS RowNum
FROM blah
and still getting the same error.
I think RowNum is a reserved word in Oracle, you can not use it as a field alias.

difference in two tables Syntax error in SQL FROM Clause

I am trying to compare two tables with different columns.
I tried below code but it is giving syntax error
Syntax error:
"Exception thrown by code stage: Syntax error in FROM clause"
SELECT [Sheet1].[ID], [Sheet2].[ID_EXT] from [Sheet1], [Sheet2]
A As (SELECT [Sheet1].[ID], ([Sheet1].[Email] + ';' + [Sheet2].[Long Email]) as email from [Sheet1] inner join [Sheet2]
on [Sheet1].[ID] = FORMAT([Sheet2].[ID_EXT],'00000000000')
WHERE [Sheet2].[Type] = 3 AND UCase [Sheet1].[Email] <> UCase [Sheet2].[Long Email])
B As (SELECT [Sheet1].[ID], ([Sheet1].[Batchcode] + ';' + str([Sheet2].[Code])) as Code from [Sheet1] inner join [Sheet2] on [Sheet1].[ID] = FORMAT([Sheet2].[ID_EXT],'00000000000') WHERE [Sheet2].[Type]= 3 AND [Sheet1].[Batchcode]<>FORMAT([Sheet2].[Code],'0000))
SELECT [A].[ID], [A].[Email], [B].[Batchcode] from [A] Full outer join [A] ON [A].[ID]=[B].[ID_EXT]
Instead of A as (select...) you need , (select ... ) as A.

Multi level GROUP BY clause not allowed

I have the following CrossTab query
TRANSFORM Max(VWDRSSTA.DATUM_ZEIT) AS MaxOfDATUM_ZEIT
SELECT VWDRSSTA.ANTRAGSNUMMER
,IIF(VWDRSSTA.SYSTEM = 'VS', (
SELECT (Max(VWDRSSTA.DUNKEL)) AS d
FROM VWDRSSTA
), NULL) AS Dunkel
,Max(VWDRSSTA.VERS_NR_INT) AS Versicherungsnummer
FROM VWDRSSTA
INNER JOIN V_NAMES ON (VWDRSSTA.SYSTEM = V_NAMES.SYSTEM_CODE)
AND (VWDRSSTA.EREIGNIS = V_NAMES.EREIGNIS)
GROUP BY VWDRSSTA.ANTRAGSNUMMER
ORDER BY VWDRSSTA.ANTRAGSNUMMER
PIVOT V_NAMES.MAPPED_NAME;
which gives me the error "Multi-level GROUP BY clause is not allowed in a subquery". Where am I going wrong with the code?
Try with VWDRSSTA.SYSTEM in the GROUP BY clause.
It should do the trick.
Edit to detail my answer :
TRANSFORM Max(VWDRSSTA.DATUM_ZEIT) AS MaxOfDATUM_ZEIT
SELECT VWDRSSTA.ANTRAGSNUMMER
,IIF(VWDRSSTA.SYSTEM = 'VS', (
SELECT (Max(VWDRSSTA.DUNKEL)) AS d
FROM VWDRSSTA
), NULL) AS Dunkel
,Max(VWDRSSTA.VERS_NR_INT) AS Versicherungsnummer
FROM VWDRSSTA
INNER JOIN V_NAMES ON (VWDRSSTA.SYSTEM = V_NAMES.SYSTEM_CODE)
AND (VWDRSSTA.EREIGNIS = V_NAMES.EREIGNIS)
GROUP BY VWDRSSTA.ANTRAGSNUMMER, VWDRSSTA.SYSTEM
ORDER BY VWDRSSTA.ANTRAGSNUMMER
PIVOT V_NAMES.MAPPED_NAME;
You cross-tab query contains a secondary "aggregated" SQL and this is not allowed in ACCESS Cross-tab query.
change the Select max(vwdrssta.dunkel) ... part to DMax("dunkel", "vwdrssta") which will get rid of that error message.