MYSQL: How to get a group total repeating in column while keeping the grouping -
generated results this:
------------------------ |group a|group b|amount| ------------------------ | | 1 | 5 | | | 2 | 4 | | b | 1 | 4 | | b | 2 | 2 | | b | 3 | 6 | | c | 1 | 1 | | c | 2 | 4 | ------------------------
looking sum in new column total of group repeats along group b:
--------------------------------------- |group a|group b|amount|sum of group a| --------------------------------------- | | 1 | 5 | 9 | | | 2 | 4 | 9 | | b | 1 | 4 | 12 | | b | 2 | 2 | 12 | | b | 3 | 6 | 12 | | c | 1 | 1 | 5 | | c | 2 | 4 | 5 | ---------------------------------------
i'm using several derived , joined tables, hoping easy enough calculate within current select statement or derived table. ideas? can't add additional rows using rollup modifier.
try this:
select t1.*, sumofgroup_a mytable t1 inner join ( select group_a, sum(amount) sumofgroup_a mytable group group_a ) t2 on t1.group_a = t2.group_a
Comments
Post a Comment