排除具有相同绝对值的负值
Exclude Negative Values that Have the Same Absolute Values
下面有table
PolicyNumber Premium Commission DataSource EffectiveDate
1 -2.25 -0.41 Internal 31/03/2018
1 160.26 29.14 Internal 31/03/2018
1 98.81 17.96 Internal 31/03/2018
2 361.24 65.67 Internal 31/01/2018
2 570.35 103.70 Internal 31/01/2018
2 -17.05 -3.10 Internal 31/01/2018
3 240.60 0.00 External 31/01/2018
3 196.64 0.00 External 31/01/2018
3 240.60 0.00 External 31/01/2018
3 196.64 0.00 External 31/01/2018
3 -240.60 0.00 External 28/02/2018
3 -196.64 0.00 External 28/02/2018
3 -240.60 0.00 External 28/02/2018
3 -196.64 0.00 External 28/02/2018
3 224.04 107.54 External 28/02/2018
3 176.97 84.95 External 28/02/2018
4 504.00 0.00 External 31/01/2018
4 68.33 0.00 External 31/01/2018
4 504.00 0.00 External 31/01/2018
4 68.33 0.00 External 31/01/2018
4 -504.00 0.00 External 28/02/2018
4 -68.33 0.00 External 28/02/2018
4 -504.00 0.00 External 28/02/2018
4 -68.33 0.00 External 28/02/2018
4 453.55 217.70 External 28/02/2018
4 61.44 29.49 External 28/02/2018
我需要排除所有与前一个绝对值相同的负溢价值。例如,PolicyNumber 3、Premium 240.60 和 196.64 按负保费值 -240.60 和 -196.64 进行调整。我需要排除 PolicyNumber 3。
我希望得到像下面 table 这样的最终结果。
PolicyNumber Premium Commission DataSource EffectiveDate
1 -2.25 -0.41 Internal 31/03/2018
1 160.26 29.14 Internal 31/03/2018
1 98.81 17.96 Internal 31/03/2018
2 361.24 65.67 Internal 31/01/2018
2 570.35 103.70 Internal 31/01/2018
2 -17.05 -3.10 Internal 31/01/2018
我尝试了下面的查询,但它没有给我预期的结果
with cte as
( select distinct a.PolicyNumber, a.datasource, a.EffectiveDate from InsuranceTable a
inner join (select PolicyNumber, premium, EffectiveDate, datasource
from InsuranceTable) b on a.PolicyNumber = b.PolicyNumber
where a.premium < 0 and a.EffectiveDate = b.EffectiveDate and a.datasource = b.datasource and b.premium > 0 )
select x.PolicyNumber, x.premium, x.EffectiveDate, x.Commission, x.datasource from InsuranceTable x
inner join cte y on x.PolicyNumber = y.PolicyNumber and x.datasource = y.datasource
order by x.PolicyNumber, x.EffectiveDate
我正在使用 Microsoft SQL Server 2012。感谢大家的帮助。
下面的 CTE 发现所有保单的保费都被相同金额的负保费正好抵消。它通过按保单和保费的绝对值进行汇总来做到这一点。那么,我们只需要一个非常简单的查询就可以得到你想看的政策。
WITH cte AS (
SELECT DISTINCT PolicyNumber
FROM InsuranceTable
GROUP BY PolicyNumber, ABS(Premium)
HAVING COUNT(*) > 1 AND MAX(Premium) <> MIN(Premium)
)
SELECT t1.*
FROM InsuranceTable t1
WHERE NOT EXISTS (SELECT 1 FROM cte t2 WHERE t1.PolicyNumber = t2.PolicyNumber);
下面有table
PolicyNumber Premium Commission DataSource EffectiveDate
1 -2.25 -0.41 Internal 31/03/2018
1 160.26 29.14 Internal 31/03/2018
1 98.81 17.96 Internal 31/03/2018
2 361.24 65.67 Internal 31/01/2018
2 570.35 103.70 Internal 31/01/2018
2 -17.05 -3.10 Internal 31/01/2018
3 240.60 0.00 External 31/01/2018
3 196.64 0.00 External 31/01/2018
3 240.60 0.00 External 31/01/2018
3 196.64 0.00 External 31/01/2018
3 -240.60 0.00 External 28/02/2018
3 -196.64 0.00 External 28/02/2018
3 -240.60 0.00 External 28/02/2018
3 -196.64 0.00 External 28/02/2018
3 224.04 107.54 External 28/02/2018
3 176.97 84.95 External 28/02/2018
4 504.00 0.00 External 31/01/2018
4 68.33 0.00 External 31/01/2018
4 504.00 0.00 External 31/01/2018
4 68.33 0.00 External 31/01/2018
4 -504.00 0.00 External 28/02/2018
4 -68.33 0.00 External 28/02/2018
4 -504.00 0.00 External 28/02/2018
4 -68.33 0.00 External 28/02/2018
4 453.55 217.70 External 28/02/2018
4 61.44 29.49 External 28/02/2018
我需要排除所有与前一个绝对值相同的负溢价值。例如,PolicyNumber 3、Premium 240.60 和 196.64 按负保费值 -240.60 和 -196.64 进行调整。我需要排除 PolicyNumber 3。
我希望得到像下面 table 这样的最终结果。
PolicyNumber Premium Commission DataSource EffectiveDate
1 -2.25 -0.41 Internal 31/03/2018
1 160.26 29.14 Internal 31/03/2018
1 98.81 17.96 Internal 31/03/2018
2 361.24 65.67 Internal 31/01/2018
2 570.35 103.70 Internal 31/01/2018
2 -17.05 -3.10 Internal 31/01/2018
我尝试了下面的查询,但它没有给我预期的结果
with cte as
( select distinct a.PolicyNumber, a.datasource, a.EffectiveDate from InsuranceTable a
inner join (select PolicyNumber, premium, EffectiveDate, datasource
from InsuranceTable) b on a.PolicyNumber = b.PolicyNumber
where a.premium < 0 and a.EffectiveDate = b.EffectiveDate and a.datasource = b.datasource and b.premium > 0 )
select x.PolicyNumber, x.premium, x.EffectiveDate, x.Commission, x.datasource from InsuranceTable x
inner join cte y on x.PolicyNumber = y.PolicyNumber and x.datasource = y.datasource
order by x.PolicyNumber, x.EffectiveDate
我正在使用 Microsoft SQL Server 2012。感谢大家的帮助。
下面的 CTE 发现所有保单的保费都被相同金额的负保费正好抵消。它通过按保单和保费的绝对值进行汇总来做到这一点。那么,我们只需要一个非常简单的查询就可以得到你想看的政策。
WITH cte AS (
SELECT DISTINCT PolicyNumber
FROM InsuranceTable
GROUP BY PolicyNumber, ABS(Premium)
HAVING COUNT(*) > 1 AND MAX(Premium) <> MIN(Premium)
)
SELECT t1.*
FROM InsuranceTable t1
WHERE NOT EXISTS (SELECT 1 FROM cte t2 WHERE t1.PolicyNumber = t2.PolicyNumber);