日期:2014-05-18  浏览次数:21040 次

根据某列数据相同情况合并DataTable的数据行
担心以下文本格式发帖后显示会很乱,所以做了张合并前、合并后的效果图,请大家下载来看看,感谢
图片地址: http://t.cn/zOl8Bqd 
 或http://s10.sinaimg.cn/middle/4d96ee05gbe7cbdf809d9&690

-----------合并前3条数据的BookName----------------
合并前
BookName DateLoan CardNo
1电脑管理与维护 2010-3-25 2001
2藏地密码 2010-3-25 2001
3档案信息检索 2010-3-25 2001
4射雕英雄传 2010-3-25 2002
5C#编程 2010-4-24 2002

---------下面是我要的合并效果----------------------

合并后
BookName DateLoan CardNo
1电脑管理与维护,2藏地密码,3档案信息检索 2010-3-25 2001
4射雕英雄传 2010-3-25 2002
5C#编程 2010-4-24 2002

-----------------------------------------------------

为了方便给大家的测试,我把测试用的数据库代码放出来:
-----------给网友创建数据表和插入数据调试-------------
SQL code

create table BookLoan
(ID int primary key identity(1,1),
 BookName varchar(200),
 DateLoan datetime, 
 CardNo   varchar(200), 
)
insert into BookLoan(BookName , DateLoan , CardNo  ) values('1电脑管理与维护','2010-3-25','2001')
insert into BookLoan(BookName , DateLoan , CardNo  ) values('2藏地密码','2010-3-25','2001')
insert into BookLoan(BookName , DateLoan , CardNo  ) values('3档案信息检索','2010-3-25','2001')
insert into BookLoan(BookName , DateLoan , CardNo  ) values('4射雕英雄传','2010-3-25','2002')
insert into BookLoan(BookName , DateLoan , CardNo  ) values('5C#编程','2010-4-24','2002')


-------------------------------------------------------

我头脑简单,想得很久都想不出来。先在此感谢大家的帮忙!希望回答的是真实通过测试的,或相仿的成功解决的例子代码也是可以了。千万不要含糊一两句说使用什么技术可以实现等那些废话的回答哦~ 谢谢!!!

------解决方案--------------------
你发错区了,这个应该在sql 区

给你说下思路

1,先写一个sql 函数,参数是 DateLoan,CardNo
,2然后用group by DateLoan,CardNo ,然后调用这个函数就可以了
------解决方案--------------------
SQL code

create table BookLoan
(ID int primary key identity(1,1),
 BookName varchar(200),
 DateLoan datetime,  
 CardNo varchar(200),  
)
insert into BookLoan(BookName , DateLoan , CardNo ) values('1电脑管理与维护','2010-3-25','2001')
insert into BookLoan(BookName , DateLoan , CardNo ) values('2藏地密码','2010-3-25','2001')
insert into BookLoan(BookName , DateLoan , CardNo ) values('3档案信息检索','2010-3-25','2001')
insert into BookLoan(BookName , DateLoan , CardNo ) values('4射雕英雄传','2010-3-25','2002')
insert into BookLoan(BookName , DateLoan , CardNo ) values('5C#编程','2010-4-24','2002')


if object_id('getnum') is not null
drop function getnum


create function getnum(@DateLoan varchar(50),@CardNo varchar(50))
returns varchar(50)
as 
begin
declare @sql varchar(50)
set @sql=''
select @sql=@sql+BookName+',' from BookLoan where DateLoan=@DateLoan AND CardNo=@CardNo
set @sql=@sql
return @sql
end

SELECT DISTINCT LEFT(dbo.getnum(DateLoan,CardNo),LEN(dbo.getnum(DateLoan,CardNo))-1) AS BookName,DateLoan,CardNo FROM BookLoan GROUP BY DateLoan,CardNo,BookName

/*
BookName                                           DateLoan                      CardNo
-------------------------------------------------- -----------------------    ----------------
1电脑管理与维护,2藏地密码,3档案信息检索          2010-03-25 00:00:00.000       2001
4射雕英雄传                                        2010-03-25 00:00:00.000       2002
5C#编程                                           2010-04-24 00:00:00.000       2002

(3 行受影响)
*/

------解决方案--------------------
探讨

引用:

SQL code

create table BookLoan
(ID int primary key identity(1,1),
BookName varchar(200),
DateLoan datetime,
CardNo varchar(200),
)
insert into BookLoan(BookName , DateLoan , ……