select top 1 table1.姓名,学历,职称
from table1,table2,table3
where table1.姓名=table2.姓名 and table1.姓名=table3.姓名
order by 获奖时间 desc
from table1,table2,table3
where table1.姓名=table2.姓名 and table1.姓名=table3.姓名
order by 获奖时间 desc
调试欢乐多
a.姓名,
获奖等级=(select 获奖等级 from table1 where 获奖时间>a.获奖时间),
学历=(select 学历 from table2 where 学历取得时间>b.学历取得时间),
职称=(select 职称 from table3 where 取得时间>c.取得时间)
from table1 a,table2 b,table3 c
where a.姓名=b.姓名 and a.姓名=c.姓名
from table1 a
left join table2 b on a.姓名 = b.姓名
left join table3 c on a.姓名 = c.姓名
where not exists(select 1 from table1 where 姓名 = a.姓名 and 获奖时间> a. 获奖时间)
and not exists(select 1 from table2 where 姓名 = b.姓名 and 学历取得时间> a. 学历取得时间)
and not exists(select 1 from table3 where 姓名 = c.姓名 and 取得时间> a. 取得时间)
from
(select *,Row_number()over(partition by 姓名 order by 学历取得时间 desc )as cnt
from table2)A,
(select *,Row_number()over(partition by 姓名 order by 取得时间 desc )as cnt
from table3)B,
(select *,Row_number()over(partition by 姓名 order by 获奖时间 desc )as cnt
from table1)C
where A.姓名=b.姓名
and A.姓名=C.姓名
and A.cnt =1
and A.cnt=B.cnt
and A.cnt=C.cnt
select top 1 table1.姓名,学历,职称
from table1,table2,table3
where table1.姓名=table2.姓名 and table1.姓名=table3.姓名 and table1.获奖时间 =table2.获奖时间 and table3.获奖时间 =table2.获奖时间
-- Author: liangCK 小梁
---------------------------------
--> 生成测试数据: @table1
DECLARE @table1 TABLE (姓名 VARCHAR(4),获奖等级 VARCHAR(6),获奖时间 INT)
INSERT INTO @table1
SELECT '王明','一等奖',2009 UNION ALL
SELECT '王明','二等奖',2010 UNION ALL
SELECT '李三','一等奖',2009 UNION ALL
SELECT '李三','二等奖',2010
--> 生成测试数据: @table2
DECLARE @table2 TABLE (姓名 VARCHAR(4),学历 VARCHAR(6),学历取得时间 INT)
INSERT INTO @table2
SELECT '王明','本科',2008 UNION ALL
SELECT '王明','研究生',2009 UNION ALL
SELECT '李三','本科',2008 UNION ALL
SELECT '李三','研究生',2009
--> 生成测试数据: @table3
DECLARE @table3 TABLE (姓名 VARCHAR(4), 职称 VARCHAR(10),取得时间 INT)
INSERT INTO @table3
SELECT '王明','工程师',2005 UNION ALL
SELECT '王明','高级工程师',2006 UNION ALL
SELECT '李三','工程师',2005 UNION ALL
SELECT '李三','高级工程师',2006--SQL查询如下:SELECT
A.姓名,
A.获奖等级,
B.学历,
C.职称
FROM (
SELECT *
FROM @table1 AS A
WHERE NOT EXISTS(SELECT * FROM @table1 WHERE 姓名=A.姓名 AND 获奖时间>A.获奖时间)
) AS A
JOIN (
SELECT *
FROM @table2 AS B
WHERE NOT EXISTS(SELECT * FROM @table2 WHERE 姓名=B.姓名 AND 学历取得时间>B.学历取得时间)
) AS B
ON A.姓名=B.姓名
JOIN (
SELECT *
FROM @table3 AS C
WHERE NOT EXISTS(SELECT * FROM @table3 WHERE 姓名=C.姓名 AND 取得时间>C.取得时间)
) AS C
ON A.姓名=C.姓名/*
姓名 获奖等级 学历 职称
---- ------ ------ ----------
王明 二等奖 研究生 高级工程师
李三 二等奖 研究生 高级工程师(2 行受影响)
*/
t1.获奖等级,
t2.学历,
t3.职称
from
(select t.* from table1 t where 获奖时间 = (select max(获奖时间) from table1 where 姓名 = t.姓名)) t1,
(select t.* from table2 t where 学历取得时间 = (select max(学历取得时间) from table2 where 姓名 = t.姓名)) t2,
(select t.* from table3 t where 取得时间 = (select max(取得时间) from table2 where 姓名 = t.姓名)) t3
where t1.姓名 = t2.姓名 and t1.姓名 = t3.姓名
INSERT INTO table1
SELECT '王明','一等奖',2009 UNION ALL
SELECT '王明','二等奖',2010 UNION ALL
SELECT '李三','一等奖',2009 UNION ALL
SELECT '李三','二等奖',2010
create TABLE table2 (姓名 VARCHAR(4),学历 VARCHAR(6),学历取得时间 INT)
INSERT INTO table2
SELECT '王明','本科',2008 UNION ALL
SELECT '王明','研究生',2009 UNION ALL
SELECT '李三','本科',2008 UNION ALL
SELECT '李三','研究生',2009
create TABLE table3 (姓名 VARCHAR(4), 职称 VARCHAR(10),取得时间 INT)
INSERT INTO table3
SELECT '王明','工程师',2005 UNION ALL
SELECT '王明','高级工程师',2006 UNION ALL
SELECT '李三','工程师',2005 UNION ALL
SELECT '李三','高级工程师',2006select t1.姓名,
t1.获奖等级,
t2.学历,
t3.职称
from
(select t.* from table1 t where 获奖时间 = (select max(获奖时间) from table1 where 姓名 = t.姓名)) t1,
(select t.* from table2 t where 学历取得时间 = (select max(学历取得时间) from table2 where 姓名 = t.姓名)) t2,
(select t.* from table3 t where 取得时间 = (select max(取得时间) from table3 where 姓名 = t.姓名)) t3
where t1.姓名 = t2.姓名 and t1.姓名 = t3.姓名drop table table1,table2,table3/*
姓名 获奖等级 学历 职称
---- ------ ------ ----------
李三 二等奖 研究生 高级工程师
王明 二等奖 研究生 高级工程师(所影响的行数为 2 行)
*/
来个简单的
select a.姓名,a.获奖等级,b.学历,c.职称 from #1 a
join #2 b on a.姓名=b.姓名
join #3 c on a.姓名 = c.姓名
where not exists(select 1 from #1 where 姓名= a.姓名 and 获奖时间 > a.获奖时间)
and not exists(select 1 from #2 where 姓名 = b.姓名 and 学历取得时间 > b.学历取得时间)
and not exists(select 1 from #3 where 姓名= c.姓名 and 取得时间 > c.取得时间)姓名 获奖等级 学历 职称
---- ------ ------ ----------
王明 二等奖 研究生 高级工程师
李三 二等奖 研究生 高级工程师(2 row(s) affected)
SELECT
A.姓名,
A.获奖等级,
B.学历,
C.职称
FROM (
SELECT *
FROM @table1 AS A
WHERE NOT EXISTS(SELECT * FROM @table1 WHERE 姓名=A.姓名 AND 获奖时间>A.获奖时间)
) AS A
JOIN (
SELECT *
FROM @table2 AS B
WHERE NOT EXISTS(SELECT * FROM @table2 WHERE 姓名=B.姓名 AND 学历取得时间>B.学历取得时间)
) AS B
ON A.姓名=B.姓名
JOIN (
SELECT *
FROM @table3 AS C
WHERE NOT EXISTS(SELECT * FROM @table3 WHERE 姓名=C.姓名 AND 取得时间>C.取得时间)
) AS C
ON A.姓名=C.姓名
-- 可以查询全部姓名的最新信息
select t1.姓名, t1.获奖等级, t2.学历, t3.职称 from (
select a1.姓名, a1.获奖等级 from table1 a1
inner join (select 姓名, max(获奖时间) as 获奖时间 from table1 group by 姓名) a2
on a1.姓名=a2.姓名 and a1.获奖时间=a2.获奖时间
) t1 inner join ( select a1.姓名, a1.学历 from table2 a1
inner join (select 姓名, max(学历取得时间) as 学历取得时间 from table2 group by 姓名) a2
on a1.姓名=a2.姓名 and a1.学历取得时间=a2.学历取得时间
) t2on t1.姓名=t2.姓名 inner join (
select a1.姓名, a1.职称 from table3 a1
inner join (select 姓名, max(取得时间) as 取得时间 from table3 group by 姓名) a2
on a1.姓名=a2.姓名 and a1.取得时间=a2.取得时间
) t3on t1.姓名=t3.姓名-- 如果只查询某一个姓名, 请加上: and t1.姓名='王明'