我认为我试图在某些列上过滤类似于DISTINCT的内容而不是其他列。
我有这样的观点:
Name
LastName
Zip
Street1
HouseholdID (may not be unique because it may have multiple addresses -- think of it in the logical sense as grouping persons but not physical locations; If you lookup HouseholdID 4130, you may get two rows.. or more, because the person may have mutiple mailing locations)
City
State
我需要提取所有这些列,但过滤LastName,Zip和Street1。这是有趣的部分:过滤器是任意的 - 这意味着我不关心哪一个副本消失了。这是一个邮件输出类型的东西,其他信息不是用于任何其他原因,而不是在需要时查找特定的人(我不知道为什么)。所以..鉴于其中一条记录,您可以轻松找出已删除的记录。
现在看来,我的Sql-Fu让我失望了,我在C#中过滤了非常慢并且几乎是一个以空列表开头的foreach并在if中添加了行组合的姓氏,邮政编码和街道不在列表中。
我觉得我错过了一个我应该理解的SQL的简单/基本部分。
答案 0 :(得分:4)
解决方案可以假设我们在视图中有唯一标识列:
Select Name,
LastName,
Zip,
Street1,
HouseholdID,
City,
State
From View V1
INNER JOIN
(
Select Min([Unique Identification]) as ID
From View v
Group By LastName,
Zip,
Street ) AS T on T.ID = V1.[Unique Identification]
当视图中没有标识符时.......
Select Name,
LastName,
Zip,
Street1,
HouseholdID,
City,
State
From
(
Select Name,
LastName,
Zip,
Street1,
HouseholdID,
City,
State,
Row_Number() Over (Partition By LastName,Zip,Street Order By Street1,HouseholdID,City,State) AS RowNum
From View v
) AS T
Where T.RowNum = 1