将多行字符串合并为单个字符串

时间:2018-08-01 17:04:40

标签: sql sql-server

我有一个预置查询,用于生成组织中的学生列表。我们设置表结构和联接的方式为每个学生提供了多个字符串。这是我的查询:

SELECT 
STU.ID as 'USER_NAME', 
concat(stu.id, '@districtname.org') as 'EXTERNAL_ID',
'' AS 'Password' ,
STU.ID AS 'SIS_ID', 
IIF(STU.ID = '300016659', concat(STU.FN, ' ', substring(stu.mn, 1, 1)), STU.FN) AS 'FIRST_NAME', 
STU.MN as 'MIDDLE_NAME', 
STU.LN as 'LAST_NAME', 
STU.GR as 'GRADE' ,
CASE WHEN (PGM.CD = '190' and PGM.ESD > '2018-07-01') then 'Y'
     WHEN (PGM.CD = '191' and PGM.ESD > '2018-07-01') then 'Y' else 'N' END AS 'AYP_MIGRANT',
CASE WHEN (CSE.DI > '200' and CSE.XD is NULL) then 'Y' else 'N' END AS 'AYP_WITH_DISABILITIES',
CASE WHEN (STU.LF = 'L') then 'Y' else 'N' END AS 'AYP_LTD_ENGLISH_PROFICIENCY',
CASE WHEN (FRE.CD = 'F' and FRE.ESD > '2018-07-01' and FRE.EED is NULL) then 'Y'
     WHEN (FRE.CD = 'R' and FRE.ESD > '2018-07-01' and FRE.EED is NULL) then 'Y' else 'N' END AS 'AYP_ECON_DISADVANTAGED',
CASE STU.SC WHEN 803 THEN 'School A' 
WHEN 804 THEN 'School B'
WHEN 805 THEN 'School C' 
WHEN 806 THEN 'School D'
WHEN 808 THEN 'School E'
WHEN 809 THEN 'School F'
WHEN 810 THEN 'School G'
WHEN 812 THEN 'School H' else '' END AS 'School_Name',
 'MASTER LIST' as 'Class_Name'      
FROM            IDN INNER JOIN  
                         STU ON IDN.ID = STU.ID LEFT JOIN
                         PGM ON IDN.ID = PGM.PID LEFT JOIN
                         FRE on IDN.ID = FRE.ID LEFT JOIN
                         CSE ON IDN.ID = CSE.ID
WHERE         
STU.SC in(803,804,805,806,808,809,810,812)
  and (STU.DEL = 0 and STU.TG = ' ')
  order by USER_NAME

我试图为每个学生生成一个字符串,即使他们在多个表(例如我们的PGM,FRE和CSE)中都有记录。我以前没有使用过东西或XML路径,但是看起来这也许是我最好的选择。这可能与我先前存在的查询有关,还是需要重建此查询才能使用STUFF?

0 个答案:

没有答案