我有两张桌子
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行且只有评论表的列。 我无法对Review表中的所有列应用group by,因为有data和ntext的数据类型。 有没有办法实现这一点,而无需将数据插入临时表并获得不同的值。
答案 0 :(得分:1)
试试这个
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