Table:Test有下列数据: ID=〉int identity(1,1)
ID Name
1 aaaa
2 bbbb
3 cccc
4 dddd
5 eeee
6 ffff
请问怎样才能输出下列样式呢?
ID Name ID Name
1 aaaa 2 bbbb
3 cccc 4 dddd
5 eeee 6 ffff
WITH CTE AS
(
SELECT [ID],[Name],ROW_NUMBER() OVER (ORDER BY [ID]) AS [Num]
FROM [Test]
)
SELECT t1.ID, t1.Name, t2.ID, t2.Name
FROM CTE AS t1
LEFT OUTER JOIN CTE AS t2
ON t1.Num = t2.Num - 1
WHERE t1.Num % 2 = 1
select a.ID,a.Name,b.ID,b.Name from Test a inner join Test b on a.ID = b.ID -1 and a.ID in(1,3,5) and b.ID in(2,4,6)
貌似可以实现你说的情况吧!你试试!
1楼的貌似有问题啊
id identity 如果删除过记录的话 那么 on a.ID = b.ID -1 会使有些记录查不出。
总之是个比较山寨的法子啊……
2楼的好快啊……