一:有個原始數據表,存儲格式如下:
單據編號 次數 狀態1 狀態2 排序
200807001 10000 Y 1
200807001 20000 Y 2
200807001 30000 Y 3
200807001 40000 Y 4
200807001 50000 Y 5
200807001 60000 Y 6 二:我要得到如下表(目標表):
次數 狀態1 狀態2 次數 狀態1 狀態2
10000 Y 40000 Y
20000 Y 50000 Y
30000 Y 60000 Y
要求按記錄的行數來顯示,如果原始數據表有6行記錄,則目標表顯示三行,順序按次數由小到大排列,並且原始數據表的
次數的值是變化的
單據編號 次數 狀態1 狀態2 排序
200807001 10000 Y 1
200807001 20000 Y 2
200807001 30000 Y 3
200807001 40000 Y 4
200807001 50000 Y 5
200807001 60000 Y 6 二:我要得到如下表(目標表):
次數 狀態1 狀態2 次數 狀態1 狀態2
10000 Y 40000 Y
20000 Y 50000 Y
30000 Y 60000 Y
要求按記錄的行數來顯示,如果原始數據表有6行記錄,則目標表顯示三行,順序按次數由小到大排列,並且原始數據表的
次數的值是變化的
from tab a left join tab b
on b.排序=a.排序+CEILING((select max(排序) from tab)*1.0/2)
where a.a.排序<=CEILING((select max(排序) from tab)*1.0/2)
order by a.排序
declare @tb table([單據編號] int,[次數] int,[狀態1] nvarchar(1),[排序] int)
Insert @tb
select 200807001,10000,N'Y',1 union all
select 200807001,20000,N'Y',2 union all
select 200807001,30000,N'Y',3 union all
select 200807001,40000,N'Y',4 union all
select 200807001,50000,N'Y',5 union all
select 200807001,60000,N'Y',6
Select [單據編號], max(case ([排序]-1)%2 when 0 then [次數] end ) [次數],
max(case ([排序]-1)%2 when 0 then [狀態1] end ) [狀態1],
max(case ([排序]-1)%2 when 1 then [次數] end ) [次數2],
max(case ([排序]-1)%2 when 1 then [狀態1] end ) [狀態2]
from @tb
group by [單據編號],([排序]-1)/2
/*
單據編號 次數 狀態1 次數2 狀態2
----------- ----------- ---- ----------- ----
200807001 10000 Y 20000 Y
200807001 30000 Y 40000 Y
200807001 50000 Y 60000 Y
*/
insert into @t
select 200807001,10000,'Y','',1 union
select 200807001,20000,'Y','',2 union
select 200807001,30000,'Y','',3 union
select 200807001,40000,'Y','',4 union
select 200807001,50000,'Y','',5 union
select 200807001,60000,'Y','',6 select a.次數,a.狀態1,a.狀態2,b.次數,b.狀態1,b.狀態2
from @t a
left join @t b on a.單據編號=b.單據編號 and a.排序+1=b.排序
where a.排序%2=1/*
次數 狀態1 狀態2 次數 狀態1 狀態2
----------- ---- ---- ----------- ---- ----
10000 Y 20000 Y
30000 Y 40000 Y
50000 Y 60000 Y (3 行受影响)*/
insert @t select
'200807001',10000,'Y',null,1
union all select
'200807001',20000,'Y',null,2
union all select
'200807001',30000,'Y',null,3
union all select
'200807001',40000,'Y',null,4
union all select
'200807001',50000,'Y',null,5
union all select
'200807001',60000,'Y',null,6select a.次數,a.狀態1,a.狀態2,b.次數,b.狀態1,b.狀態2
from @t a left join @t b
on b.排序=a.排序+CEILING((select max(排序) from @t)*1.0/2)
where a.排序<=CEILING((select max(排序) from @t)*1.0/2)
order by a.排序--结果次數 狀態1 狀態2 次數 狀態1 狀態2
----------- ---------- ---------- ----------- ---------- ----------
10000 Y NULL 40000 Y NULL
20000 Y NULL 50000 Y NULL
30000 Y NULL 60000 Y NULL(所影响的行数为 3 行)
from @t a
left join @t b on a.單據編號=b.單據編號 and a.排序+1=b.排序
where a.排序%2=1
insert @t select
'200807001',10000,'Y',null,1
union all select
'200807001',20000,'Y',null,2
union all select
'200807001',30000,'Y',null,3
union all select
'200807001',40000,'Y',null,4
union all select
'200807001',50000,'Y',null,5
union all select
'200807001',60000,'Y',null,6
union all select
'200807001',70000,'Y',null,7select a.次數,a.狀態1,a.狀態2,b.次數,b.狀態1,b.狀態2
from @t a left join @t b
on b.排序=a.排序+CEILING((select max(排序) from @t)*1.0/2)
where a.排序<=CEILING((select max(排序) from @t)*1.0/2)
order by a.排序--结果次數 狀態1 狀態2 次數 狀態1 狀態2
----------- ---------- ---------- ----------- ---------- ----------
10000 Y NULL 50000 Y NULL
20000 Y NULL 60000 Y NULL
30000 Y NULL 70000 Y NULL
40000 Y NULL NULL NULL NULL(所影响的行数为 4 行)