You can also put the sum inside a case statement, where the case evaluates the other condition, and then only sum thoses records where the condition is true...
SELECT m.member_id, m.teamname,
Sum(Case When r.track_Id = '$chosentrack'
Then total_points Else 0 End) TotalChosenTrackPoints,
Sum(Case When r.track_Id < '$chosentrack'
Then total_points Else 0 End) TotalLessThanChosenTrackPoints,
total_points as last_race_points
FROM members m
Join members_leagues l
On l.member_id = m.member_id
Join member_results r
On r.member_id = m.member_id
Where l.league_id = '$chosenleague'
And l.start_race = '$chosentrack'
Group By m.member_id
Order By r.total_points Desc,
last_race_points Desc, m.TeamName Desc
Answer from Charles Bretana on Stack OverflowYou can also put the sum inside a case statement, where the case evaluates the other condition, and then only sum thoses records where the condition is true...
SELECT m.member_id, m.teamname,
Sum(Case When r.track_Id = '$chosentrack'
Then total_points Else 0 End) TotalChosenTrackPoints,
Sum(Case When r.track_Id < '$chosentrack'
Then total_points Else 0 End) TotalLessThanChosenTrackPoints,
total_points as last_race_points
FROM members m
Join members_leagues l
On l.member_id = m.member_id
Join member_results r
On r.member_id = m.member_id
Where l.league_id = '$chosenleague'
And l.start_race = '$chosentrack'
Group By m.member_id
Order By r.total_points Desc,
last_race_points Desc, m.TeamName Desc
SELECT ...
SUM(CASE
WHEN track_id <= [your_value] THEN total_points
ELSE 0
END
) AS total_points, ....
Change this:
SUM(btc_total WHERE order_type = 'BUY') AS buy_total
to this:
SUM(IF(order_type='BUY',btc_total,NULL)) AS buy_total
The MySQL IF() function evaluates the first argument as a boolean, if that's TRUE, it returns the second argument, else it returns the third argument.
The IF() will be evaluated for each row, and the return from that expression will get totaled up by the SUM() aggregate.
or, use the more ANSI-standard equivalent to achieve the same result:
SUM(CASE WHEN order_type = 'BUY' THEN btc_total END) AS buy_total
This pattern is commonly referred to as "conditional aggregation".
For the "counts" we can replace COUNT with SUM, like this:
SUM(order_type = 'BUY') AS buy_fill
MySQL evaluates the equality comparison as a boolean, which returns 1, 0 or NULL, which are then totaled up by the SUM aggregate. (A COUNT of that would include zeros and ones, not just the ones.)
The above is equivalent to
SUM( CASE
WHEN order_type = 'BUY' THEN 1
WHEN order_type <> 'BUY' THEN 0
ELSE NULL
END
) AS buy_fill
If we want to use a COUNT aggregate, we could do it like this:
COUNT(IF(order_type = 'Buy',1,NULL)) AS buy_fill
(We could use any non-null value in place of 1, and get an equivalent result.)
"conditional aggregates" conventionally contain a case expression
SELECT
COUNT(CASE WHEN order_type = 'BUY' THEN order_type END) AS buy_fill
, COUNT(CASE WHEN order_type = 'SELL' THEN order_type END) AS sell_fill
, SUM(btc_total) AS fill_sum
, SUM(CASE WHEN order_type = 'BUY' THEN btc_total ELSE 0 END) AS buy_total
, SUM(CASE WHEN order_type = 'SELL' THEN btc_total ELSE 0 END) AS sell_total
FROM fill_orders
WHERE coin_id = '$coin'
AND time_stamp >= DATE_SUB(NOW(), INTERVAL 55 SECOND)
You need to have a subquery that gives you all codes that have at least 1 record where update=1 and you need to join this back to your table and do the group by and sum:
select m.code, sum(total)
from mytable m
inner join (select distinct code from mytable where `to_update`=1) t on m.code=t.code
group by m.code
Or you can sum the to_update column as well and filter in having:
select m.code, sum(total)
from mytable m
group by m.code
having sum(to_update)> 0
You could do it like this:
SELECT code, SUM(total) AS total
FROM mytable
GROUP BY code
HAVING MAX(to_update) = 1
This assumes that the possible values of to_update are 0 or 1.
Implemented in this fiddle, which outputs the result as requested in the question.
As this query only scans the table once, it will have better performance than solutions that make joins.