简体   繁体   English

在包含列数据类型(例如ntext)的联接查询中从一个表中获取唯一值

[英]Get Distinct values from one table in a join query containing column data type like ntext

I have two tables Review and ProjectsReview . 我有两个表ReviewProjectsReview 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.

相关问题 选择SQL Query从ntext列获取xml节点值? - Select SQL Query to get xml node values from ntext column? 联接中另一个表的不同列值 - DISTINCT COLUMN VALUES FROM ANOTHER TABLE IN JOIN 如何从联接查询中获取一列的DISINCT值 - How to get DISINCT values of one column from a join query 从列表获取自定义不同值 - Get custom Distinct Values from column table 从一个表中选择不同的值并连接到另一个表 - Select distinct values from one table and join with another table 当同一表的另一列中存在多于1个不同值时,用于从一列中计算不同值的SQL查询 - SQL Query for Counting Distinct Values From One Column When More Than 1 Distinct Value exists in Another Column in the same table SQL Query获取表中所有列值的不同值 - SQL Query to get Distinct Values of all column values in a table PostgreSQL外连接查询从一个表中获取所有数据 - postgresql outer join query to get all data from one table SQL 查询从一个表中获取不同的结果并将这些结果与表中的另一列匹配 - SQL Query to get distinct results from one table and match those results to the another column in the table 无需连接即可将一个表中的不同值循环到另一个表 - Looping distinct values from one table through another without a join
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM