The table has the values like,
group_name group_id parent_group_id amount
One 69 10 1000
Two 70 11 500
Three 71 69 700
Here I need to make sum of the values in amount column if the group id = 69
matches with the parent_group_id = 69
for which I have tried with
SUM(CASE WHEN fee_groups.groups_id = fee_groups.parent_fee_groups_id
THEN amount
ELSE 0
END) AS total
But above line doing nothing for me. If I am wrong with it then kindly help me to achieve the following result,
group_name group_id parent_group_id amount
One 69 10 1700
Two 70 11 500
Here the group_id
and parent_group_id
make a relationship, if there is a match from any of the parent_group_id
with group_id
then both are comes under single group and hence I need to sum both the values.
It doesn't make sense to me to include group_id
and parent_group_id
given what you want to do. You also don't explain what happens to the "71".
select min(group_name) as group_name,
sum(amount)
from t
group by (case when 69 not in (group_id, parent_group_id) then group_name end);
This will combine the groups that share "69" and keep the others separate.
Try this solution:
SELECT t1.group_name, t1.group_id, t1.parent_group_id,
(t1.amount + IFNULL(t2.amount,0)) Amount
FROM Table1 t1
LEFT JOIN Table1 t2
ON t1.group_id = t2.parent_group_id
OR t2.group_id = t1.parent_group_id
WHERE t1.group_name < t2.group_name OR t2.group_name IS NULL
GROUP BY t1.group_name, t1.group_id, t1.parent_group_id
OUTPUT:
group_name group_id parent_group_id Amount
One 69 10 1700
Two 70 11 500
Try this
SELECT
group_name,group_id,parent_group_id,
(CASE WHEN group_id = 69 THEN
(amount+(SELECT amount FROM table1 WHERE parent_group_id = 69))
ELSE amount END) as new_amount
FROM Table1 WHERE parent_group_id !=69;
do the execute below query or this link
select t.group_name,t.group_id,t.parent_group_id,
(case when t.group_id = t1.parent_group_id then t1.amount + t.amount else t.amount end)
from t
left join t as t1 on t.group_id = t1.parent_group_id
where t.group_id <>71
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.