SQL 合并多行記錄的相同字段值
發(fā)布日期:2022-01-30 10:39 | 文章來(lái)源:源碼中國(guó)
2.從數(shù)據(jù)庫(kù)中一次讀取數(shù)據(jù)到一張表中返回并顯示到UI層。說(shuō)起來(lái)誰(shuí)都想這么做,但是以前太笨,沒(méi)有去研究這個(gè),今天因?yàn)閿?shù)據(jù)量較大的原因,讓我不得不想些其他辦法來(lái)提高點(diǎn)效率。
Google~hk一下,果真有答案,然后依葫蘆畫瓢,自己寫了一個(gè)
目的是獲取不定量的符合條件的兼職記錄,并將每個(gè)兼職參與項(xiàng)目情況記錄到某幾個(gè)字段當(dāng)中,然后一次返回Table
復(fù)制代碼 代碼如下:
Create function Fn_GetJobListByPID--創(chuàng)建自定義函數(shù)獲取指定兼職參與的所有項(xiàng)目編號(hào)及項(xiàng)目數(shù)量
(
@ParttimerID int
)
returns @t table(Jobs varchar(5000),ParttimerID int,TotalCount int)
as
begin
declare @sql varchar(5000),@TotalCount int
set @sql=''
set @TotalCount=0
select @sql=@sql+j.JobNo+'-'+j.JobWave+' ',@TotalCount=@TotalCount+1
from ONJB_JobApplication a,ONJB_Jobs j
where a.ParttimerID=@ParttimerID
and a.Result='V'
and a.JobID=j.JobID
insert @t values(@sql,@ParttimerID,@TotalCount)
return
end
引用
復(fù)制代碼 代碼如下:
--...........................
--做過(guò)項(xiàng)目
left join (select Jobs,ParttimerID,TotalCount From Fn_GetJobListByPID(@ParttimerID)) as j1
on p.ParttimerID=j1.ParttimerID
--在做項(xiàng)目
left join (select CurJobs,ParttimerID,CurCount From Fn_GetCurJobsByPID(@ParttimerID)) as j2
on p.ParttimerID=j2.ParttimerID
where p.ParttimerID=@ParttimerID
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章