SQL - 删除重复项,使用 HAVING 子句和 NTEXT 列选择最近的
SQL - Removing duplicates, selecting most recent using HAVING clause & an NTEXT column
我查询 return 的客户回复。响应 table (rp) 加入到客户 table (ah)。查询return是我想要的结果。
但是,如果记录了多个有效响应,则每个客户 return 的响应超过 1 个。
所以我试图通过添加一个 HAVING 子句来限制它,但我无法与 responsefreetxt 分组,因为它是 NTEXT
。
SELECT
ah.CUSTACCOUNT, /--- type = CHAR
rp.RESPONSEFREETXT /--- type = NTEXT
FROM
ah
INNER JOIN
rp ON rp.CUSTACCOUNT = ah.CUSTACCOUNT
...
WHERE
ah.ACTIVE = 1
AND rp.ACTIVE = 1
AND ...
GROUP BY
ah.CUSTACCOUNT, rp.RESPONSEFREETXT
HAVING
COUNT(ah.CUSTACCOUNT) = 1
ORDER BY
rp.date DESC
导致此错误:
Msg 306, Level 16, State 2, Line 21
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
理想情况下,我希望每个客户 return 1 行,在重复的情况下,只有 return 最近的响应。
我该怎么做?
谢谢!
从 SQL 服务器处理此问题的一种方法是使用 ROW_NUMBER
:
WITH cte AS (
SELECT ah.CUSTACCOUNT, rp.RESPONSEFREETXT,
ROW_NUMBER() OVER (PARTITION BY ah.CUSTACCOUNT
ORDER BY rp.SOME_DATE DESC) rn
FROM ah
INNER JOIN rp ON rp.CUSTACCOUNT = ah.CUSTACCOUNT
...
WHERE ah.ACTIVE = 1 AND rp.ACTIVE = 1 AND ...
-- I have removed the GROUP BY clause
)
SELECT *
FROM cte
WHERE rn = 1;
这假设在响应 table 中存在一个名为 SOME_DATE
的列,它跟踪每个客户响应的时间戳。编写上述查询的另一种方法是使用 TOP 1 WITH TIES
:
SELECT TOP 1 WITH TIES ah.CUSTACCOUNT, rp.RESPONSEFREETXT
FROM ah
INNER JOIN rp ON rp.CUSTACCOUNT = ah.CUSTACCOUNT
...
WHERE ah.ACTIVE = 1 AND rp.ACTIVE = 1 AND ...
ORDER BY ROW_NUMBER() OVER (PARTITION BY ah.CUSTACCOUNT
ORDER BY rp.SOME_DATE DESC);
我查询 return 的客户回复。响应 table (rp) 加入到客户 table (ah)。查询return是我想要的结果。
但是,如果记录了多个有效响应,则每个客户 return 的响应超过 1 个。
所以我试图通过添加一个 HAVING 子句来限制它,但我无法与 responsefreetxt 分组,因为它是 NTEXT
。
SELECT
ah.CUSTACCOUNT, /--- type = CHAR
rp.RESPONSEFREETXT /--- type = NTEXT
FROM
ah
INNER JOIN
rp ON rp.CUSTACCOUNT = ah.CUSTACCOUNT
...
WHERE
ah.ACTIVE = 1
AND rp.ACTIVE = 1
AND ...
GROUP BY
ah.CUSTACCOUNT, rp.RESPONSEFREETXT
HAVING
COUNT(ah.CUSTACCOUNT) = 1
ORDER BY
rp.date DESC
导致此错误:
Msg 306, Level 16, State 2, Line 21
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
理想情况下,我希望每个客户 return 1 行,在重复的情况下,只有 return 最近的响应。
我该怎么做?
谢谢!
从 SQL 服务器处理此问题的一种方法是使用 ROW_NUMBER
:
WITH cte AS (
SELECT ah.CUSTACCOUNT, rp.RESPONSEFREETXT,
ROW_NUMBER() OVER (PARTITION BY ah.CUSTACCOUNT
ORDER BY rp.SOME_DATE DESC) rn
FROM ah
INNER JOIN rp ON rp.CUSTACCOUNT = ah.CUSTACCOUNT
...
WHERE ah.ACTIVE = 1 AND rp.ACTIVE = 1 AND ...
-- I have removed the GROUP BY clause
)
SELECT *
FROM cte
WHERE rn = 1;
这假设在响应 table 中存在一个名为 SOME_DATE
的列,它跟踪每个客户响应的时间戳。编写上述查询的另一种方法是使用 TOP 1 WITH TIES
:
SELECT TOP 1 WITH TIES ah.CUSTACCOUNT, rp.RESPONSEFREETXT
FROM ah
INNER JOIN rp ON rp.CUSTACCOUNT = ah.CUSTACCOUNT
...
WHERE ah.ACTIVE = 1 AND rp.ACTIVE = 1 AND ...
ORDER BY ROW_NUMBER() OVER (PARTITION BY ah.CUSTACCOUNT
ORDER BY rp.SOME_DATE DESC);