最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。
所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。
1、请教一个存储过程,同批不足指定行数的插行
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行是要插入的行次。)
我的方法:
--补足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、sql 合并id相同的数据
http://bbs.csdn.net/topics/390726775
表A:
id 车号
1 辽A1111
2 辽B2222
表B:
id 表A_id 车号 箱号 封号 客户
1 1 辽A1111 001 001 张三
2 1 辽A1111 002 002 李四
3 2 辽B2222 003 003 王五
通过表A的id和表B的 表A_id实现关联,一条表A的数据可以有一个或者两个箱号,一个箱号可能有多个客户,
实现综合查询 组合成一个新表(不使用函数)。
车号 箱号 封号 箱号 封号 客户
辽A1111 001 001 002 002 张三/李四
辽B2222 003 003 王五
sql server 2000的系统。
这个问题,由于不能用函数,这里我通过分组求max,然后再相加的方法来处理字符串的累加问题。
我的方法:
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 王五 */
生成的动态语句:
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, 车号