Skip to main content
Format
Source Link
McNets
  • 23.9k
  • 11
  • 50
  • 89

I've seen some advice but want to know the best way to select a group, group total, grand total from a data table WITHOUT using a subquery or unnecessary join.

My initial thought was something like this--:

select   product_family, 
         sum(widgets), 
         sum(widgets) over ()
from     table.widget
group by product_family

or the following:

select   product_family, 
         sum(widgets), 
         sum(widgets) over (partition by all_field)
from     table.widget
group by product_family

Obviously neither of these works. I know a partition can be of a higher order than the actual rows/ groups, but not how to partition by "all" other than leaving it blank as the first example. However, it breaks with the group by statement.

Best I can find is something like this:

 select product_family, 
       family_sum, 
       sum(family_sum) over () as grand_sum from

from (
      select   product_family, sum(widgets)
      from     table.widget
      group by product_family
     ) as A

That still involves a subquery though, which is okay ---. I just feel like I'm missing an easy function here.

I've seen some advice but want to know the best way to select a group, group total, grand total from a data table WITHOUT using a subquery or unnecessary join.

My initial thought was something like this--

select product_family, sum(widgets), sum(widgets) over ()
from table.widget
group by product_family

or the following:

select product_family, sum(widgets), sum(widgets) over (partition by all_field)
from table.widget
group by product_family

Obviously neither of these works. I know a partition can be of a higher order than the actual rows/ groups, but not how to partition by "all" other than leaving it blank as the first example. However, it breaks with the group by statement.

Best I can find is something like this:

 select product_family, family_sum, sum(family_sum) over () as grand_sum from

 (
    select product_family, sum(widgets)
    from table.widget
    group by product_family
    ) as A

That still involves a subquery though, which is okay --- I just feel like I'm missing an easy function here.

I've seen some advice but want to know the best way to select a group, group total, grand total from a data table WITHOUT using a subquery or unnecessary join.

My initial thought was something like this:

select   product_family, 
         sum(widgets), 
         sum(widgets) over ()
from     table.widget
group by product_family

or the following:

select   product_family, 
         sum(widgets), 
         sum(widgets) over (partition by all_field)
from     table.widget
group by product_family

Obviously neither of these works. I know a partition can be of a higher order than the actual rows/ groups, but not how to partition by "all" other than leaving it blank as the first example. However, it breaks with the group by statement.

Best I can find is something like this:

select product_family, 
       family_sum, 
       sum(family_sum) over () as grand_sum 
from (
      select   product_family, sum(widgets)
      from     table.widget
      group by product_family
     ) as A

That still involves a subquery though, which is okay. I just feel like I'm missing an easy function here.

Source Link
user45867
  • 1.7k
  • 5
  • 24
  • 38

SQL query - How to select to display group, group total, grand total?

I've seen some advice but want to know the best way to select a group, group total, grand total from a data table WITHOUT using a subquery or unnecessary join.

My initial thought was something like this--

select product_family, sum(widgets), sum(widgets) over ()
from table.widget
group by product_family

or the following:

select product_family, sum(widgets), sum(widgets) over (partition by all_field)
from table.widget
group by product_family

Obviously neither of these works. I know a partition can be of a higher order than the actual rows/ groups, but not how to partition by "all" other than leaving it blank as the first example. However, it breaks with the group by statement.

Best I can find is something like this:

 select product_family, family_sum, sum(family_sum) over () as grand_sum from

 (
    select product_family, sum(widgets)
    from table.widget
    group by product_family
    ) as A

That still involves a subquery though, which is okay --- I just feel like I'm missing an easy function here.