插入分组数据
Insert grouped data
我的查询得到了预期的结果,我正在使用 group by 根据不同的 ID 对数据进行分组。
我面临的问题是我必须将这个分组数据插入到名为 gstl_calculated_daily_fee 的 table 中,但是当我将分组结果传递给名为 @total_mada_local_switch_high_value 的变量并且@mada_range_id 并将它们插入 table 然后我只在 table.
中得到查询的最后结果
示例结果:
Fee range_id
1.23 1
1.22 2
2.33 3
插入后我只得到 2.33 和 1 但我必须将整个结果插入 table.
请建议如何将整个查询结果插入 table。下面是查询:
DECLARE @total_mada_local_switch_high_value decimal(32,4) = 0.00;
DECLARE @mada_range_id int = 0;
select
@total_mada_local_switch_high_value = SUM(C.settlement_fees),
@mada_range_id = C.range_id
From
(
select
*
from
(
select
rowNumber = @previous_mada_switch_fee_volume_based_count + (ROW_NUMBER() OVER(PARTITION BY DATEPART(MONTH, x_datetime) ORDER BY x_datetime)),
tt.x_datetime
from gstl_trans_temp tt where (message_type_mapping = 0220) and card_type ='GEIDP1' and response_code IN(00,10,11) and tran_amount_req >= 5000
) A
CROSS APPLY
(
select
rtt.settlement_fees,
rtt.range_id
From gstl_mada_local_switch_fee_volume_based rtt
where A.rowNumber >= rtt.range_start
AND (A.rowNumber <= rtt.range_end OR rtt.range_end IS NULL)
) B
) C
group by CAST(C.x_datetime AS DATE),C.range_id
-- Insert Daily Volume
INSERT INTO
gstl_calculated_daily_fee(business_date,fee_type,fee_total,range_id)
VALUES
(@tlf_business_date,'MADA_SWITCH_FEE_LOCAL_CARD', @total_mada_local_switch_high_value, @mada_range_id)
我看这里不需要变量。您可以直接插入聚合结果。
示例数据
create table Data
(
Range int,
Fee money
);
insert into Data (Range, Fee) values
(1, 1.00),
(1, 0.50),
(2, 3.00),
(3, 0.25),
(3, 0.50);
create table DataSum
(
Range int,
FeeSum money
);
解决方案
insert into DataSum (Range, FeeSum)
select d.Range, sum(d.Fee)
from Data d
group by d.Range;
Fiddle 查看实际情况。
我的查询得到了预期的结果,我正在使用 group by 根据不同的 ID 对数据进行分组。
我面临的问题是我必须将这个分组数据插入到名为 gstl_calculated_daily_fee 的 table 中,但是当我将分组结果传递给名为 @total_mada_local_switch_high_value 的变量并且@mada_range_id 并将它们插入 table 然后我只在 table.
中得到查询的最后结果示例结果:
Fee range_id
1.23 1
1.22 2
2.33 3
插入后我只得到 2.33 和 1 但我必须将整个结果插入 table.
请建议如何将整个查询结果插入 table。下面是查询:
DECLARE @total_mada_local_switch_high_value decimal(32,4) = 0.00;
DECLARE @mada_range_id int = 0;
select
@total_mada_local_switch_high_value = SUM(C.settlement_fees),
@mada_range_id = C.range_id
From
(
select
*
from
(
select
rowNumber = @previous_mada_switch_fee_volume_based_count + (ROW_NUMBER() OVER(PARTITION BY DATEPART(MONTH, x_datetime) ORDER BY x_datetime)),
tt.x_datetime
from gstl_trans_temp tt where (message_type_mapping = 0220) and card_type ='GEIDP1' and response_code IN(00,10,11) and tran_amount_req >= 5000
) A
CROSS APPLY
(
select
rtt.settlement_fees,
rtt.range_id
From gstl_mada_local_switch_fee_volume_based rtt
where A.rowNumber >= rtt.range_start
AND (A.rowNumber <= rtt.range_end OR rtt.range_end IS NULL)
) B
) C
group by CAST(C.x_datetime AS DATE),C.range_id
-- Insert Daily Volume
INSERT INTO
gstl_calculated_daily_fee(business_date,fee_type,fee_total,range_id)
VALUES
(@tlf_business_date,'MADA_SWITCH_FEE_LOCAL_CARD', @total_mada_local_switch_high_value, @mada_range_id)
我看这里不需要变量。您可以直接插入聚合结果。
示例数据
create table Data
(
Range int,
Fee money
);
insert into Data (Range, Fee) values
(1, 1.00),
(1, 0.50),
(2, 3.00),
(3, 0.25),
(3, 0.50);
create table DataSum
(
Range int,
FeeSum money
);
解决方案
insert into DataSum (Range, FeeSum)
select d.Range, sum(d.Fee)
from Data d
group by d.Range;
Fiddle 查看实际情况。