日期:2014-05-18  浏览次数:20483 次

没看懂语句,求解释~在线等
泡论坛,看了一条小F姐姐的问题解答,如下:
表T的数据如下,
A B
-------
A1 B1
A1 B2
A2 B1
A3 B2

希望得到
A B
--------
A1 B1
A2 B2
A3
SQL code
;with f as
(
select id=row_number()over(partition by a order by getdate()),* from tb
)
select
  isnull(a.a,'') as a,isnull(b.b,'') as b
from
(select px=row_number()over(order by getdate()),* from (select distinct id,a from f where id=1)t)a
left join
 (select px=row_number()over(order by getdate()),* from (select distinct id,b from f where id=1)t)b
on
 a.px=b.px

惭愧,直接没看懂,上网搜索了一下,都讲的很抽象。
在此求教,那位老师能用直白的语言给逐条解释一下。

------解决方案--------------------
SQL code
;with f as
(
select id=row_number()over(partition by a order by getdate()),* from tb   --按照A列排序
)


select
  isnull(a.a,'') as a,isnull(b.b,'') as b  --A列,B列有值的直接列出,没有的就显示为空字符
from
(select px=row_number()over(order by getdate()),* from (select distinct id,a from f where id=1)t)a  --取出按照A列中的序列(不重复的)
left join --俩个序列进行连接
 (select px=row_number()over(order by getdate()),* from (select distinct id,b from f where id=1)t)b  --取出按照B列中的序列(不重复的)
on
 a.px=b.px