[英]Get Distinct values from one table in a join query containing column data type like ntext
我有兩個表Review
和ProjectsReview
。 我想按列更改順序,而不會影響結果。 最初的訂單依據是評論表中的createdDate
列。 初始查詢如下。
SELECT
*
FROM Review r
WHERE (status IS NULL
OR fstatus = '')
AND (crBy = '100'
OR crByPr = '')
ORDER BY createdDate
查詢返回8行。
用戶想通過使用另一個表中的程序名稱來更改順序。 獲取相同的查詢如下。
SELECT
r.*
FROM Review r
INNER JOIN ProjectsReview rp
ON rp.rID = r.rID
WHERE (status IS NULL
OR fstatus = '')
AND (crBy = '100'
OR crByPr = '')
ORDER BY prNo, prName
這將返回10行。 但是所需的結果只有8行,而只有列的復查表。 由於存在帶有image和ntext的數據類型,因此我無法在Review表的所有列上應用group by。 有沒有一種方法可以在不將數據插入臨時表並獲得不同值的情況下實現這一目標。
嘗試這個
with cte
as
(
select
rn = row_number() over(partition by rID order by prNo,prName),
rID,
prNo,
prName
from ProjectsReview
)
SELECT r.*
FROM Review r
inner join cte rp on rp.rID =r.rID
WHERE (status IS NULL OR fstatus = '') AND (crBy = '100' OR crByPr = '')
and cte.rn = 1
ORDER BY prNo,prName
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.