Martin Andersen
Martin Andersen

Reputation: 313

T-SQL combine tables

I want a+b to get to C. How do I combine A and B ?

Thanks :)

Table A

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

Table 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

Upvotes: 1

Views: 58

Answers (3)

clurd
clurd

Reputation: 136

They're the same table, so you just need to combine the logic of both queries. See below for an example.

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

Upvotes: 1

GMB
GMB

Reputation: 222402

Are you just looking for this ?

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

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269503

You just select both expressions in a single select:

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

Upvotes: 1

Related Questions