trade_data Table
id symbol action price percent
1 1 Buy 3.4 50
2 1 SELL 3.8 25
3 2 sell 45 75
3 2 buy 25.2 50
symbol Table
id name
1 RCC
2 REG
SELECT SUM( IF( trade_data.action = 'BUY', price, 0 ) ) AS 'BUYSUM', SUM( IF( trade_data.action = 'SELL', price, 0 ) ) AS 'BUYSELL', AVG( trade_data.percent ) AS peravg, symbol. * FROM trade_data
INNER JOIN symbol
ON trade_data.symbol = symbol.id
This Query Gives me One Row But there Are Two Row are available at this Condition. But When I change the query As
SELECT trade_data.* , symbol. *
FROM `trade_data`
INNER JOIN `symbol` ON trade_data.symbol = symbol.id
Then this Query Gives me Exact 2 Rows.
Use group by with aggregate function ex.
SELECT SUM( IF( trade_data.action = 'BUY', price, 0 ) ) AS 'BUYSUM', SUM( IF( trade_data.action = 'SELL', price, 0 ) ) AS 'BUYSELL', AVG( trade_data.percent ) AS peravg, symbol. *
FROM trade_data
INNER JOIN symbol
ON trade_data.symbol = symbol.id group by symbol.id
SUM()
in mysql gives you the sum of the specified column from the result rows. So here, SUM(price) will give you the Total of price, adding each row's price value. And the total is always a single value due to which you get a single row output.
Hope this helped.
You are using AVG()
and SUM()
functions , that is why you are getting a single reord
The AVG() Function
The AVG() function returns the average value of a numeric column.
The SUM() Function
The SUM() function returns the total sum of a numeric column.
If you have multiple trade_data.symbol = symbol.id
(more than 1 id) you can use a GROUP BY
to get sum and average of each record with similar id , since you have only 1 same symbol.id
and trade_data.symbol
you will get only one record .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.