Appear in the forum more difficult question sql: 26 (dynamic line train + merge strings, make up the number of rows)

Original: appearing in the forums more difficult question sql: 26 (dynamic line train + merge strings, make up the number of rows)

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:


   
   
  1. --补足4条记录
  2. insert into [Table_test]
  3. select bh, null, null
  4. from
  5. (
  6. select bh, COUNT(*) c
  7. from [Table_test]
  8. group by bh
  9. )t,master..spt_values s
  10. where s.type = 'P' and s.number >= 1 and s.number <= 4-c
  11. --再次查询
  12. select *
  13. from [Table_test]
  14. order by bh, name desc
  15. /*
  16. bh name ye
  17. t001 李张 1000.00
  18. t001 李四 1000.00
  19. t001 李三 1000.00
  20. t001 李明 1000.00
  21. t002 孙明 1100.00
  22. t002 李达 1100.00
  23. t002 NULL NULL
  24. t002 NULL NULL
  25. t003 孙华 1200.00
  26. t003 刘志 1200.00
  27. t003 陈明 1200.00
  28. t003 NULL NULL
  29. */


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:


   
   
  1. create table A( id int, 车号 varchar( 20))
  2. insert into a
  3. select 1 , '辽A1111' union all
  4. select 2 , '辽B2222'
  5. create table B(
  6. id int,A_id int,
  7. 车号 varchar( 10),
  8. 箱号 varchar( 10),
  9. 封号 varchar( 10),
  10. 客户 varchar( 10))
  11. insert into B
  12. SELECT 1, 1, '辽A1111', '001', '001', '张三' UNION ALL
  13. SELECT 2, 1, '辽A1111', '002', '002', '李四' UNION ALL
  14. SELECT 3, 2, '辽B2222', '003', '003', '王五'
  15. go
  16. if OBJECT_ID( 'tempdb..#temp') is not null
  17. drop table #temp
  18. select *,
  19. ( select count(*) from B where t.A_id = b.A_id and t.id>=b.id) rn
  20. into #temp
  21. from B t
  22. declare @ sql varchar( 4000)
  23. declare @sql_t varchar( 4000)
  24. set @ sql = ''
  25. set @sql_t = ''
  26. select @ sql = @ sql + ',max(case when rn ='+ CAST(rn as varchar)+ ' then 箱号 else '''' end) 箱号'
  27. + ',max(case when rn ='+ CAST(rn as varchar)+ ' then 封号 else '''' end) 封号'
  28. from #temp
  29. group by rn
  30. select @sql_t = @sql_t + '+max(case when rn ='+ CAST(rn as varchar)+ ' then ''/''+客户 else '''' end)'
  31. from #temp
  32. group by rn
  33. set @ sql = 'select a_id as id,车号'+@ sql + ',stuff('+ stuff(@sql_t, 1, 1, '')+ ',1,1,'''') as 客户'+
  34. ' from #temp
  35. group by a_id,车号'
  36. exec(@ sql)
  37. /*
  38. id 车号 箱号 封号 箱号 封号 客户
  39. 1 辽A1111 001 001 002 002 张三/李四
  40. 2 辽B2222 003 003 王五
  41. */

Dynamically generated statements:


   
   
  1. select a_id as id,
  2. 车号,
  3. max( case when rn = 1 then 箱号 else '' end) 箱号,
  4. max( case when rn = 1 then 封号 else '' end) 封号,
  5. max( case when rn = 2 then 箱号 else '' end) 箱号,
  6. max( case when rn = 2 then 封号 else '' end) 封号,
  7. stuff( max( case when rn = 1 then '/'+客户 else '' end)+
  8. max( case when rn = 2 then '/'+客户 else '' end)
  9. , 1, 1, '') as 客户
  10. from #temp
  11. group by a_id,
  12. 车号


Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12020046.html