在论坛中出现的比较难的sql问题:26(动态行专列+合并字符串、补足行数)

来源:互联网 发布:wkwebview 启动优化 编辑:程序博客网 时间:2024/04/30 11:00

最近,在论坛中,遇到了不少比较难的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.00t001李张1000.00t001李三1000.00t001李四1000.00t002孙明1100.00t002李达1100.00t002NULLNULL        t002NULLNULLt003陈明1200.00t003刘志1200.00t003孙华1200.00t003NULLNULL(注: 7.8.12行是要插入的行次。)

我的方法:

--补足4条记录insert into [Table_test]select bh,null,nullfrom (select bh,COUNT(*) cfrom [Table_test]group by bh)t,master..spt_values swhere s.type = 'P' and s.number >=1 and s.number <= 4-c--再次查询select *from [Table_test]order by bh,name desc/*bhnameyet001李张1000.00t001李四1000.00t001李三1000.00t001李明1000.00t002孙明1100.00t002李达1100.00t002NULLNULLt002NULLNULLt003孙华1200.00t003刘志1200.00t003陈明1200.00t003NULLNULL*/


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 allselect 2      ,'辽B2222'create table B(id int,A_id int,   车号 varchar(10),箱号 varchar(10), 封号 varchar(10),客户 varchar(10))insert into BSELECT 1,1,'辽A1111','001','001','张三' UNION ALLSELECT 2,1,'辽A1111','002','002','李四' UNION ALLSELECT 3,2,'辽B2222','003','003','王五'goif OBJECT_ID('tempdb..#temp') is not null  drop table #tempselect *,       (select count(*) from B where t.A_id = b.A_id and t.id>=b.id) rn       into #tempfrom B tdeclare @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 #tempgroup by rnselect @sql_t = @sql_t + '+max(case when rn ='+CAST(rn as varchar)+' then ''/''+客户 else '''' end)'from #tempgroup by rnset @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辽A1111001001002002张三/李四2辽B2222003003王五*/  

生成的动态语句:

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,         车号


0 0
原创粉丝点击