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

请教:如何实现这样的查询?
表:ICstorckbill
物料名称       批次     数量
    a                 001         20
    b                 002         30
    c                 003         10
    d                 004         40

上面有4条记录,现在客户要求把查询出来的结果分成2半,
每边2条记录,效果如下
物料名称       批次     数量       物料名称1       批次1     数量1        
    a                 001         20             c                   003         10
    b                 002         30             d                   004         40    

请教各位,谢谢!!



------解决方案--------------------
create table ICstorckbill(物料名称 varchar(20), 批次 varchar(20), 数量 int)
insert ICstorckbill select 'a ', '001 ', 20
union all select 'b ', '002 ', 30
union all select 'c ', '003 ', 10
union all select 'd ', '004 ', 40

select ID=identity(int, 1, 1), * into #T from ICstorckbill

select * from
(
select * from #T
where ID%2=1
)A
left join
(
select * from #T
where ID%2=0
)B on A.ID=B.ID-1
------解决方案--------------------
declare @ta table(物料名称 varchar(2), 批次 varchar(5), 数量 int)
insert @ta select 'a ', '001 ', 20
insert @ta select 'b ', '002 ', 30
insert @ta select 'c ', '003 ', 10
insert @ta select 'd ', '004 ', 40

select * from @ta a join @ta b on a.物料名称!=b.物料名称
and a.批次%2=b.批次%2 and b.批次> a.批次
物料名称 批次 数量 物料名称 批次 数量
---- ----- ----------- ---- ----- -----------
a 001 20 c 003 10
b 002 30 d 004 40

(2 行受影响)