Join two SQL queries [duplicated]

0

I have two tables and two queries for each table

A query is

SELECT DISTINCT CodContrato, FechaBaja, FechaFin
FROM Contrato
WHERE (FechaBaja != FechaFin)
AND (FechaBaja BETWEEN '20180101 00:00:00' AND '20181231 23:59:59')

and the other one

SELECT DISTINCT FechaFinProrroga
FROM DatoAuxiliarContrato
WHERE (FechaFinProrroga != '') AND (FechaFinProrroga BETWEEN '20180101 00:00:00' AND '20181231 23:59:59')

Both tables have a common data that is CodContrato

I need to make a query that unifies both querys, that is,

SELECT DISTINCT CodContrato, FechaBaja, FechaFin, FechaFinProrroga
FROM Contrato, DatoAuxiliarContrato
WHERE ((FechaBaja != FechaFin)
        OR (FechaBaja != FechaFinProrroga))
AND (FechaBaja BETWEEN '20180101 00:00:00' AND '20181231 23:59:59')

Obviously this last one does not work, but it is for you to see what I want and I do not know how to do it Thanks in advance

    
asked by Paolo Frigenti 12.12.2018 в 10:16
source

4 answers

2

You do not see clearly what you are trying because initially you define queries that change in the unified query, but I understand that it will be something similar to this. I have also changed the ORs for AND since I understand that you want all the conditions to be met. I hope that from this query you can adapt it to what you need.

SELECT DISTINCT CodContrato, FechaBaja, FechaFin, FechaFinProrroga
FROM Contrato INER JOIN DatoAuxiliarContrato ON Contrato.CodContrato=DatoAuxiliarContrato.CodContrato
WHERE FechaBaja != FechaFin AND FechaBaja != FechaFinProrroga AND FechaBaja BETWEEN '20180101 00:00:00' AND '20181231 23:59:59'
    
answered by 12.12.2018 / 10:39
source
1

I do not know if the dates that have the same name in both tables should match (I suppose so), but if they are different (once you understand the example and can adapt it) the query would be something like this:

SELECT DISTINCT Contrato.CodContrato, Contrato.FechaBaja AS ContratoFechaBaja, Contrato.FechaFin AS ContratoFechaFin, DatoAuxiliarContrato.FechaBaja AS DatoAuxiliarContratoFechaBaja, DatoAuxiliarContrato.FechaFin AS DatoAuxiliarContratoFechaFin, DatoAuxiliarContrato.FechaFinProrroga
FROM Contrato INNER JOIN DatoAuxiliarContrato ON Contrato.CodContrato=DatoAuxiliarContrato.CodContrato 
WHERE (Contrato.FechaBaja != Contrato.FechaFin) AND (Contrato.FechaBaja BETWEEN '20180101 00:00:00' AND '20181231 23:59:59')) 
AND ((DatoAuxiliarContrato.FechaFinProrroga != '') AND (DatoAuxiliarContrato.FechaFinProrroga BETWEEN '20180101 00:00:00' AND '20181231 23:59:59'))

I advise you to keep an eye on the different types of JOINS available and use the one that best suits your problem: Types of JOINS

    
answered by 12.12.2018 в 10:44
1

Using a subselect

SELECT DISTINCT c.CodContrato, c.FechaBaja, c.FechaFin
FROM Contrato c
WHERE (c.FechaBaja != c.FechaFin OR c.FechaBaja 
NOT IN (SELECT DISTINCT FechaFinProrroga 
FROM DatoAuxiliarContrato WHERE CodContrato=c.CodContrato AND (FechaFinProrroga != '') AND (FechaFinProrroga BETWEEN '20180101 00:00:00' AND '20181231 23:59:59')) )
AND (c.FechaBaja BETWEEN '20180101 00:00:00' AND '20181231 23:59:59')
    
answered by 12.12.2018 в 12:03
1

You tried doing:

SELECT DISTINCT
    con.CodContrato,
    con.FechaBaja,
    con.FechaFin,
    dac.FechaFinProrroga
FROM
    Contrato AS con,
    DatoAuxiliarContrato AS dac
WHERE
    (
        con.FechaBaja != con.FechaFin 
        OR con.FechaBaja != dac.FechaFinProrroga
    )
    AND con.FechaBaja BETWEEN '20180101 00:00:00' AND '20181231 23:59:59';

ANSI SQL has a feature called EQUI JOIN . Viewing the query you wanted to make, just modify it to fit the standard.

I hope it serves you.

    
answered by 12.12.2018 в 15:31