difference in two tables Syntax error in SQL FROM Clause - sql

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.

Related

Cannot enclose a query with an outer query in Firebird 2.5

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
-- ...

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

Syntax error on CASE Statement join in subquery

I have the below query and subquery where I am getting a SUM value, and I want to join PS_VOUCHER_LINE LineSub conditionally (on Line.LINE_NBR = LineSub.LINE_NBR) on PS_VOUCHER Line only when a VOUCHER_ID has more than 1 LINE_NBR, otherwise I don't want this last join condition to execute. I am getting a Syntax error on the statement (Incorrect syntax near '='.) . How can I get this conditional join to work properly?
SELECT
CONCAT(Header.BUSINESS_UNIT, Header.VOUCHER_ID) AS INVOICE_ID
,
(
SELECT SUM(LineSub.MERCHANDISE_AMT)
FROM PS_VOUCHER_LINE LineSub
WHERE Line.BUSINESS_UNIT = LineSub.BUSINESS_UNIT
AND Line.VOUCHER_ID = LineSub.VOUCHER_ID
AND
CASE
WHEN COUNT(Line.LINE_NBR) > 1 THEN Line.LINE_NBR = LineSub.LINE_NBR
END
GROUP BY LineSub.VOUCHER_ID
) + Header.FREIGHT_AMT + Header.SALETX_AMT AS GROSS_AMT_LINE_FREIGHT_TAX
FROM
PS_VOUCHER Header
INNER JOIN PS_VOUCHER_LINE Line ON Line.BUSINESS_UNIT = Header.BUSINESS_UNIT
AND Line.VOUCHER_ID = Header.VOUCHER_ID
WHERE
Header.VOUCHER_ID = '00241107'
just remove case and add add having where condition:
GROUP BY LineSub.VOUCHER_ID HAVING COUNT(Line.LINE_NBR) > 1

I am converting Oracle queries to Standard Bigquery, i am gettting error "IN subquery is not supported inside join predicate."

I have converted oracle query into below standard bq but in last statement(IN subquery). I am getting error:
"IN subquery is not supported inside join predicate."
Please advise how to use IN subquery in bq in the below code
#Last part of the code
INNER JOIN (
SELECT
DISTINCT `domain-rr.oracle_DB_DB.he_project_assoc`.PARENT_ISBN
PARENT_ISBN,
SUM (`domain-rr.DB_RPT.PROJECT_GR_QTY`.GR_QTY) GR_QTY
FROM
`domain-rr.oracle_DB_DB.he_project_assoc`
INNER JOIN
`domain-rr.DB_RPT.PROJECT_GR_QTY`
ON
`domain-rr.oracle_DB_DB.he_project_assoc`.child_ISBN = `domain-
rr.DB_RPT.PROJECT_GR_QTY`.BIC_GCISBN
AND `domain-rr.oracle_DB_DB.he_project_assoc`.BREAK_LABEL <>
'Associated ISBNs'
GROUP BY
`domain-rr.oracle_DB_DB.he_project_assoc`.PARENT_ISBN) xx
ON
yy.PARENT_ISBN = xx.PARENT_ISBN
AND yy.CIRCULATION_INT < xx.GR_QTY
AND yy.PARENT_ISBN IN
( SELECT
DISTINCT _BIC_GCISBN
FROM
`domain-rr.DB_RPT.BIC_GM_AGCPOAODS00_BO_VW`
INNER JOIN
`domain-rr.oracle_DB_boadmin.fiscal_bo`
ON
_BIC_ZC2GRIRIN = 'G'
AND _BIC_ZCLOEKZ = ' '
AND SUBSTR (BOUND_DATE, 1, 6) = `domain-
rr.oracle_DB_boadmin.fiscal_bo`.PRIOR_FISC_YEAR_MONTH )
Can you try like this:
Select * from (
#Last part of the code
INNER JOIN (
SELECT
DISTINCT `pearson-rr.oracle_grdw_grdw.he_project_assoc`.PARENT_ISBN
PARENT_ISBN,
SUM (`pearson-rr.GRDW_RPT.PROJECT_GR_QTY`.GR_QTY) GR_QTY
FROM
`pearson-rr.oracle_grdw_grdw.he_project_assoc`
INNER JOIN
`pearson-rr.GRDW_RPT.PROJECT_GR_QTY`
ON
`pearson-rr.oracle_grdw_grdw.he_project_assoc`.child_ISBN = `pearson-
rr.GRDW_RPT.PROJECT_GR_QTY`.BIC_GCISBN
AND `pearson-rr.oracle_grdw_grdw.he_project_assoc`.BREAK_LABEL <>
'Associated ISBNs'
GROUP BY
`pearson-rr.oracle_grdw_grdw.he_project_assoc`.PARENT_ISBN) xx
ON
yy.PARENT_ISBN = xx.PARENT_ISBN
AND yy.CIRCULATION_INT < xx.GR_QTY
) AA
where AA.PARENT_ISBN IN
( SELECT
DISTINCT _BIC_GCISBN
FROM
`pearson-rr.GRDW_RPT.BIC_GM_AGCPOAODS00_BO_VW`
INNER JOIN
`pearson-rr.oracle_grdw_boadmin.fiscal_bo`
ON
_BIC_ZC2GRIRIN = 'G'
AND _BIC_ZCLOEKZ = ' '
AND SUBSTR (BOUND_DATE, 1, 6) = `pearson-
rr.oracle_grdw_boadmin.fiscal_bo`.PRIOR_FISC_YEAR_MONTH )

why sql case condition gives error

I do something like this which works fine :
select nameOfCols
from
FACost
inner join FAT as D on d.nid=a.atypeid
and
d.nid in (select item from SplitString('1,2,3,',','))
But when i use case to handle a situation where user dynamically may enter '' instead of '1,2,3,'. Then it gives error in my case condition
declare #selectedAssetTypeID varchar(50)='1,2,3,'
select nameOfCols
from
FACost
inner join FAT as D on d.nid=a.atypeid
and
case when #selectedAssetTypeID<>'' then d.nid in (select item from SplitString( #selectedAssetTypeID,',')) else d.nid=1 end //error causing lines
errors are:
Msg 156, Level 15, State 1, Line 33
Incorrect syntax near the keyword 'in'.
Msg 156, Level 15, State 1, Line 33
Incorrect syntax near the keyword 'else'.
Use and and or conditions instead of a case expression. The case expression as you have it is assigning a value (else d.nid=1) or checking for a true/false condition (d.nid in (select item from SplitString( #selectedAssetTypeID,','))).
and (
(#selectedAssetTypeID <>'' and d.nid in (select item from SplitString( #selectedAssetTypeID,',')) )
or (d.nid=1)
)
you cannot use in clause with case statement. because Case has to return one value per statement (either true or false)
either you can split your queries in two blocks or you can use "OR" clause.
IF #selectedAssetTypeID = " "
BEGIN
select nameOfCols
from FACost
inner join FAT as D
on (d.nid = a.atypeid)
where d.nid = 1
END
ELSE
BEGIN
select nameOfCols
from FACost
inner join FAT as D
on (d.nid = a.atypeid)
where d.nid IN
(select item from SplitString( #selectedAssetTypeID,','))
END
You can also use "OR" clause
select nameOfCols
from FACost
inner join FAT as D
on (d.nid = a.atypeid)
where ((#selectedAssetTypeID <>'' and d.nid in (select item from SplitString(#selectedAssetTypeID,',')))
or (d.nid=1))
link for the discussion about the similar issue is below
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bc8a7a0b-1980-4481-a2df-6a5fde38f362/in-clause-in-case-statement?forum=sqlgetstarted