如何总结自定义组的值
how to sum up the values for custom groups
我 select 查询每个子组中元素的 returns 计数(如下图所示)。现在我想做两件事:
- 限制子组数量
- 有限子组的总计数
我想得到以下结果(如下图):只有3个子组(当前结果中的子组1和2保持原样,所有其他子组一起分组为子组3及其各自的计数总结出来了。所有组都一样。
SELECT inner_counts.level4 as group, inner_counts.luc as subgroup, SUM(inner_counts.count_no)
FROM (SELECT original_table2.level
, CASE original_table2.level
WHEN 1 THEN 1
WHEN 2 THEN 2
WHEN 3 THEN 3
ELSE 4
END as level4,
original_table1.classes
, CASE original_table1.classes
WHEN 1
THEN 1
WHEN 2
THEN 2
ELSE 3
END
as luc,
count(original_table2.id) as count_no
FROM original_table2
LEFT JOIN original_table1
ON original_table1.plot_id=original_table2.id
WHERE original_table2.selected_for_field>0
AND original_table1.reachability_2=2
AND original_table1.classes IN (SELECT DISTINCT original_table1.classes
FROM original_table1)
AND original_table2.level IN (SELECT DISTINCT original_table2.level
FROM original_table2)
GROUP BY original_table2.level, original_table1.classes
ORDER BY original_table2.level ASC) as inner_counts
GROUP BY inner_counts.level4, inner_counts.luc,inner_counts.count_no
ORDER BY inner_counts.level4 ASC
查看 OVER() PARTITION BY 子句。这将允许您在不影响其余结果的情况下计算单独的汇总。
我相信还有更优雅的方法,但这应该能让您得到结果集。在子查询中使用 case when 转换子组中的值,然后分组。
create table #test
([Group] int, [SubGroup] int, [Count] int)
Insert into #test Values(1,1,175)
insert into #test values(1,2,5)
insert into #test values(1,3,29)
insert into #test values(1,4,116)
insert into #test values(1,5,5)
insert into #test values(1,6,4)
insert into #test values(1,7,8)
insert into #test values(1,8,5)
insert into #test values(1,9,479)
insert into #test values(2,1,12)
insert into #test values(2,2,33)
insert into #test values(2,3,147)
insert into #test values(2,4,6)
insert into #test values(2,5,5)
insert into #test values(2,6,38)
insert into #test values(2,7,440)
insert into #test values(2,8,67)
insert into #test values(2,9,110)
insert into #test values(3,1,256)
insert into #test values(3,2,7)
insert into #test values(3,3,17)
insert into #test values(3,4,8)
insert into #test values(3,5,428)
insert into #test values(3,6,23)
insert into #test values(3,7,26)
insert into #test values(3,8,78)
insert into #test values(3,9,1)
insert into #test values(4,1,81)
insert into #test values(4,2,425)
insert into #test values(4,3,129)
insert into #test values(4,4,327)
insert into #test values(4,5,455)
insert into #test values(4,6,126)
insert into #test values(4,7,48)
insert into #test values(4,8,287)
insert into #test values(4,9,363)
insert into #test values(5,1,239)
insert into #test values(5,2,93)
insert into #test values(5,3,373)
insert into #test values(5,4,53)
insert into #test values(5,5,109)
insert into #test values(5,6,126)
insert into #test values(5,7,419)
insert into #test values(5,8,214)
insert into #test values(5,9,342)
insert into #test values(6,1,104)
insert into #test values(6,2,437)
insert into #test values(6,3,356)
insert into #test values(6,4,337)
insert into #test values(6,5,6)
insert into #test values(6,6,388)
insert into #test values(6,7,172)
insert into #test values(5,8,280)
insert into #test values(5,9,349)
Select
[Group],
[SubGroup],
CountNumeric = sum([Count])
From (
Select
[Group],
[SubGroup] = Case subgroup
When 1
Then 1
WHen 2
Then 2
Else 3
End,
[Count]
From #test) a
Group By
[Group],
[SubGroup]
Order by 1,2 asc
我的结果集是:
1 1 175
1 2 5
1 3 646
2 1 12
2 2 33
2 3 813
3 1 256
3 2 7
3 3 581
4 1 81
4 2 425
4 3 1735
5 1 239
5 2 93
5 3 2265
6 1 104
6 2 437
6 3 1259
这是你要找的吗?
我 select 查询每个子组中元素的 returns 计数(如下图所示)。现在我想做两件事:
- 限制子组数量
- 有限子组的总计数
我想得到以下结果(如下图):只有3个子组(当前结果中的子组1和2保持原样,所有其他子组一起分组为子组3及其各自的计数总结出来了。所有组都一样。
SELECT inner_counts.level4 as group, inner_counts.luc as subgroup, SUM(inner_counts.count_no)
FROM (SELECT original_table2.level
, CASE original_table2.level
WHEN 1 THEN 1
WHEN 2 THEN 2
WHEN 3 THEN 3
ELSE 4
END as level4,
original_table1.classes
, CASE original_table1.classes
WHEN 1
THEN 1
WHEN 2
THEN 2
ELSE 3
END
as luc,
count(original_table2.id) as count_no
FROM original_table2
LEFT JOIN original_table1
ON original_table1.plot_id=original_table2.id
WHERE original_table2.selected_for_field>0
AND original_table1.reachability_2=2
AND original_table1.classes IN (SELECT DISTINCT original_table1.classes
FROM original_table1)
AND original_table2.level IN (SELECT DISTINCT original_table2.level
FROM original_table2)
GROUP BY original_table2.level, original_table1.classes
ORDER BY original_table2.level ASC) as inner_counts
GROUP BY inner_counts.level4, inner_counts.luc,inner_counts.count_no
ORDER BY inner_counts.level4 ASC
查看 OVER() PARTITION BY 子句。这将允许您在不影响其余结果的情况下计算单独的汇总。
我相信还有更优雅的方法,但这应该能让您得到结果集。在子查询中使用 case when 转换子组中的值,然后分组。
create table #test
([Group] int, [SubGroup] int, [Count] int)
Insert into #test Values(1,1,175)
insert into #test values(1,2,5)
insert into #test values(1,3,29)
insert into #test values(1,4,116)
insert into #test values(1,5,5)
insert into #test values(1,6,4)
insert into #test values(1,7,8)
insert into #test values(1,8,5)
insert into #test values(1,9,479)
insert into #test values(2,1,12)
insert into #test values(2,2,33)
insert into #test values(2,3,147)
insert into #test values(2,4,6)
insert into #test values(2,5,5)
insert into #test values(2,6,38)
insert into #test values(2,7,440)
insert into #test values(2,8,67)
insert into #test values(2,9,110)
insert into #test values(3,1,256)
insert into #test values(3,2,7)
insert into #test values(3,3,17)
insert into #test values(3,4,8)
insert into #test values(3,5,428)
insert into #test values(3,6,23)
insert into #test values(3,7,26)
insert into #test values(3,8,78)
insert into #test values(3,9,1)
insert into #test values(4,1,81)
insert into #test values(4,2,425)
insert into #test values(4,3,129)
insert into #test values(4,4,327)
insert into #test values(4,5,455)
insert into #test values(4,6,126)
insert into #test values(4,7,48)
insert into #test values(4,8,287)
insert into #test values(4,9,363)
insert into #test values(5,1,239)
insert into #test values(5,2,93)
insert into #test values(5,3,373)
insert into #test values(5,4,53)
insert into #test values(5,5,109)
insert into #test values(5,6,126)
insert into #test values(5,7,419)
insert into #test values(5,8,214)
insert into #test values(5,9,342)
insert into #test values(6,1,104)
insert into #test values(6,2,437)
insert into #test values(6,3,356)
insert into #test values(6,4,337)
insert into #test values(6,5,6)
insert into #test values(6,6,388)
insert into #test values(6,7,172)
insert into #test values(5,8,280)
insert into #test values(5,9,349)
Select
[Group],
[SubGroup],
CountNumeric = sum([Count])
From (
Select
[Group],
[SubGroup] = Case subgroup
When 1
Then 1
WHen 2
Then 2
Else 3
End,
[Count]
From #test) a
Group By
[Group],
[SubGroup]
Order by 1,2 asc
我的结果集是:
1 1 175
1 2 5
1 3 646
2 1 12
2 2 33
2 3 813
3 1 256
3 2 7
3 3 581
4 1 81
4 2 425
4 3 1735
5 1 239
5 2 93
5 3 2265
6 1 104
6 2 437
6 3 1259
这是你要找的吗?