How to simulate Subqueries in the ON Clause in Vertica database? - join

Hy everbody , i am having some trouble in transforming a query that has a set of subqueries in the ON clause.
The initial code was created in SQL Server and i have changed to work on Vertica.
This is the code :
SELECT
DISTINCT DocFinanceiro.AutoId AS AutoId,
DocFinanceiro.Classe AS ClasseDoc,
EspecPrestador.Seq AS Sequencia,
PrestadorServico.Codigo AS CodigoPrest,
PessoaPrest.Nome AS NomePrestador,
DocFinanceiro.CompFinanceira AS CompFinanceira,
EspecialidadeServico.Nome AS Especialidade,
ClassePrestador.Nome AS ClsPrestador,
RegistroPessoa.Numero AS RegistroINSS,
RegistroPessoa.Tipo AS TipoRegPessoa,
DocFinanceiro.DataVencimento AS DataVencimentoDocFin
FROM DocFinanceiro
INNER JOIN ClasseDocFinanceiro
ON DocFinanceiro.Classe = ClasseDocFinanceiro.Codigo
INNER JOIN ItemFinanceiro ON DocFinanceiro.AutoId = ItemFinanceiro.Documento
INNER JOIN ClasseApropriacaoFinan
ON ItemFinanceiro.ClasseApro = ClasseApropriacaoFinan.Codigo
INNER JOIN ContratoFinanceiro
ON DocFinanceiro.ContratoFinanceiro = ContratoFinanceiro.AutoId
INNER JOIN Pessoa ON ContratoFinanceiro.Pessoa = Pessoa.AutoId
INNER JOIN GeradorItemFinan
ON ItemFinanceiro.AutoId = GeradorItemFinan.ItemFinan
INNER JOIN PrestadorServico
ON ContratoFinanceiro.AutoId = PrestadorServico.ContratoFinanceiro
INNER JOIN Pessoa PessoaPrest ON PrestadorServico.Pessoa = PessoaPrest.AutoId
INNER JOIN TipoValorFinan
ON ClasseApropriacaoFinan.TipoValor = TipoValorFinan.Codigo
INNER JOIN ClasseContratoFinanceiro
ON ContratoFinanceiro.Classe = ClasseContratoFinanceiro.AutoId
INNER JOIN ClassePrestador
ON PrestadorServico.Classe = ClassePrestador.Codigo
LEFT JOIN EspecPrestador
ON EspecPrestador.Prestador = PrestadorServico.AutoId
AND EspecPrestador.Seq = 1
LEFT JOIN EspecialidadeServico
ON EspecialidadeServico.AutoId = EspecPrestador.Especialidade
LEFT JOIN RegistroPessoa ON PessoaPrest.AutoId = RegistroPessoa.Pessoa
--This is the part where i getr stuck
AND
(
RegistroPessoa.AutoId =
(
SELECT
RegistroPessoa.AutoId
FROM RegistroPessoa
WHERE RegistroPessoa.Pessoa = PessoaPrest.Autoid
AND (RegistroPessoa.Tipo = 1 OR RegistroPessoa.Tipo = 1) order by 1 limit 1
)
)
--until here
WHERE (GeradorItemFinan.TipoGerador = 1)
AND (DocFinanceiro.CompFinanceira = 1)
AND (PrestadorServico.AutoId = 1)
AND DocFinanceiro.CompSeq = 1
AND (DocFinanceiro.Numero IS NOT NULL)
AND (DocFinanceiro.Classe <> 1)
ORDER BY AutoId, Sequencia
;
If anybody has any ideas it would be great !!

try replace
RegistroPessoa.AutoId = (SELECT...)
to
RegistroPessoa.AutoId IN (SELECT...)
and use aliases for table short names

Related

How do I modify a Stored Procedure to Join an additional table?

I have a fairly complex Stored Procedure that is joining several tables together, but I need yet another column called in from a table that is yet to be joined.
Here's the Stored Procedure as it stands:
CREATE PROCEDURE [rpt].[PlannerShipToLocations_ds1]
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql nvarchar(4000)
,#DateStart nvarchar(10) = '2015-01-01'
DECLARE #t TABLE (
[PCN] int
,[Part_Key] int
,[Customer_Address_No] int
PRIMARY KEY CLUSTERED (
[Part_Key]
,[Customer_Address_No]
,[PCN]
)
)
SET #sql =
'SELECT *
FROM OPENQUERY (PLEXRPTSVR,
''SELECT DISTINCT
s.[PCN]
,sl.[Part_Key]
,s.[Customer_Address_No]
FROM [Sales_v_Shipper_e] AS s
INNER JOIN [Sales_v_Shipper_Status_e] AS ss
ON s.[PCN] = ss.[PCN]
AND s.[Shipper_Status_Key] = ss.[Shipper_Status_Key]
INNER JOIN [Sales_v_Shipper_Line_e] AS sl
ON s.[PCN] = sl.[PCN]
AND s.[Shipper_Key] = sl.[Shipper_Key]
WHERE 1 = 1
AND ss.[Shipped] = 1
AND s.[Ship_Date] >= ''''' + #DateStart + '''''
;'')'
INSERT INTO #t
EXECUTE sp_executesql #sql
;WITH base AS (
SELECT DISTINCT u.[Last_Name] + ', ' + u.[First_Name] AS [Planner]
,c.[Customer_Code] AS [Customer Code]
,c.[Name] AS [Customer Name]
,a.[Customer_Address_Code] AS [Customer Address Code]
**/* xxx.[Country] AS [Country] */**
FROM [plx].[Part_v_Customer_Part_e] cp
INNER JOIN [plx].[Part_v_Part_e] p
ON cp.[Plexus_Customer_No] = p.[Plexus_Customer_No]
AND cp.[Part_Key] = p.[Part_Key]
INNER JOIN [plx].[Common_v_Customer_e] c
ON cp.[Plexus_Customer_No] = c.[Plexus_Customer_No]
AND cp.[Customer_No] = c.[Customer_No]
INNER JOIN [plx].[Plexus_Control_v_Plexus_User_e] u
ON p.[Plexus_Customer_No] = u.[Plexus_Customer_No]
AND p.[Planner] = u.[Plexus_User_No]
OUTER APPLY (
SELECT [Customer_Address_Code], **/*[Country]*/**
FROM [plx].[Common_v_Customer_Address_e] a
INNER JOIN #t t
ON a.[Plexus_Customer_No] = t.[PCN]
AND a.[Customer_Address_No] = t.[Customer_Address_No]
**/* INNER JOIN [plx].[Common_v_Country] xxx
ON a.[Country_Key] = xxx.[Country_Key] */**
WHERE a.[Plexus_Customer_No] = p.[Plexus_Customer_No]
AND a.[Customer_No] = c.[Customer_No]
AND t.[Part_Key] = p.[Part_Key]
AND a.[Ship_To] = 1
) a
**
/* INNER JOIN [plx].[Common_v_Country] xxx
ON a.[Country_Key] = xxx.[Country_Key] */
/*
OUTER APPLY (
SELECT [Country]
FROM [plx].[Common_v_Country] xxx
INNER JOIN #t t
ON a.[Country_Key] = xxx.[Country_Key]
WHERE a.[Plexus_Customer_No] = p.[Plexus_Customer_No]
AND a.[Customer_No] = c.[Customer_No]
AND t.[Part_Key] = p.[Part_Key]
AND a.[Ship_To] = 1
) xxx
*/**
WHERE 1 = 1
AND u.[Lockout] = 0
AND p.[Part_Status] IN ('Production', 'Production - Near EOP', 'Pre-Production')
AND cp.[Active] = 1
)
SELECT t1.[Planner]
,t1.[Customer Code]
,t1.[Customer Name]
,STUFF(
(SELECT
' | ' + t2.[Customer Address Code]
FROM base t2
WHERE t1.[Planner] = t2.[Planner]
and t1.[Customer Code] = t2.[Customer Code]
ORDER BY t2.[Customer Address Code]
FOR XML PATH(''), TYPE
).value('.','varchar(max)')
,1,3,'') AS [Customer Address Code(s)]
FROM base t1
GROUP BY t1.[Planner]
,t1.[Customer Code]
,t1.[Customer Name]
ORDER BY [Customer Code]
,[Planner]
,[Customer Address Code(s)]
END
GO
I've bolded the sections that are my best guesses about how to go about joining this additional table, I recognize that I wouldn't use all of them but I wanted to show my thoughts. To break it down:
1.) [plx].[Common_v_Customer_Address_e] a AND [plx].[Common_v_Country] xxx are the two tables I need in order to call [Country] out by name. I essentially need to add this as a column displayed on the report and eventually will need to sort on it as well (I'll probably add it to ORDER BY at the end).
2.) I'm not sure if I need to be joining [plx].[Common_v_Country] xxx within the OUTER APPLY or if it needs its own separate INNER JOIN or what. I've illustrated and commented out both here. So far everything I've tried results in "The multi-part identifier 'xxx.Country' could not be bound."
Thanks for the help.
I've tried modifying the OUTER APPLY to include the new table. I've tried creating my own new INNER JOIN. I've tried creating my own new OUTER APPLY.

how can i use inner join in solr

SELECT A.ioTDeviceKind, B.resourceRiskLevelLowCount, B.resourceRiskLevelMiddleCount, B.resourceRiskLevelHighCount FROM ioTDevice AS A JOIN resourcelogCurrent AS B ON A.ioTDeviceKind = B.ioTDeviceKind WHERE A.resourceId = 'airconditinal'
or
SELECT A.ioTDeviceKind, B.resourceRiskLevelLowCount, B.resourceRiskLevelMiddleCount, B.resourceRiskLevelHighCount FROM ioTDevice AS A INNER JOIN resourcelogCurrent AS B ON A.ioTDeviceKind = B.ioTDeviceKind WHERE A.resourceId = 'airconditinal'
but the result is null.
there is no error, but the result always is null,
where i user left join, there is no error..
please help me , how can i use inner join in solr??

Join subqueries serval times

I have code that run:
How can I join another query in this subqueries?
I whant a third table that joins with the other tables.
How can I achive this?
select *
from
( select irart, fiben1 as "Benämning", fisald +irbant as "disp.saldo", sum(irbant) as "Lev.Best"
from triir00
left join trafi00
on irart = fiart and irfili = fifili
where IRSTYP between 00 and 02 and irlev = '2602688' and irfili = 10 and fihugr = 5
group by irart,fiben1,irlev,fisald,irbant
) as antalLev
full join
( select raart, fiben1 as "Benämning", raorn9 as "Ordernummer", fisald- raanta as "Disp.Saldo", sum(raanta) as "Kund.Best"
from trora00
left outer join trohu00
on huorn9 = raorn9
left join trafi00
on raart = fiart and rafili = fifili
where rastyp <= 02 and rafili <> 0 and raotyp = 1 and ralev1 = '2602688' and fihugr = 5
group by raart,fiben1,raorn9,fisald,raanta
) as antalBest
on antalLev.irart = antalBest.raart
Wrap your original query into brackets:
select *
from
(
-- your original query follows below
select *
from
(
select columns_for_outer_join, other_columns
from ...
) as antalLev
full join
(
select columns_for_outer_join, other_columns
from ...
) as antalBest on antalLev.irart = antalBest.raart
-- end of your original query
) a
join (
-- your another sub-query comes here
select columns_for_outer_join, other_columns
from ...
-- end of your another sub-query
) b on <a.columns_for_outer_join = b.columns_for_outer_join>
...;

PSQL query to comapre data from two instances of a database

I have two instances of the same database from different days. All tables from one day are called tableA* and from the other tableB*. I would like to compare data to see what have changed. I would like to select all rows that don't match exactly. So for example if one value is different in tables tableA1 and tableB1 I would like to select a corresponding row from table A and mark it as 'new' and from table B and mark it as 'deleted'. I tried with a query like this:
SELECT 'new', ta1.name, ta2.name, ta3.name, ta4.name, ta5.name
FROM tableA1 ta1
LEFT JOIN tableA2 ta2 ON ta1.ta2_id = ta2.id
LEFT JOIN tableA3 ta3 ON ta1.ta3_id = ta3.id
LEFT JOIN tableA4 ta4 ON ta1.ta4_id = ta4.id
LEFT JOIN tableA5 ta5 ON ta5.ta1_id = ta1.id WHERE NOT EXISTS
(SELECT tb1.name, tb2.name, tb3.name, tb4.name, tb5.name
FROM tableB1 tb1
LEFT JOIN tableB2 tb2 ON tb1.tb2_id = tb2.id
LEFT JOIN tableB3 tb3 ON tb1.tb3_id = tb3.id
LEFT JOIN tableB4 tb4 ON tb1.tb4_id = tb4.id
LEFT JOIN tableB5 tb5 ON tb5.tb1_id = tb1.id WHERE
tb1.name = ta1.name AND
tb2.name = ta2.name AND
tb3.name = ta3.name AND
tb4.name = ta4.name AND
tb5.name = ta5.name)
UNION
SELECT 'deleted', tb1.name, tb2.name, tb3.name, tb4.name, tb5.name
FROM tableB1 tb1
LEFT JOIN tableB2 tb2 ON tb1.tb2_id = tb2.id
LEFT JOIN tableB3 tb3 ON tb1.tb3_id = tb3.id
LEFT JOIN tableB4 tb4 ON tb1.tb4_id = tb4.id
LEFT JOIN tableB5 tb5 ON tb5.tb1_id = tb1.id WHERE NOT EXISTS
(SELECT ta1.name, ta2.name, ta3.name, ta4.name, ta5.name
FROM tableA1 ta1
LEFT JOIN tableA2 ta2 ON ta1.ta2_id = ta2.id
LEFT JOIN tableA3 ta3 ON ta1.ta3_id = ta3.id
LEFT JOIN tableA4 ta4 ON ta1.ta4_id = ta4.id
LEFT JOIN tableA5 ta5 ON ta5.ta1_id = ta1.id WHERE
tb1.name = ta1.name AND
tb2.name = ta2.name AND
tb3.name = ta3.name AND
tb4.name = ta4.name AND
tb5.name = ta5.name)
Hoping that if I created the same stuructre and compare all the values I would get the anticipated result. Even if databases are the same I get a lot row selected.
Found the problem. When comparing two NULL values the result is FALSE, the query itself should be fine. So I should have added conditions to check whether values are NULL.

TFS2015 - How can I recover shelved changes

Same situation as this [question]: TFS Meltdown - How can I recover shelved changes but for TFS2015. The issue I'm having is I can't find the files that were added or renamed in my query result.
SELECT c.[CreationDate], c.[Content], vi.ChildItem, vi.ParentPath
FROM [dbo].[tbl_Content] c
INNER JOIN [dbo].[tbl_FileMetadata] fm ON fm.ResourceId = c.ResourceId
INNER JOIN [dbo].[tbl_FileReference] fr ON fr.ResourceId = fm.ResourceId
INNER JOIN [dbo].[tbl_PendingChange] pc ON pc.FileId = fr.FileId
INNER JOIN [dbo].[tbl_Workspace] w ON w.WorkspaceId = pc.WorkspaceId
INNER JOIN [dbo].[tbl_Version] vi ON vi.ItemId = pc.ItemId AND vi.VersionTo = 2147483647
WHERE w.WorkspaceName = 'SHELVESET_NAME'
UPDATE:
Was able to figure out the query.
SELECT w.WorkspaceId, c.[CreationDate], c.[Content], pc.TargetChildItem, pc.TargetParentPath
FROM [dbo].[tbl_Content] c
JOIN [dbo].[tbl_FileMetadata] fm ON fm.ResourceId = c.ResourceId
JOIN [dbo].[tbl_FileReference] fr ON fr.ResourceId = fm.ResourceId
JOIN [dbo].[tbl_PendingChange] pc ON pc.FileId = fr.FileId
JOIN [dbo].[tbl_Workspace] w ON w.WorkspaceId = pc.WorkspaceId
WHERE w.WorkspaceName = 'SHELVESET_NAME'
I was able to figure out the query by looking at table tbl_PendingChange and looking for the new files that was added and then correctly updated the original query.

Resources