PSQL query to comapre data from two instances of a database - psql

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.

Related

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

Geonames - Admin3 and Admin4 data

I've downloaded the geonames data dump
The main table has admin3 and admin4 columns but there is no data dump which corresponds to the data in these columns.
Does any one know how to map the admin3 and admin4 codes with their respective place names?
Documentation on the primary website does not seem to explain this but here you go:
select t4.name as town,
t3.name as county,
t2.name as district,
t1.name as admin1,
t0.name,
t0.feature_class,
t0.feature_code,
t0.country_code,
t0.admin1,
t0.admin2,
t0.admin3,
t0.admin4,
t0.population
from geoname t0
left join geoname t1 on t1.admin1 = t0.admin1 and t1.feature_code = 'ADM1'
left join geoname t2 on t2.admin2 = t0.admin2 and t2.feature_code = 'ADM2'
left join geoname t3 on t3.admin3 = t0.admin3 and t3.feature_code = 'ADM3'
left join geoname t4 on t4.admin4 = t0.admin4 and t4.feature_code = 'ADM4'
where t0.name = 'London'

select all records from left table that do not exist in right table

I am working on sql server I have two tables and I need to return records from the left table which are not found in the right table for that I am using left join like below query,
select #MID=MID,#MName=Name,#PID=PID,#PName=PName,#DID=DID from #CompanyDataInfo where id=#MCount
insert into #temp SELECT Top(1) f.Name,f.PID,f.PName,v.* FROM #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where v.Id =#MID and v.DId = #DId and v.PId = #PId and v.CId =#CId and DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() order by DATE_TIME desc
Result should be all rows from #CompanyDataInfo table while no record found in Employee table for related ID, I googled and use "v.Id is null" but not getting expected result
Is there any solution greatly appriciable
Thanks In advance
Your query is not using left join in correct way. You are using your right table reference in where clause. I try to correct it below but I don't have full information about your table schema. Please try this-
select
#MID = MID,
#MName = Name,
#PID = PID,
#PName = PName,
#DID = DID
from #CompanyDataInfo
where id = #MCount
insert into #temp
select
f.Name,
f.PID,
f.PName,
v.*
from #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where f.Id = #MID and
f.DId = #DId and
f.PId = #PId and
f.CId = #CId and
f.DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() and
v.Id is null
order by f.DATE_TIME desc
Add ...and v.Id is null to your where clause.

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.

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

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

Resources