根据 ROW_NUMBER 高于 1 的结果进行过滤
Filter based on results where ROW_NUMBER goes higher than 1
我设法使用 ROW_NUMBER()
获得了一些有序的结果,并按国家代码和客户参考号进行了分区,并按报价日期排序。 ROW_NUMBER()
列称为 PRank。
CountryCode ¦ ClientRef ¦ Quote Ref ¦ Name ¦ Address ¦ PostCode ¦ PreferencesCode ¦ QuoteDate ¦ PolicyType ¦ PRank ¦
0 AABB001 AABB001MB1 Mr A Smith 1 Country Lane WA21 2PU 5934611 2017-03-18 00:00:00 MB 1
0 AABB001 AABBMM1MB2 Mr A Smith 1 Country Lane WA21 2PU 5934611 2017-03-18 00:00:00 MB 2
0 AABB001 AABBMM1MB3 Mr A Smith 1 Country Lane WA21 2PU 5934611 2017-03-18 00:00:00 MB 3
0 BBGG003 BBGG003MB1 Mrs B Jones 2 City Road M1 3XY 312191 2017-09-02 00:00:00 MB 1
这是我的 ROW_NUMBER()
专栏的代码:
ROW_NUMBER() OVER(PARTITION BY CountryCode, ClientRef, QuoteDate
ORDER BY QuoteDate DESC, PolicyRef asc)
有很多像最后一行这样的结果;他们只计为 1,因为客户在相关日期只有一个报价。我想消除这些行,而不从客户结果中删除在同一日期出现多个报价的任何行(即我仍然希望在上面的结果中看到第一个客户的所有三行)所以我想通过使用 CTE 和过滤在 ROW_NUMBER 上会导致我丢失我仍然需要查看的结果中的数据。
我试图用 HAVING 做一些事情并对 PRank 列求和,但我无法弄清楚分组,因为我不想实际聚合数据,只需删除 ClientRef 只有的任何行每天一个 QuoteRef。
如果您正在寻找一天内有超过 1 个报价的客户,CTE 仍然是最好的选择。
假设这与 InfocentrePlus 中的 ic_dapolicy
视图相反(这显然是 OpenGI 数据,并且 icp 表不那么容易使用)那么您的查询将类似于:
WITH Quotes AS(
SELECT *,
COUNT(*) OVER (PARTITION BY PPY.B@, PPY.Ref@) AS ClientQuotes
FROM ic_Dapolicy PPY
WHERE PPY.Quote_date = @Quote_date) --If you aren't filtering on Quote_date in your where, add it to the PARTITION BY clause
SELECT [YourColumns]
FROM Quotes
WHERE ClientQuotes > 1;
我想你想要像下面这样的东西。
;WITH Rankings AS
(
-- Your query here
SELECT
CountryCode = 1,
ClientRef = 1,
QuoteDate = 1,
PRank = 1
),
Only1Record AS
(
SELECT
R.CountryCode,
R.ClientRef,
R.QuoteDate
FROM
Rankings AS R
GROUP BY
R.CountryCode,
R.ClientRef,
R.QuoteDate
HAVING
MAX(R.PRank) = 1
)
DELETE D FROM
Only1Record AS R
INNER JOIN Rankings AS D ON
R.CountryCode = D.CountryCode AND
R.ClientRef = D.ClientRef AND
R.QuoteDate = D.QuoteDate
另一个更简单的变体是使用窗口COUNT()
。
;WITH Rankings AS
(
-- Your query here
SELECT
CountryCode = 1,
ClientRef = 1,
QuoteDate = 1,
PRank = 1,
Total = COUNT(1) OVER (PARTITION BY CountryCode, ClientRef, QuoteDate)
)
DELETE D FROM
Rankings AS D
WHERE
D.Total = 1
我设法使用 ROW_NUMBER()
获得了一些有序的结果,并按国家代码和客户参考号进行了分区,并按报价日期排序。 ROW_NUMBER()
列称为 PRank。
CountryCode ¦ ClientRef ¦ Quote Ref ¦ Name ¦ Address ¦ PostCode ¦ PreferencesCode ¦ QuoteDate ¦ PolicyType ¦ PRank ¦
0 AABB001 AABB001MB1 Mr A Smith 1 Country Lane WA21 2PU 5934611 2017-03-18 00:00:00 MB 1
0 AABB001 AABBMM1MB2 Mr A Smith 1 Country Lane WA21 2PU 5934611 2017-03-18 00:00:00 MB 2
0 AABB001 AABBMM1MB3 Mr A Smith 1 Country Lane WA21 2PU 5934611 2017-03-18 00:00:00 MB 3
0 BBGG003 BBGG003MB1 Mrs B Jones 2 City Road M1 3XY 312191 2017-09-02 00:00:00 MB 1
这是我的 ROW_NUMBER()
专栏的代码:
ROW_NUMBER() OVER(PARTITION BY CountryCode, ClientRef, QuoteDate
ORDER BY QuoteDate DESC, PolicyRef asc)
有很多像最后一行这样的结果;他们只计为 1,因为客户在相关日期只有一个报价。我想消除这些行,而不从客户结果中删除在同一日期出现多个报价的任何行(即我仍然希望在上面的结果中看到第一个客户的所有三行)所以我想通过使用 CTE 和过滤在 ROW_NUMBER 上会导致我丢失我仍然需要查看的结果中的数据。
我试图用 HAVING 做一些事情并对 PRank 列求和,但我无法弄清楚分组,因为我不想实际聚合数据,只需删除 ClientRef 只有的任何行每天一个 QuoteRef。
如果您正在寻找一天内有超过 1 个报价的客户,CTE 仍然是最好的选择。
假设这与 InfocentrePlus 中的 ic_dapolicy
视图相反(这显然是 OpenGI 数据,并且 icp 表不那么容易使用)那么您的查询将类似于:
WITH Quotes AS(
SELECT *,
COUNT(*) OVER (PARTITION BY PPY.B@, PPY.Ref@) AS ClientQuotes
FROM ic_Dapolicy PPY
WHERE PPY.Quote_date = @Quote_date) --If you aren't filtering on Quote_date in your where, add it to the PARTITION BY clause
SELECT [YourColumns]
FROM Quotes
WHERE ClientQuotes > 1;
我想你想要像下面这样的东西。
;WITH Rankings AS
(
-- Your query here
SELECT
CountryCode = 1,
ClientRef = 1,
QuoteDate = 1,
PRank = 1
),
Only1Record AS
(
SELECT
R.CountryCode,
R.ClientRef,
R.QuoteDate
FROM
Rankings AS R
GROUP BY
R.CountryCode,
R.ClientRef,
R.QuoteDate
HAVING
MAX(R.PRank) = 1
)
DELETE D FROM
Only1Record AS R
INNER JOIN Rankings AS D ON
R.CountryCode = D.CountryCode AND
R.ClientRef = D.ClientRef AND
R.QuoteDate = D.QuoteDate
另一个更简单的变体是使用窗口COUNT()
。
;WITH Rankings AS
(
-- Your query here
SELECT
CountryCode = 1,
ClientRef = 1,
QuoteDate = 1,
PRank = 1,
Total = COUNT(1) OVER (PARTITION BY CountryCode, ClientRef, QuoteDate)
)
DELETE D FROM
Rankings AS D
WHERE
D.Total = 1