Question

I have the following:

bvagnt  bvname
A123A   Level 1 
A123G   Level 2
A123M   Level 3
A333A   Level 1 

I want to pivot if the bvagnt baset on if bvagnt starts and end with A or start with A and ends with G or starts with A and ends with G.

The result should be the follwoing

A%A      A%G      A%M
Level 1  Level 2  Level 3
Level 1  NULL     NULL
Was it helpful?

Solution

Try the code below.

select 
bvagnt,
[Level 1] as [A%A],
[Level 2] as [A%G],
[Level 3] as [A%M]
from
(
select left(bvagnt, LEN(bvagnt) - 1) as bvagnt, bvname
from tablez
) as src
pivot(
max(bvname)
for bvname in ([Level 1], [Level 2], [Level 3]) 
) as piv
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top