简体   繁体   English

SQL:按所有列选择不同的行,但忽略一列(例如ID列)

[英]SQL : Select Distinct rows by all columns but omit one column (say ID column)

I am constructing a view by joining few tables to extract the data via Entity framework. 我通过连接几个表以通过Entity Framework提取数据来构建视图。 As this view is having no unique column, EntityFramework could not retrive the correct result set (ie the first column is getting repeated). 由于此视图没有唯一列,因此EntityFramework无法检索正确的结果集(即,第一列正在重复)。

To resolve this I have added one ID column ie the ROW_NUMBER() & made necessary changes in my .net code (Entity) 为了解决这个问题,我添加了一个ID列,即ROW_NUMBER(),并在.net代码(实体)中进行了必要的更改

Refer : Entity Framework. 请参阅:实体框架。 View return duplicate records 查看返回重复记录

Now real problem striked me, as this ID column atributed Unique identity to the earlier duplicate rows too :|. 现在,真正的问题困扰了我,因为此ID列也为之前的重复行分配了唯一标识:|。

I want to retrive only distinct rows without considering this ID / identity column in to picture. 我只想检索不同的行,而不考虑图片中的此ID /标识列。

For example : 例如 :

+--------+---------+-------------+-------------+
| ID     | NUMBER  | COUNTRY     | LANG        |
+--------+---------+-------------+-------------+
| 1      | 3968    | UK          | English     |
| 2      | 3968    | UK          | English     |
| 3      | 1234    | Greece      | Greek       |
| 4      | 1234    | Italy       | Italian     |

The retrival should happen like : (just take only one entry out of fisrt & second row, as they are repeated, if we ignore the ID column) 检索应该像这样进行:(如果忽略ID列,只需从fisrt和第二行中取出一个条目,因为它们是重复的,所以)

+--------+---------+-------------+-------------+
| ID     | NUMBER  | COUNTRY     | LANG        |
+--------+---------+-------------+-------------+
| 1      | 3968    | UK          | English     |
| 3      | 1234    | Greece      | Greek       |
| 4      | 1234    | Italy       | Italian     |

I have used some dummy columns above to illustrate my problem easily, else my real query looks something like : 我在上面使用了一些虚拟列来轻松说明我的问题,否则我的真实查询看起来像:

Create VIEW [dbo].[vw_PlanDetails]
AS
SELECT **DISTINCT** ROW_NUMBER() OVER (ORDER BY PLAN_CODE_LONG) ID, PM.PLAN_CODE_LONG,     SA.SERVICE_AREA_NAME, SAC.COUNTY_NAME
FROM             PLAN_MASTER AS PM INNER JOIN ...

... ... ……

If required I can paste the whole query, but that might complicate the question. 如果需要,我可以粘贴整个查询,但这会使问题复杂化。 So expecting a generic answer based on my dummy tables. 因此,期待基于我的虚拟表的通用答案。

SELECT min(id) id, number, country, lang
FROM PLAN_MASTER
GROUP BY number, country, lang

I think you should use another function DENSE_RANK(), then use the row_number function like this way : 我认为您应该使用另一个函数DENSE_RANK(),然后像这样使用row_number函数:

SELECT *
FROM(
SELECT 
ROW_NUMBER() OVER (ORDER BY PLAN_CODE_LONG) ROW_NUMBER_ID
,DENSE_RANK() OVER (ORDER BY PLAN_CODE_LONG) DENSE_RANK_ID
,PM.PLAN_CODE_LONG,     SA.SERVICE_AREA_NAME, SAC.COUNTY_NAME
FROM             PLAN_MASTER AS PM INNER JOIN ...
) AS Temp
WHERE 
ROW_NUMBER_ID = DENSE_RANK_ID

I hope thiw will help you 希望thiw能帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM