Recently, in the forum, I met a lot more difficult sql problem, although they can be resolved, but found a few days later, they can not remember, forget the solution of.
So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.
1. Ask a stored procedure, lack of the same batch insert rows specified number of rows
http://bbs.csdn.net/topics/390738052
一、表结构如下:
CREATE TABLE [dbo].[Table_test](
[bh] [varchar](20) NULL,
[name] [varchar](50) NULL,
[ye] [decimal](18, 2) NULL
) ON [PRIMARY]
二、测试数据如下:
insert into table_test(bh,name,ye) values('t001','李明',1000)
insert into table_test(bh,name,ye) values('t001','李张',1000)
insert into table_test(bh,name,ye) values('t001','李三',1000)
insert into table_test(bh,name,ye) values('t001','李四',1000)
insert into table_test(bh,name,ye) values('t002','孙明',1100)
insert into table_test(bh,name,ye) values('t002','李达',1100)
insert into table_test(bh,name,ye) values('t003','陈明',1200)
insert into table_test(bh,name,ye) values('t003','刘志',1200)
insert into table_test(bh,name,ye) values('t003','孙华',1200)
三、达到目标:现在是4行为一个批次编号(注4行或5行都可以),同一批次
不足4行的,要插入同批次编号的行。 效果如下:
bh name ye
------------------------------
t001 李明 1000.00
t001 李张 1000.00
t001 李三 1000.00
t001 李四 1000.00
t002 孙明 1100.00
t002 李达 1100.00
t002 NULL NULL
t002 NULL NULL
t003 陈明 1200.00
t003 刘志 1200.00
t003 孙华 1200.00
t003 NULL NULL
(注: 7.8.12行是要插入的行次。)
My method:
-
--补足4条记录
-
insert
into [Table_test]
-
select bh,
null,
null
-
from
-
(
-
select bh,
COUNT(*) c
-
from [Table_test]
-
group
by bh
-
)t,master..spt_values s
-
where s.type =
'P'
and s.number >=
1
and s.number <=
4-c
-
-
-
--再次查询
-
select *
-
from [Table_test]
-
order
by bh,
name
desc
-
/*
-
bh name ye
-
t001 李张 1000.00
-
t001 李四 1000.00
-
t001 李三 1000.00
-
t001 李明 1000.00
-
t002 孙明 1100.00
-
t002 李达 1100.00
-
t002 NULL NULL
-
t002 NULL NULL
-
t003 孙华 1200.00
-
t003 刘志 1200.00
-
t003 陈明 1200.00
-
t003 NULL NULL
-
*/
2, the same combined data sql ID
http://bbs.csdn.net/topics/390726775
Table A:
ID vehicle number
1 Liao A1111
2 Liao B2222
Table B:
ID No. Table title A_id client license plate box
11 A1111 001 Liao 001 Zhang
A1111 002 002 21 John Doe Liao
B2222 003 003 Wang Wu Liao 32
A_id achieved by association table id table a and table B, and a table data a may have one or two box number, a box number there may be multiple clients
to achieve integrated queries are combined into a new table (do not use function).
License plate number Box number Box number title titles client
Liaoning A1111 001 001 002 002 Joe Smith / John Doe
Liao Wang Wu B2222 003 003
sql server 2000 system.
This problem, since the method can not use the function, max where I pray through the packet, and then added to deal with the problem accumulated string.
My method:
-
create
table A(
id
int, 车号
varchar(
20))
-
-
insert
into a
-
select
1 ,
'辽A1111'
union all
-
select
2 ,
'辽B2222'
-
-
-
create
table B(
-
id
int,A_id
int,
-
车号
varchar(
10),
-
箱号
varchar(
10),
-
封号
varchar(
10),
-
客户
varchar(
10))
-
-
insert
into B
-
SELECT
1,
1,
'辽A1111',
'001',
'001',
'张三'
UNION ALL
-
SELECT
2,
1,
'辽A1111',
'002',
'002',
'李四'
UNION ALL
-
SELECT
3,
2,
'辽B2222',
'003',
'003',
'王五'
-
go
-
-
-
if OBJECT_ID(
'tempdb..#temp')
is
not
null
-
drop
table #temp
-
-
select *,
-
(
select
count(*)
from B
where t.A_id = b.A_id
and t.id>=b.id) rn
-
into #temp
-
from B t
-
-
-
declare @
sql
varchar(
4000)
-
declare @sql_t
varchar(
4000)
-
-
set @
sql =
''
-
set @sql_t =
''
-
-
select @
sql = @
sql +
',max(case when rn ='+
CAST(rn
as
varchar)+
' then 箱号 else '''' end) 箱号'
-
+
',max(case when rn ='+
CAST(rn
as
varchar)+
' then 封号 else '''' end) 封号'
-
from #temp
-
group
by rn
-
-
-
select @sql_t = @sql_t +
'+max(case when rn ='+
CAST(rn
as
varchar)+
' then ''/''+客户 else '''' end)'
-
from #temp
-
group
by rn
-
-
-
-
set @
sql =
'select a_id as id,车号'+@
sql +
',stuff('+
stuff(@sql_t,
1,
1,
'')+
',1,1,'''') as 客户'+
-
' from #temp
-
group by a_id,车号'
-
-
exec(@
sql)
-
/*
-
id 车号 箱号 封号 箱号 封号 客户
-
1 辽A1111 001 001 002 002 张三/李四
-
2 辽B2222 003 003 王五
-
*/
Dynamically generated statements:
-
select a_id
as
id,
-
车号,
-
-
max(
case
when rn =
1
then 箱号
else
''
end) 箱号,
-
max(
case
when rn =
1
then 封号
else
''
end) 封号,
-
-
max(
case
when rn =
2
then 箱号
else
''
end) 箱号,
-
max(
case
when rn =
2
then 封号
else
''
end) 封号,
-
-
stuff(
max(
case
when rn =
1
then
'/'+客户
else
''
end)+
-
max(
case
when rn =
2
then
'/'+客户
else
''
end)
-
,
1,
1,
'')
as 客户
-
from #temp
-
group
by a_id,
-
车号