sqlpostgresqlsql-insert

INSERT a constant combined with the result of SELECT


For example suppose I have a table TRADES as follows

DATE, PRICE, ...

I wish to construct a table TRADING_DAYS as follows

MARKET, DATE

With sample data as follows

NYSE, 2011-03-03
NYSE, 2011-03-04
NYSE, 2011-03-05

Is it possible to write a query that extracts DATE from TRADES, combines it with the constant 'NYSE' and inserts into TRADING_DAYS?


Solution

  • INSERT INTO TRADING_DAYS 
    SELECT 'NYSE', DATE
    FROM TRADES
    

    But I guess you want the distinct dates rather then duplicates?

    INSERT INTO TRADING_DAYS 
    SELECT DISTINCT 'NYSE', DATE
    FROM TRADES