Anomaly wrote:
Hi everyone,
My problem is that I have a table for which I need the totals, the table contains 6 columns, 3 of them are a character and the other 3 are money amounts:
Group1 | Group2 | Group3 | TaxGroup1 | TaxGroup2 | TaxGroup3
A A1 A1a 1.00 .50 .25
B B1 B1a 2.00 .30 .45
A A1 <empty> 1.20 .25 .00
As you can see the TaxGroup1 is connected with Group1 and so on, also the other groups are sub-groups. I need to be able to generate a cursor to show the totals of each group.
I hope my explanation helps.
Thank you.
We can get:
select Group1, sum(TaxGroup1) as TotalTaxGroup1 from myTable group by 1 into cursor cur1
select Group2, sum(TaxGroup2) as TotalTaxGroup2 from myTable group by 1 into cursor cur2
select Group3, sum(TaxGroup3) as TotalTaxGroup3 from myTable group by 1 into cursor cur3
To connect them you may try to connect first based on Group1, second based on Group2, third based on Group3. So, you can combine now all steps in 1 SQL, e.g.
select g.Group1, g.Group2, g.Group3, cur1.TotalTGroup1, cur2.TotalTGroup2, cur3.TotalTGroup3 from MyTable g ;
inner join (select Group1, sum(TaxGroup1) as TotalTaxGroup1 from myTable group by 1) cur1 on g.Group1 = cur1.Group1 ;
inner join ....
Or you may have to use left join in case you don't have data in one of the columns. The idea is from the top of my head, not tested.
The problem of that is, that A,A1, <empty> will not merge with A,A1,A1a
to get the sums for A1 and B1 etc right you need
Select Group1, Group2,;
Sum(TaxGroup1) as TaxGroup1,;
Sum(TaxGroup2) as TaxGroup2;
from myTable group by 1,2
To get A,B etc right
Select Group1,;
Sum(TaxGroup1) as TaxGroup1;
from myTable group by 1
But I'm not sure how you would want to merge these seperate results, if there was A1b or A2.