我有表books
ID 条形码 分类号
1 1001 A
2 1002 B
4 1003 C
5 1005 D
6 1006 E
7 1007 F
8 1008 G
想通过sql语句,实现下面六列显示效果
条形码1 分类号1 条形码2 分类号2 条形码3 分类号3
1001 A 1002 B 1003 C
1005 D 1006 E 1007 F
1008 G
希望高手解答,在此感谢各位!
ID 条形码 分类号
1 1001 A
2 1002 B
4 1003 C
5 1005 D
6 1006 E
7 1007 F
8 1008 G
想通过sql语句,实现下面六列显示效果
条形码1 分类号1 条形码2 分类号2 条形码3 分类号3
1001 A 1002 B 1003 C
1005 D 1006 E 1007 F
1008 G
希望高手解答,在此感谢各位!
--sql 2000
select
max(case (px - 1) % 3 when 0 then 条形码 else '' end) [条形码1],
max(case (px - 1) % 3 when 0 then 分类号 else '' end) [分类号1],
max(case (px - 1) % 3 when 1 then 条形码 else '' end) [条形码2],
max(case (px - 1) % 3 when 1 then 分类号 else '' end) [分类号2],
max(case (px - 1) % 3 when 2 then 条形码 else '' end) [条形码3],
max(case (px - 1) % 3 when 2 then 分类号 else '' end) [分类号3]
from
(
select t.* , px = (select count(1) from books where id < t.id) + 1 from books t
) m
group by (px - 1) / 3--sql 2005
select
max(case (px - 1) % 3 when 0 then 条形码 else '' end) [条形码1],
max(case (px - 1) % 3 when 0 then 分类号 else '' end) [分类号1],
max(case (px - 1) % 3 when 1 then 条形码 else '' end) [条形码2],
max(case (px - 1) % 3 when 1 then 分类号 else '' end) [分类号2],
max(case (px - 1) % 3 when 2 then 条形码 else '' end) [条形码3],
max(case (px - 1) % 3 when 2 then 分类号 else '' end) [分类号3]
from
(
select t.* , px = row_number() over(order by id) from books t
) m
group by (px - 1) / 3
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-04-22 10:26:51
-- Verstion:
-- Microsoft SQL Server 2005 - 9.00.4053.00 (Intel X86)
-- May 26 2009 14:24:20
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[books]
if object_id('[books]') is not null drop table [books]
go
create table [books]([ID] int,[条形码] int,[分类号] varchar(1))
insert [books]
select 1,1001,'A' union all
select 2,1002,'B' union all
select 4,1003,'C' union all
select 5,1005,'D' union all
select 6,1006,'E' union all
select 7,1007,'F' union all
select 8,1008,'G'
--------------开始查询--------------------------
select
(id0-1)/3,
max(case when (id0-1)%3=0 then 条形码 else '' end) as '条形码1',
max(case when (id0-1)%3=1 then 条形码 else '' end) as '条形码2',
max(case when (id0-1)%3=2 then 条形码 else '' end) as '条形码3'
from
(select id0=row_number()over(order by getdate()),* from books)t
group by
(id0-1)/3
----------------结果----------------------------
/* 条形码1 条形码2 条形码3
-------------------- ----------- ----------- -----------
0 1001 1002 1003
1 1005 1006 1007
2 1008 0 0(3 行受影响)*/
create table books(ID int,条形码 varchar(10),分类号 varchar(10))
insert into books values( 1 ,'1001', 'A')
insert into books values( 2 ,'1002', 'B')
insert into books values( 4 ,'1003', 'C')
insert into books values( 5 ,'1005', 'D')
insert into books values( 6 ,'1006', 'E')
insert into books values( 7 ,'1007', 'F')
insert into books values( 8 ,'1008', 'G')
go--sql 2000
select
max(case (px - 1) % 3 when 0 then 条形码 else '' end) [条形码1],
max(case (px - 1) % 3 when 0 then 分类号 else '' end) [分类号1],
max(case (px - 1) % 3 when 1 then 条形码 else '' end) [条形码2],
max(case (px - 1) % 3 when 1 then 分类号 else '' end) [分类号2],
max(case (px - 1) % 3 when 2 then 条形码 else '' end) [条形码3],
max(case (px - 1) % 3 when 2 then 分类号 else '' end) [分类号3]
from
(
select t.* , px = (select count(1) from books where id < t.id) + 1 from books t
) m
group by (px - 1) / 3drop table books/*
条形码1 分类号1 条形码2 分类号2 条形码3 分类号3
---------- ---------- ---------- ---------- ---------- ----------
1001 A 1002 B 1003 C
1005 D 1006 E 1007 F
1008 G (所影响的行数为 3 行)
*/
create table books(ID int,条形码 varchar(10),分类号 varchar(10))
insert into books values( 1 ,'1001', 'A')
insert into books values( 2 ,'1002', 'B')
insert into books values( 4 ,'1003', 'C')
insert into books values( 5 ,'1005', 'D')
insert into books values( 6 ,'1006', 'E')
insert into books values( 7 ,'1007', 'F')
insert into books values( 8 ,'1008', 'G')
go--sql 2005
select
max(case (px - 1) % 3 when 0 then 条形码 else '' end) [条形码1],
max(case (px - 1) % 3 when 0 then 分类号 else '' end) [分类号1],
max(case (px - 1) % 3 when 1 then 条形码 else '' end) [条形码2],
max(case (px - 1) % 3 when 1 then 分类号 else '' end) [分类号2],
max(case (px - 1) % 3 when 2 then 条形码 else '' end) [条形码3],
max(case (px - 1) % 3 when 2 then 分类号 else '' end) [分类号3]
from
(
select t.* , px = row_number() over(order by id) from books t
) m
group by (px - 1) / 3drop table books/*
条形码1 分类号1 条形码2 分类号2 条形码3 分类号3
---------- ---------- ---------- ---------- ---------- ----------
1001 A 1002 B 1003 C
1005 D 1006 E 1007 F
1008 G (3 行受影响)*/
drop table tb
Go
Create table tb([ID] int,[条形码] int,[分类号] nvarchar(1))
Insert tb
select 1,1001,N'A' union all
select 2,1002,N'B' union all
select 4,1003,N'C' union all
select 5,1005,N'D' union all
select 6,1006,N'E' union all
select 7,1007,N'F' union all
select 8,1008,N'G'
Go
alter table tb
add cnt int identity
go
Select [条形码1]=max(case when ([cnt]-1)%3=0 then [条形码] else '' end),
[分类号1]=max(case when ([cnt]-1)%3=0 then 分类号 else '' end),
[条形码1]=max(case when ([cnt]-1)%3=1 then [条形码] else '' end),
[分类号1]=max(case when ([cnt]-1)%3=1 then 分类号 else '' end),
[条形码1]=max(case when ([cnt]-1)%3=2 then [条形码] else '' end),
[分类号1]=max(case when ([cnt]-1)%3=2 then 分类号 else '' end)
from tb
group by ([cnt]-1)/3
/*
条形码1 分类号1 条形码1 分类号1 条形码1 分类号1
----------- ---- ----------- ---- ----------- ----
1001 A 1002 B 1003 C
1005 D 1006 E 1007 F
1008 G 0 0 */
insert into books values( 1 ,'1001', 'A')
insert into books values( 2 ,'1002', 'B')
insert into books values( 4 ,'1003', 'C')
insert into books values( 5 ,'1005', 'D')
insert into books values( 6 ,'1006', 'E')
insert into books values( 7 ,'1007', 'F')
insert into books values( 8 ,'1008', 'G')
go--sql 2000
select
max(case (px - 1) % 3 when 0 then 条形码 else '' end) [条形码1],
max(case (px - 1) % 3 when 0 then 分类号 else '' end) [分类号1],
max(case (px - 1) % 3 when 1 then 条形码 else '' end) [条形码2],
max(case (px - 1) % 3 when 1 then 分类号 else '' end) [分类号2],
max(case (px - 1) % 3 when 2 then 条形码 else '' end) [条形码3],
max(case (px - 1) % 3 when 2 then 分类号 else '' end) [分类号3]
from
(
select t.* , px = (select count(1) from books where id < t.id) + 1 from books t
) m
group by (px - 1) / 3drop table books/*
条形码1 分类号1 条形码2 分类号2 条形码3 分类号3
---------- ---------- ---------- ---------- ---------- ----------
1001 A 1002 B 1003 C
1005 D 1006 E 1007 F
1008 G (所影响的行数为 3 行)
*/