sql - 如何选择只有一个不同列的多个列来连接多个表

时间:2014-09-15 14:47:48

标签: sql sql-server sql-server-2008

我正在使用SQL Server。我想选择只有一个不同列的多个列。

例如,

表1:

ID    NAME  ...(other columns)
 1     A
 2     B
 3     C

表2(ID和数字一起是唯一键):

ID    Number         Year...(other columns)
1      111           2011
2      12345678      2011
2      22222222      2012
3      333           2013

表3:

Name  Company       ...(other columns)
 A      Amazon    
 B      Google   
 C      Amazon    

上面的每个表都有很多列(超过2个)。如何得到结果,结果只有5列没有其他"无用"列和ID列是不同的列。

更具体地说,例如, 我有正常的sql语句如下:

select distinct ID, NAME, NUMBER, COMPANY, Year
from table1
left join table2 on table1.ID = table2.ID
left join table3 on table1.name = table3.name
group by ID, NAME, NUMBER, COMPANY, year
order by ID desc, Year desc

这将输出以下内容:

ID   NAME NUMBER        COMPANY  YEAR
1    A     111          Amazon   2011
2    B     12345678     google   2011
2    B     22222222     google   2012
3    c     333          Amazon   2013

我想要的实际上是以下内容:

ID   NAME NUMBER        COMPANY  YEAR
1    A     111          Amazon   2011
2    B     22222222     google   2012
3    c     333          Amazon   2013   

我希望结果没有重复的ID。如果有重复的ID,我想只显示最新的ID。在上面的示例中,ID 2在table2中有2行。我想展示最新日期是2012年的那个。 我怎样才能做到这一点。提前谢谢。

3 个答案:

答案 0 :(得分:1)

您可以使用not exists仅选择每个ID的最新行(其中具有相同ID和更大年份的另一行不存在)。

select * from table1 t1
where not exists (
    select 1 from table1 t2
    where t2.id = t1.id
    and t2.year > t1.year
)

使用分析函数(这应该比上面的查询更快)

select * from 
  (select *,
    row_number() over(partition by id order by year desc) rn
   from table1) t1 where rn = 1

编辑:应用于您的表格

select t2.id, t3.name, t2.number, t3.company, t2.year from
(
    select * from 
    (select *,
      row_number() over(partition by id order by year desc) rn
      from table2
    ) t1 where rn = 1
) t2 join table1 t1 on t2.id = t1.id
join table3 t3 on t3.name = t1.name

答案 1 :(得分:0)

WITH CTE AS 
(
  SELECT t1.ID, t1.NAME, t2.NUMBER, t3.COMPANY, t2.Year,
         Row_number() OVER(partition BY t1.ID, t1.NAME, t2.NUMBER, t3.COMPANY ORDER BY t2.Year DESC) AS rn
  FROM table1 t1
    LEFT JOIN table2 t2 ON t1.ID = t2.ID
    LEFT JOIN table3 t3 ON t1.name = t3.name
)
SELECT ID, NAME, NUMBER, COMPANY, Year
FROM CTE
WHERE rownum = 1
ORDER BY ID desc, Year desc

答案 2 :(得分:0)

我使用子查询,注意子查询效率低。

select distinct t1.ID, t1.NAME, t2.NUMBER, t3.COMPANY, t2.Year
from table1 t1
left join table2 t2 on t1.ID = t2.ID                        
inner join table3 t3 on t1.name = t3.name --inner join to select the latest record only
                    and t2.Year = (Select MAX(year) from table2 t22 
                                     where t22.ID = t2.Id group by ID)    
group by t1.ID, t1.NAME, t2.NUMBER, t3.COMPANY, t2.year
order by t1.ID, t2.Year desc

编辑:使用更有效的CTE

WITH CTE as 
(
Select Id, MAX(year) as [yr] from table2 t2 group by ID
) 
select distinct t1.ID, t1.NAME, t2.NUMBER, t3.COMPANY, t2.Year
from table1 t1
left join table2 t2 on t1.ID = t2.ID                        
left join table3 t3 on t1.name = t3.name
inner join CTE on cte.yr = t2.Year
                  and t2.Id = CTE.Id            
group by t1.ID, t1.NAME, t2.NUMBER, t3.COMPANY, t2.year
order by t1.ID, t2.Year desc