Skip to content
Advertisement

How to sum 2 columns in a non-named table I have built from other tables

I have built a table taking columns from several other tables that are all linked by one or another common columns. I now need to perform simple multiplication on 2 the data in 2 columns contained in the table I have built. I see examples of the syntax such as this: SELECT name, price*quantity AS total_price FROM purchase;

however I don’t know what to put in here for the ‘FROM’ table as I am performing this arithmetic on 2 columns from the table I have built which to my knowledge doesn’t have a name (unless there is some sort of default name you can use for these tables we build? I am very new to SQL..).

Here is my code so far:

select A.*,
    A.TRANSACTION_ID,
    B.AMOUNT,
    B.CHARGE_ID,
    C.CHARGE_TYPE_ID,
    D.CHARGE_GROUP,
    E.EXCHANGE_ID,
    E.TRADE_DATE,
    timestamp_table.timeformated
    
FROM REPLDOADM.IRE_ESTIMATE_TRANS_MAP A
left join REPLDOADM.IRE_CHARGES_ESTIMATE B ON A.ESTIMATE_ID = B.ESTIMATE_ID
left join REPLDOADM.IRE_CHARGES_LU C ON B.CHARGE_ID = C.CHARGE_ID
left join REPLDOADM.ire_charge_types_lU D ON C.CHARGE_TYPE_ID = D.CHARGE_TYPE_ID
left join REPLDOADM.VW_IRE_TRADE_TRANSACTIONS E ON A.TRANSACTION_ID = E.TRANS_ID
left join (
select to_char(create_ts, 'HH24:MI:SS') as timeformated, TRANS_ID
from REPLDOADM.VW_IRE_TRADE_TRANSACTIONS) timestamp_table ON (A.TRANSACTION_ID = 
timestamp_table.TRANS_ID)

On my output I have 2 columns called RATE and BASIS_TYPE which I need to muliply together on a row by row basis and get the output in a new column. These 2 columns were both taken from REPLDOADM.IRE_ESTIMATE_TRANS_MAP initially.

Any thoughts?

Advertisement

Answer

I think you simply need this –

select A.*,
    A.TRANSACTION_ID,
    B.AMOUNT,
    B.CHARGE_ID,
    C.CHARGE_TYPE_ID,
    D.CHARGE_GROUP,
    E.EXCHANGE_ID,
    E.TRADE_DATE,
    timestamp_table.timeformated,
    A.RATE * A.BASIS_TYPE CALC_RATE
FROM REPLDOADM.IRE_ESTIMATE_TRANS_MAP A
left join REPLDOADM.IRE_CHARGES_ESTIMATE B ON A.ESTIMATE_ID = B.ESTIMATE_ID
left join REPLDOADM.IRE_CHARGES_LU C ON B.CHARGE_ID = C.CHARGE_ID
left join REPLDOADM.ire_charge_types_lU D ON C.CHARGE_TYPE_ID = D.CHARGE_TYPE_ID
left join REPLDOADM.VW_IRE_TRADE_TRANSACTIONS E ON A.TRANSACTION_ID = E.TRANS_ID
left join (
select to_char(create_ts, 'HH24:MI:SS') as timeformated, TRANS_ID
from REPLDOADM.VW_IRE_TRADE_TRANSACTIONS) timestamp_table ON (A.TRANSACTION_ID = 
timestamp_table.TRANS_ID)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement