Question : Nested group by query

Hi experts,

I have a query which is grouped by 4 levels and  I return a count of he totals ie:

select level1, level2, level3, level4, count(*) as Total
from table1
group by level1, level2, level3, level4

This results in:
level1         level2        level3          level4     Total
Cat1            Cat1          Cat1            Cat1       15
Cat1            Cat1          Cat1            Cat2       10
Cat1            Cat1          Cat1            Cat3        4

However, I also need to return the Total for the 3rd level as well. Ie:

level1         level2        level3          level4     Total      Total2
Cat1            Cat1          Cat1            Cat1       15           29
Cat1            Cat1          Cat1            Cat2       10           29
Cat1            Cat1          Cat1            Cat3        4            29          

But I don't know how to formulate this query.
I have tried using some sort of nested subquery in the select statement:

select level1, level2, level3, level4, count(*) as Total, (select count(*) from table1
                                                                                       group by level1, level2, level3)
from table1
group by level1, level2, level3, level4

But this doesn't work. Is it possible to do what I am trying to achieve?

Answer : Nested group by query

This error message usually points to the email editor that you are using and is not set properly. Please do the following steps...

- Click Start, click Run, type regedit in the Open box, and then click OK.
- Locate and then click the following registry subkey:
HKEY_CURRENT_USER\Software\Microsoft\Shared Tools\Proofing Tools\Custom Dictionaries

- Set the value of CUSTOM.DIC to 1.
- Exit Registry Editor

Hope this helps~!
Random Solutions  
 
programming4us programming4us