views:

156

answers:

2

Suppose 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

+4  A: 

Something like this:

insert into symbols(symbol,value)
  (select symbol, max(value) from symbolvalues group by symbol);
David Gelhar
I think you should group by symbol
BobbyShaftoe
@BobbyShaftoe yes, of course; I'll correct the example
David Gelhar
+2  A: 

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)
BobbyShaftoe
I think you mean "AND v2.value > v.value"
David Gelhar
@David Gelhar, yes, thank you. :)
BobbyShaftoe