首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问:怎么实现这样的查询

2012-02-14 
请教:如何实现这样的查询?表:ICstorckbill物料名称批次数量a00120b00230c00310d00440上面有4条记录,现在客

请教:如何实现这样的查询?
表: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 行受影响)

热点排行
Bad Request.