Sql query Group By
I have a table 'OrderBook
'
it has field Oid, symbol, price, qty, buysell
开发者_如何学运维i want to sum of buy/sell
price and Qty
group by symbol
SELECT SUM(buy),
SUM(sell),
SUM(price),
SUM(qty)
FROM OrderBook
GROUP BY symbol
As you can see - SQL looks like natural language and is almost similar what you asked in the question ;-)
I think this is a 'stock trading order book'; the table probably contains individual orders made. The 'buy/sell' column indicates whether the trade was a purchase or a sale; and it is likely that the required statistics are the sum of 'price * quantity' (the value of the sales or purchases) and maybe sum of quantity too (total volume of shares), grouped by symbol and buy/sell status. I wonder if the OID somehow identifies the date and time of the order?
If this is correct, then the aggregation should probably be:
SELECT Symbol, BuySell,
SUM(Price * Quantity) AS Value,
SUM(Quantity) AS Volume
FROM OrderBook
GROUP BY Symbol, BuySell
ORDER BY Symbol, BuySell;
SELECT symbol,SUM(buy),SUM(sell),SUM(qty) FROM OrderBook GROUP BY symbol
use it this way, may the query could help u upto some extend........... inside the SUM(table_field) write the field's name for which u want to get the sum.
精彩评论