我有一个表格yrstphn,其记录如下:
| yr | st | phn |
| 2012 | NV | 1234 |
| 2012 | NV | 2361 |
| 2012 | MD | 1234 |
| 2012 | MD | 2361 |
| 2012 | MA | 1234 |
| 2012 | MA | 2361 |
我想通过使用程序(sql或cursor)获得以下内容:
| yr | st | phn |
| 2012 | NV | 1234 |
| null | MD | 2361 |
| null | MA | null |
欢迎任何建议。
答案 0 :(得分:1)
您似乎希望将每列视为单独的列表。这不是真正的关系,但您可以在SQL中执行:
select max(yr) as yr, max(st) as st, max(phn) as phn
from ((select row_number() over (order by yr) as seqnum, yr, null as st, null as phn
from t
group by year
) union all
(select row_number() over (order by yr) as seqnum, NULL, st, NULL
from t
group by year
) union all
(select row_number() over (order by yr) as seqnum, NULL, NULL, phn
from t
group by year
)
) t
group by seqnum
order by seqnum;