T-SQL合并表

时间:2019-02-24 18:43:07

标签: sql sql-server tsql

我希望a + b到达C。如何合并A和B?

谢谢:)

表A

SELECT init, MIN(p.aarstal) AS startyear
FROM placering p
GROUP BY init
ORDER BY startyear

表B

SELECT init, MAX(p.aarstal) AS endyear
FROM placering p
GROUP BY init 
ORDER BY endyear

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:1)

您只需在一个select中选择两个表达式:

select init, min(p.aarstal) as startyear, max(p.aarstal) as endyear
from placering p
group BY init
order by startyear

答案 1 :(得分:1)

您只是在寻找这个吗?

SELECT init, min(p.aarstal) as startyear, max(p.aarstal) as endyear
FROM placering p
GROUP BY init
ORDER BY startyear, endyear

答案 2 :(得分:1)

它们是同一张表,因此您只需要结合两个查询的逻辑即可。参见下面的示例。

SELECT 
  p.init, 
  min(p.aarstal) as startyear, 
  max(p.aarstal) as endyear
FROM placering p
GROUP BY init
ORDER BY startyear, endyear