'Pivot data in without using pivot and just with join

I have a table like this in SQL Server

name tems aver
a 1 12
a 2 13.5
b 1 19
b 2 15.5
c 2 5
d 1 16.75

How should I have following form with join clause? Not with group by, CTE, COALESCE, Null functions, conditional aggregation and PIVOT. Case clause in only allowed in join conditions.

select columns
from Table T1
join Table T2 on .......................
join Table T3 on .......................
name aver1 aver2
a 12 13.5
b 19 15.5
b Null 5
d 16.75 Null


Solution 1:[1]

..for join...

declare @t table(name varchar(10), tems smallint, aver decimal(9,2));

insert into @t(name, tems, aver)
values
('a', 1, 12),
('a', 2, 13.5),
('b', 1, 19),
('b', 2, 15.5),
('c', 2, 5),
('d', 1, 16.75);

select coalesce(t1.name, t2.name) as name, t1.aver as aver1, t2.aver as aver2
from 
(
select *
from @t
where tems = 1
) as t1
full outer join 
(
select *
from @t
where tems = 2
) as t2 on t1.name = t2.name;


select coalesce(t1.name, t2.name) as name, t1.aver as aver1, t2.aver as aver2
from @t as t1
full outer join @t as t2 on t1.name = t2.name and t1.tems = 1 and t2.tems = 2
where t1.tems = 1
or
(t1.tems is null and t2.tems=2);


select t3.name, t1.aver as aver1, t2.aver as aver2
from @t as t1
full outer join @t as t2 on t1.name = t2.name and t1.tems = 1 and t2.tems = 2
join @t as t3 on case when t1.name is null then t2.name else t1.name end = t3.name
and case when t1.name is null then 2 else 1 end = t3.tems
where t1.tems = 1
or
t1.tems is null and t2.tems=2;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1