我想在一个字段中合并来自多个记录的字段值

时间:2018-07-18 09:13:32

标签: sql

我想在一个字段中合并来自多个记录的字段值。例如:

stud_admno  Student  Class  InstallName HeadName    Remain. Amt
4361        DEWANG    I-A   Sep-17      Fine         200
4361        DEWANG    I-A   Nov-17     Tution Fee    60
4361        DEWANG    I-A   Dec-17     Tution Fee    35

结果应如下所示:

stud_admno  Student  Class      InstallName        HeadName       Remain. Amt
   ------------------------------------------------------------------------------
    4361        DEWANG    I-A   Sep-17,Nov-17,Dec-17  Tution Fee,Fine        295

我正在通过此查询获取此数据。...

select b.stud_admno,case when  Stud_MiddleName='' then (Stud_FirstName+' '+Stud_LastName) else (Stud_FirstName+' '+Stud_MiddleName+' '+Stud_LastName) end  as Student,
c.ClassName+'-'+d.SecName as Class,f.InstallName,g.HeadName,sum(RemainBal)as [Remain. Amt]
 from StudentDue a
left outer join AdmissionMaster b
on a.Stud_UID=b.Stud_UID
left outer join ClassMaster c
on c.ClassID=a.Class
left outer join SectionMaster d
on a.Sec=d.SecID
left outer join ConcMaster e
on a.ConcID=e.ConcID
left outer join InstallmentMaster f
on b.Stud_FeeStruct=f.FeeStructID and a.DueDate=f.InstallDueDate
left outer join FeeHeadMaster g
on a.FeeHead=g.HeadID
where b.nso=0 and b.tc=0 and a.Stud_AdmNo='4361'

group by a.Stud_UID,b.Stud_AdmNo,
b.Stud_MiddleName,b.Stud_firstName,b.Stud_lastName,b.Father_FirstName,b.Father_MiddleName,b.Father_LastName,
a.Class,c.ClassName,d.SecName,[Close],a.DueDate,f.InstallName,a.Struct,b.Father_MoblieNo, g.HeadName
having [Close]=0 and GETDATE()>DueDate  
order by a.Class,b.Stud_AdmNo 

0 个答案:

没有答案