[英]Get Distinct values from one table in a join query containing column data type like ntext
I have two tables Review
and ProjectsReview
. 我有两个表
Review
和ProjectsReview
。 I want to change the order by columns without impacting the result. 我想按列更改顺序,而不会影响结果。 Initial order by was on
createdDate
column from review table. 最初的订单依据是评论表中的
createdDate
列。 Initial query is as below. 初始查询如下。
SELECT
*
FROM Review r
WHERE (status IS NULL
OR fstatus = '')
AND (crBy = '100'
OR crByPr = '')
ORDER BY createdDate
The query returns 8 rows. 查询返回8行。
The user wants to change the order by using program name which is in another table. 用户想通过使用另一个表中的程序名称来更改顺序。 The query to get the same is as below.
获取相同的查询如下。
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
This returns 10 rows. 这将返回10行。 But the required result is only 8 rows and only columns of review table.
但是所需的结果只有8行,而只有列的复查表。 I cannot apply group by on all the columns from Review table since there are data types with image and ntext.
由于存在带有image和ntext的数据类型,因此我无法在Review表的所有列上应用group by。 Is there a way to achieve this without inserting the data to a temp table and get distinct values.
有没有一种方法可以在不将数据插入临时表并获得不同值的情况下实现这一目标。
Try this 尝试这个
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.