简体   繁体   中英

Adding a column from another table into existing SQL/query statement

Currently, I have a query that I run in my java code that displays just a simple grid output of columns with the corresponding data for those specific fields. I am reading 2 tables that all have the same column names. I need to add just 1 column to that grid, but the field name resides on a different table. How would I add this to my existing query?

This is my current query that I execute in the Java:

    SELECT  TRNSP_EQP_EIN, TRNSP_EQP_ID, PRE_EQP_ID, EQP_GRP, AAR_CT_C,
AAR_MCHDSG_C,BLD_D, REBLD_D
FROM  EQ.TE_TRNSP_EQPACTV  A
WHERE TRNSP_EQP_ID = ‘BNSF0000000123’
UNION
SELECT TRNSP_EQP_EIN, TRNSP_EQP_ID, PRE_EQP_ID, EQP_GRP, AAR_CT_C,
AAR_MCHDSG_C,BLD_D, REBLD_D
FROM  EQ.TE_TRNSP_EQPHIST  A
WHERE A.TRNSP_EQP_ID = ‘ABC0123’

ORDER BY TRNSP_EQP_EFF_TS
WITH UR

Below is the information that I am trying to add to the grid to the existing SQL.

Table: EQ.TE_LOCO_EQP

Field: DEL_RSN_CD

You need to provide us with the full field list of EQ.TE_LOCO_EQP to answer this question in full, yet I think you'll be able to manage with what I have provided you below. Replace what is in the square brackets ([]) with the field relevant for the join.

I agree with @Snowman in that you could easily research this one.

SELECT
    *
FROM
(
    SELECT
        TRNSP_EQP_EIN
        ,TRNSP_EQP_ID
        ,PRE_EQP_ID
        ,EQP_GRP
        ,AAR_CT_C
        ,AAR_MCHDSG_C
        ,BLD_D
        ,REBLD_D
    FROM
        EQ.TE_TRNSP_EQPACTV  A
    WHERE 
        TRNSP_EQP_ID = ‘BNSF0000000123’
    UNION
    SELECT
        TRNSP_EQP_EIN
        ,TRNSP_EQP_ID
        ,PRE_EQP_ID
        ,EQP_GRP
        ,AAR_CT_C
        ,AAR_MCHDSG_C
        ,BLD_D
        ,REBLD_D
    FROM 
        EQ.TE_TRNSP_EQPHIST  B
    WHERE
        A.TRNSP_EQP_ID = ‘ABC0123’

    ORDER BY
        TRNSP_EQP_EFF_TS
    WITH UR /* No idea what this is? */
) X
LEFT JOIN
    EQ.TE_LOCO_EQP Y
ON 
    X.[PRIMARY_KEY] = Y.[EQUIVELANT_FOREIGN_KEY]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM