How to get two fields from the same table in SQL through a query

0

What I want is to get from the same vista or consulta two fields of the same column but with different value, this means that I must recover two different values within a same column, one value is old or obsolete and the other value is the current or new one and they must be in the view to access the obsolete one that was updated with a new one.

Current Code:

select p.numParte,f.alterno from DBEMPDEV.dbo.tMaestroPartes p
inner join DBSIADANA.dbo.tAlterno f on p.idNParte = f.idNParte;

The above code brings me the numero de parte and my alterno , only that the alternate is a id and what I want to come out in that consulta are my two numbers of parts. What I would like to know is how with the ID that I get from an alternate, I can get the current part number within the same field of my table.

This is how I currently get the record, the alternate that is an id must be replaced by a part number, it should only be substituted in the query not permanently.

    
asked by David 03.03.2017 в 17:22
source

2 answers

4

If I understood the question correctly, this is the query:

SELECT P.numParte AS numParte, Q.numParte AS numParteAlterno
FROM DBEMPDEV.dbo.tMaestroPartes P
    INNER JOIN DBSIADANA.dbo.tAlterno F on P.idNParte = F.idNParte
    INNER JOIN DBEMPDEV.dbo.tMaestroPartes Q on Q.idNParte = F.alterno;

(assuming that f.alterno is a idNParte in table tMaestroPartes )

    
answered by 03.03.2017 / 17:43
source
0

I do not know which table is the alternate, but if it is the same as the first, you can try this

select p.numParte,
(select DBEMPDEV.dbo.tMaestroPartes.numParte 
   from DBEMPDEV.dbo.tMaestroPartes
   where DBEMPDEV.dbo.tMaestroPartes.idNParte = DBSIADANA.dbo.tAlterno.alterno)
as numParteAlterno
from DBEMPDEV.dbo.tMaestroPartes p
inner join DBSIADANA.dbo.tAlterno f on p.idNParte = f.idNParte;
    
answered by 03.03.2017 в 17:42