Sum/Aggregate 2 个最小值,因此 total2 将 > 2
Sum/Aggregate the 2 least values so that total2 will be > 2
我有这个 table,如果 total2 <=2,我需要聚合 code1。
基本上将它 up/combine total 滚动到下一个值 least value 以便字段 total2 将是 >2 。我怎样才能在 t-sql 中做到这一点?
感谢您的任何帮助。我对此很困惑。
原始:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=ad7f47b37d4694411b8a38db07356a7e
code1
code2
total1
total2
AAA1
123
9
3
AAA1
120
3
5
AAA1
124
4
2
AAA2
125
2
1
AAA2
126
3
2
AAA3
121
4
4
AAA5
119
1
1
AAA6
118
4
2
AAA6
117
2
5
汇总:
code1
code2
total1
total2
AAA1
120
3
5
AAA1
123+124
13
5
AAA2
125+126
5
3
AAA3
121
4
4
AAA5
119
1
1
AAA6
118+117
6
7
首先需要根据total2
确定最少的2行。您可以使用 row_number()
row_number() over(partition by code1 order by total2)
确定 grp
后,对其执行聚合(仅针对最少的 2 行 grp 2
)
对于total1
和total2
,使用SUM()
,对于code2
,因为是字符串,使用字符串聚合string_agg()
示例数据:
code1
code2
total1
total2
AAA1
123
9
3
AAA1
120
3
5
AAA1
124
4
2
AAA2
125
2
1
AAA2
126
3
2
AAA3
121
4
4
AAA5
119
1
1
AAA6
118
4
2
AAA6
117
2
5
with cte as
(
select *,
grp = case when row_number() over(partition by code1
order by total2) <= 2
then 2
else 1
end
from Test1
)
select code1,
code2 = case when grp = 1 then max(code2)
else string_agg(code2, '+') within group (order by code2)
end,
total1 = case when grp = 1 then max(total1) else sum(total1) end,
total2 = case when grp = 1 then max(total2) else sum(total2) end
from cte
group by code1, grp
order by code1, grp
结果:
code1
code2
total1
total2
AAA1
120
3
5
AAA1
123+124
13
5
AAA2
125+126
5
3
AAA3
121
4
4
AAA5
119
1
1
AAA6
117+118
6
7
我有这个 table,如果 total2 <=2,我需要聚合 code1。 基本上将它 up/combine total 滚动到下一个值 least value 以便字段 total2 将是 >2 。我怎样才能在 t-sql 中做到这一点? 感谢您的任何帮助。我对此很困惑。
原始:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=ad7f47b37d4694411b8a38db07356a7e
code1 | code2 | total1 | total2 |
---|---|---|---|
AAA1 | 123 | 9 | 3 |
AAA1 | 120 | 3 | 5 |
AAA1 | 124 | 4 | 2 |
AAA2 | 125 | 2 | 1 |
AAA2 | 126 | 3 | 2 |
AAA3 | 121 | 4 | 4 |
AAA5 | 119 | 1 | 1 |
AAA6 | 118 | 4 | 2 |
AAA6 | 117 | 2 | 5 |
汇总:
code1 | code2 | total1 | total2 |
---|---|---|---|
AAA1 | 120 | 3 | 5 |
AAA1 | 123+124 | 13 | 5 |
AAA2 | 125+126 | 5 | 3 |
AAA3 | 121 | 4 | 4 |
AAA5 | 119 | 1 | 1 |
AAA6 | 118+117 | 6 | 7 |
首先需要根据total2
确定最少的2行。您可以使用 row_number()
row_number() over(partition by code1 order by total2)
确定 grp
后,对其执行聚合(仅针对最少的 2 行 grp 2
)
对于total1
和total2
,使用SUM()
,对于code2
,因为是字符串,使用字符串聚合string_agg()
示例数据:
code1 | code2 | total1 | total2 |
---|---|---|---|
AAA1 | 123 | 9 | 3 |
AAA1 | 120 | 3 | 5 |
AAA1 | 124 | 4 | 2 |
AAA2 | 125 | 2 | 1 |
AAA2 | 126 | 3 | 2 |
AAA3 | 121 | 4 | 4 |
AAA5 | 119 | 1 | 1 |
AAA6 | 118 | 4 | 2 |
AAA6 | 117 | 2 | 5 |
with cte as
(
select *,
grp = case when row_number() over(partition by code1
order by total2) <= 2
then 2
else 1
end
from Test1
)
select code1,
code2 = case when grp = 1 then max(code2)
else string_agg(code2, '+') within group (order by code2)
end,
total1 = case when grp = 1 then max(total1) else sum(total1) end,
total2 = case when grp = 1 then max(total2) else sum(total2) end
from cte
group by code1, grp
order by code1, grp
结果:
code1 | code2 | total1 | total2 |
---|---|---|---|
AAA1 | 120 | 3 | 5 |
AAA1 | 123+124 | 13 | 5 |
AAA2 | 125+126 | 5 | 3 |
AAA3 | 121 | 4 | 4 |
AAA5 | 119 | 1 | 1 |
AAA6 | 117+118 | 6 | 7 |