Sup开发者_如何学Gopose that I have a Table Symbols(Symbol, Value) and a Table SymbolValues (Symbol, Value) which contains a list of values for the symbol. How to choose maximum values fromt he SymbolValues table and insert into Symbols table. For Example, The SymbolValues Table has following values
A 1
A 2
A 3
B 6
B 7
Then only A 3 and B 7 should be inserted in the Symbols table.
Is this possible using insert into select statement.
Thanks
Something like this:
insert into symbols(symbol,value)
(select symbol, max(value) from symbolvalues group by symbol);
You could modify your select query to be something like:
SELECT v.symbol, v.value
FROM SymbolValues v
WHERE NOT EXISTS (SELECT * FROM SymbolValues v2 WHERE v2.symbol = v.symbol AND v2.value > v.value)
精彩评论