--1.创建一个合并的函数
create function fmerg(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+cast(pid as varchar) from 表A where id=@id
set @str=right(@str,len(@str)-1)
return(@str)
go--调用自定义函数得到结果
select distinct id,dbo.fmerg(id) from 表A
create function fmerg(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+cast(pid as varchar) from 表A where id=@id
set @str=right(@str,len(@str)-1)
return(@str)
go--调用自定义函数得到结果
select distinct id,dbo.fmerg(id) from 表A
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+rtrim(pid) from 表A where id=@id
set @str=right(@str,len(@str)-1)
return (@str)
end
go--调用自定义函数得到表B
select distinct id,dbo.getstr(id) into 表B from 表A
select * from 表B
能通过简单的SQL语句实现吗,帮帮忙。
insert into #a (序号,内容) values(1,'aaaa')
insert into #a (序号,内容) values(1,'bbbb')
insert into #a (序号,内容) values(1,'cccc')
insert into #a (序号,内容) values(2,'dddd')
insert into #a (序号,内容) values(2,'dddd')
insert into #a (序号,内容) values(2,'hhhh')
declare @Z varchar(100),@id int
set @id=1
set @z=''
update #a
set @z = 内容 = case when @id=序号 then 内容+ ','+ @z else 内容 end ,@id=序号
from #a
select 序号,max(内容) from #a group by 序号
drop table #a
select id,cast(pid as varchar(8000)) as pid into #temp from 原表名--为数据合并准备变量
declare @id int,@pid varchar(8000)--进行数据合并
update #temp set @pid=case @id when id then @pid+','+pid else pid end
,pid=@pid,@id=id--显示结果
select id,max(pid) from #temp group by id--删除使用的临时表
drop table #temp
select id,cast(pid as varchar(8000)) as pid into #temp from #tb order by id
1.1、一级用户 该版得分小于等于100分
1.2、二级用户 该版得分小于等于500分,大于100分
1.3、三级用户 该版得分小于等于1000分,大于500分
2、该版得分大于1000分的算中级用户
2.1、四级用户 该版得分小于等于2000分,大于1000分
2.2、五级用户 该版得分小于等于5000分,大于2000分
2.3、一星用户 该版得分小于等于10000分,大于5000分
2.4、二星用户 该版得分大于10000分
3、被授予一定称号的会员算是高级用户
3.1、三星会员:在技术总榜前10名上榜一周以上 或其他各类专家(网友专栏、MS全球技术支持、 CSDN技术编辑)
3.2、四星会员:在技术总榜前3名上榜一周以上或者有突出贡献、热心网友
3.3、五星会员:在技术总榜第一名上榜一周以上或者有突出贡献、热心网友
3.4、名家专栏
3.5、csdn管理员
pengdali(大力 V2.0) 是四星会员。
请参考:
http://expert.csdn.net/Expert/topic/1994/1994840.xml?temp=.5102808
http://expert.csdn.net/Expert/topic/1994/1994377.xml?temp=.6759912并up
我列一个例子给你看看吧,我的情况跟你一模一样:我感觉还不错,希望帮的上你的忙
源表 jhb
jh xh zkpc
---- ---------------- -------------------- -----------
51 IC006824 .38 13
51 AD007924 .62 14
51 ID058124 3.96 15
目标表 jhk
jh ds task
51 双 IC006824(0.38/13) ID058124(3.96/15) AD007924(0.62/14)
说明:我在jhk表中预先设置好了 jh 的记录的如果不预先设置,请你用
insert jhk(jh)(select distinct jh from jhb)
好了,接下来就是存储过程,你自己仔细分析吧,不懂的地方发邮件给我吧,我很少上去,今天是半年后第一次上去呢。
源码:(是SQL的)
CREATE PROCEDURE [sp_jhb_jhk]
@pjh char(2)='00'
AS
declare @chmod varchar(10),@xh varchar(18),@nktd varchar(20),@ddate datetime,@nzkpc numeric(10,2)
declare @cday char(2),@oldxh varchar(18),@lprint int,@lprint_pre int
declare @ctask varchar(200),@i int,@quality char(2),@saverq datetime,@tech_sequ varchar(3)
declare @cchmod varchar(10),@cjh char(2),@cxh varchar(18),@cnktd varchar(20),@cdate char(2),@czkpc char(10)
------------------------------------------------------------
START:
if @pjh<>'00'
goto dataprocess --如果机号传递过来,则处理单一机号
declare curs_jhk cursor
for select jh,task
from jhk
order by jh
for update of task ---read only 定义可更新光标[更新列 task ],指向 jhk
---------------------------------------------------------
update jhk
set task=' '
open curs_jhk
fetch curs_jhk
into @cjh,@ctask ---读取数据
--print @cjh-----------,@ctask
while @@fetch_status=0 ---循环读取, -1表示读取完毕
begin
------------------内循环--依次读取 jhb 表中的记录数*****************************************************************
---------------------------------------------------------
dataprocess: ---单个机号处理,加快执行速度
if @pjh<>'00'
begin
set @cjh=@pjh
update jhk set task=' ' where jh=@cjh
end
declare curs_jhb scroll cursor ---定义具有滚动功能的光标
for select chmod,xh,date,nktd,zkpc,quality,saverq,tech_sequ
from jhb
where jh=@cjh and finish=0----定义未完成单一机号的光标
order by jh,sequ---在 jhb 表中,按 jh+sequ 排序
for read only ---定义只读光标,指向 jhb
--------------------------------------------------------
open curs_jhb ---打开光标
fetch curs_jhb
into @chmod,@xh,@ddate,@nktd,@nzkpc,@quality,@saverq,@tech_sequ ---读取参数(读取第一条记录)
--- 当天下午所排的数据
if datepart(hour,@saverq)>12 and convert(char(10),@saverq,101)=convert(char(10),getdate(),101)
set @lprint=1 ---如果为下午的计划,均要重新排刀
else
set @lprint=0
select @oldxh=ltrim(rtrim(@xh)) ---读取型号
select @i=1
select @lprint_pre=1
while @@fetch_status=0 ---循环读取, -1表示读取完毕
begin
--------读取参数并转化为字符型
select @cchmod=ltrim(rtrim(@chmod)) ---读取改刀情况
select @cxh=ltrim(rtrim(@xh)) ---读取型号
if @oldxh=@cxh and @i>1 --处理相邻相同的型号
begin
select @cxh=''
end
else
select @oldxh=@cxh
select @cnktd=ltrim(rtrim(@nktd))---读取纽扣特点
select @i=@i+1
---------------------------------------------------------------------------------
select @czkpc=ltrim(rtrim(convert(varchar(10),@nzkpc))) --读取排产数
---去掉后缀零
select @czkpc=rtrim(@czkpc)
if @czkpc like '%.00'
select @czkpc=left(@czkpc,len(@czkpc)-3)
if @czkpc like '%._0'
select @czkpc=left(@czkpc,len(@czkpc)-1)
----------------------------------------------
select @cdate=ltrim(rtrim(convert(char(2),day(@ddate)))) ---读取日期
----------------------------------------------------------------------
if @ctask<>' '
begin
if @lprint_pre=@lprint and @lprint_pre=1 and datepart(hour,getdate())>12
begin ---- 标记‘||’为备件库设计
select @ctask =ltrim(rtrim(@ctask))+' ||'+@cchmod+ltrim(rtrim(@quality))+@cxh+@cnktd+
case when len(@tech_sequ)>0 then '['+@tech_sequ+']' else '' end+'('+rtrim(@czkpc)+'/'+rtrim(@cdate)+')'
select @lprint_pre=0 ---表示已经处理过
end
else
select @ctask =ltrim(rtrim(@ctask))+' '+@cchmod+ltrim(rtrim(@quality))+@cxh+@cnktd+
case when len(@tech_sequ)>0 then '['+@tech_sequ+']' else '' end+'('+rtrim(@czkpc)+'/'+rtrim(@cdate)+')'
end
else
begin
if @lprint_pre=@lprint and @lprint_pre=1 and datepart(hour,getdate())>12
begin ---- 标记‘||’为备件库设计
select @ctask ='||'+@cchmod+ltrim(rtrim(@quality))+@cxh+@cnktd+
case when len(@tech_sequ)>0 then '['+@tech_sequ+']' else '' end+'('+rtrim(@czkpc)+'/'+rtrim(@cdate)+')'
select @lprint_pre=0 ---表示已经处理过
end
else
select @ctask =@cchmod+ltrim(rtrim(@quality))+@cxh+@cnktd+
case when len(@tech_sequ)>0 then '['+@tech_sequ+']' else '' end+'('+rtrim(@czkpc)+'/'+rtrim(@cdate)+')'
end update jhk
set task =@ctask
where jh=@cjh
-- where current of curs_jhk
IF @@ERROR>0
GOTO ERROR
fetch next from curs_jhb
into @chmod,@xh,@ddate,@nktd,@nzkpc,@quality,@saverq,@tech_sequ ---读取参数
if datepart(hour,@saverq)>12
set @lprint=1
else
set @lprint=0
end
--------select * from jhb---------内循环
close curs_jhb --关闭光标jhb
deallocate curs_jhb ---删除光标
if @pjh<>'00' ---如果有传递过来的机号,处理完毕即退出
return 1
fetch next from curs_jhk
into @cjh,@ctask ---在 jhk 中读取下一条数据
-------------------------***************************************************************************
end
deallocate curs_jhk
-------------------------------------
RETURN 1
error:
close curs_jhb --关闭光标jhb
deallocate curs_jhb
close curs_jhk --关闭光标jhk
deallocate curs_jhk
goto start ---跳到开始处重新执行
-------------------------------------
GO
转换过程中,要出错的,但重新执行一遍就行,所以加了个判断出错语句,出错了,让它重新执行,获得一次执行,就能够成功
declare @YourResultTable table (id varchar(2), value varchar(10))
insert @YourResultTable values('1', 'cool')
insert @YourResultTable values('1', 'nice')
insert @YourResultTable values('1', 'wow')
insert @YourResultTable values('2', 'cool')
insert @YourResultTable values('2', 'wow')
insert @YourResultTable values('4', 'nice')
insert @YourResultTable values('6', 'cool')
insert @YourResultTable values('6', 'nice')--select * from @YourResultTabledeclare @z varchar(100),@q varchar(2)
select @q = nulldeclare @ProcessTable table (id varchar(2), value varchar(100))
insert @ProcessTable
select id, value from @YourResultTable
order by id, valueupdate @ProcessTable
set @z = value = case @q when id then @z else '' end + value +',' , @q = id
from @ProcessTable
--select * from @ProcessTableselect id,max(value) from @ProcessTable group by idset nocount off