简体   繁体   English

SQL Server - INNER JOIN WITH DISTINCT

[英]SQL Server - INNER JOIN WITH DISTINCT

I am having a hard time doing the following:我很难做到以下几点:

select a.FirstName, a.LastName, v.District
from AddTbl a order by Firstname
inner join (select distinct LastName from
            ValTbl v  where a.LastName = v.LastName)  

I want to do a join on ValTbl but only for distinct values.我想对 ValTbl 进行连接,但仅适用于不同的值。

Try this:尝试这个:

select distinct a.FirstName, a.LastName, v.District
from AddTbl a 
  inner join ValTbl v
  on a.LastName = v.LastName
order by a.FirstName;

Or this (it does the same, but the syntax is different):或者这个(它的作用相同,但语法不同):

select distinct a.FirstName, a.LastName, v.District
from AddTbl a, ValTbl v
where a.LastName = v.LastName
order by a.FirstName;

I think you actually provided a good start for the correct answer right in your question (you just need the correct syntax).我认为您实际上为您的问题中的正确答案提供了一个良好的开端(您只需要正确的语法)。 I had this exact same problem, and putting DISTINCT in a sub-query was indeed less costly than what other answers here have proposed.我遇到了完全相同的问题,并且将 DISTINCT 放在子查询中确实比此处提出的其他答案成本更低。

select a.FirstName, a.LastName, v.District
from AddTbl a 
inner join (select distinct LastName, District 
    from ValTbl) v
   on a.LastName = v.LastName
order by Firstname   

You can use CTE to get the distinct values of the second table, and then join that with the first table.您可以使用 CTE 获取第二个表的不同值,然后将其与第一个表连接。 You also need to get the distinct values based on LastName column.您还需要根据 LastName 列获取不同的值。 You do this with a Row_Number() partitioned by the LastName, and sorted by the FirstName.您可以使用按姓氏分区并按名字排序的 Row_Number() 执行此操作。

Here's the code这是代码

;WITH SecondTableWithDistinctLastName AS
(
        SELECT  *
        FROM    (
                    SELECT  *,
                            ROW_NUMBER() OVER (PARTITION BY LastName ORDER BY FirstName) AS [Rank]
                    FROM    AddTbl
                )   
        AS      tableWithRank
        WHERE   tableWithRank.[Rank] = 1
) 
SELECT          a.FirstName, a.LastName, S.District
FROM            SecondTableWithDistinctLastName AS S
INNER JOIN      AddTbl AS a
    ON          a.LastName = S.LastName
ORDER   BY      a.FirstName

add "distinct" after "select".在“选择”之后添加“不同”。

select distinct a.FirstName, a.LastName, v.District , v.LastName
from AddTbl a 
inner join ValTbl v  where a.LastName = v.LastName  order by Firstname

It's not the same doing a select distinct at the beginning because you are wasting all the calculated rows from the result.在开始时执行 select distinct 是不一样的,因为您正在浪费结果中的所有计算行。

select a.FirstName, a.LastName, v.District
from AddTbl a order by Firstname
natural join (select distinct LastName from
            ValTbl v  where a.LastName = v.LastName)

try that.试试那个。

select distinct a.FirstName, a.LastName, v.District from AddTbl a inner join ValTbl v on a.LastName = v.LastName order by a.FirstName;选择不同的 a.FirstName, a.LastName, v.District from AddTbl a 内连接 ValTbl v on a.LastName = v.LastName order by a.FirstName;

hope this helps希望这可以帮助

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

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