SQL查询&对来自不同表的行数据进行分组

时间:2017-11-15 02:34:28

标签: sql sql-server

我有以下查询工作正常,但我试图得到一些合并的行,例如第一列(名称)是完全匹配。我已经尝试使用XML等来修改变量,但似乎无法使其正常工作。

bundle.js

我不会包含它由SELECT部分​​生成的所有列,但是我关注的那些列显示如下:

SELECT        MasterName.Last+ ', ' + MasterName.First+ ', ' 
              +MasterName.Middle AS NameCombined, MasterName.Type, 
              JailIntake.Booking_Status, JailIntake.Intake_Date, 
              JailIntake.Agency, MasterDescriptors.DOB, JailCharge.Suffix_1, 
              JailCharge.Charge_Literal, JailIntake.Incident_Nr, 
              JailCharge.Bond_Amount, Pictures.UnitID, ct500.code, 
              Pictures.IType

FROM          RMS.dbo.MasterName AS MasterName INNER JOIN
              RMS.dbo.MasterDescriptors AS MasterDescriptors ON 
              MasterName.Suffix_1 = MasterDescriptors.Suffix_1 AND 
              MasterName.Incident_Nr = MasterDescriptors.Incident_Nr AND 
              MasterName.Agency = MasterDescriptors.Agency AND 
              MasterName.Module_ID = MasterDescriptors.Module_ID INNER JOIN
              RMS.dbo.JailIntake AS JailIntake ON 
              MasterDescriptors.Module_ID = JailIntake.Module_ID AND 
              MasterDescriptors.Incident_Nr = JailIntake.Incident_Nr AND 
              MasterDescriptors.Agency = JailIntake.Agency AND 
              MasterDescriptors.Suffix_1 = JailIntake.Suffix_1 INNER JOIN
              RMS.dbo.JailCharge AS JailCharge ON JailIntake.Incident_Nr = 
              JailCharge.Incident_Nr AND JailIntake.Agency = 
              JailCharge.Agency AND JailIntake.Module_ID = 
              JailCharge.Module_ID INNER JOIN
              IMAGES.dbo.Pictures ON JailIntake.Incident_Nr = 
              JailCharge.Incident_Nr AND 'ABBV^' + JailIntake.Incident_Nr + 
              '^^^^INT^IN^MNI^' = Pictures.UnitID INNER JOIN
              PIMSTABL.dbo.ct500 AS ct500 ON ct500.code = 
              JailIntake.Inmate_Type
WHERE         (MasterName.Type = 'IN') AND (JailIntake.Booking_Status = 'IJ' 
              OR JailIntake.Booking_Status = 'OF') AND 
              (JailIntake.Agency = 'ABBV') AND (Pictures.IType = '1106')

ORDER BY      MasterName.Last

我的最终目标是让它看起来像:

NAME OF PERSON       CHARGENUMBER     CHARGEDESCRIPTION

Doe, Jane                001               TEXT
Doe, Jane                002               TEXT
Doe, Jane                003               TEXT
Johnson, John            001               TEXT
Johnson, John            002               TEXT
Thompson, Tommy          001               TEXT
Williams, Wendy          001               TEXT
Williams, Wendy          002               TEXT

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

CREATE TABLE #Temp([NAME OF PERSON] VARCHAR(100),CHARGENUMBER VARCHAR(100), CHARGEDESCRIPTION VARCHAR(100))

INSERT INTO #Temp VALUES('Doe, Jane','001','TEXT')
INSERT INTO #Temp VALUES('Doe, Jane','002','TEXT')
INSERT INTO #Temp VALUES('Doe, Jane','003','TEXT')
INSERT INTO #Temp VALUES('Johnson, John','001','TEXT')
INSERT INTO #Temp VALUES('Johnson, John','002','TEXT')
INSERT INTO #Temp VALUES('Thompson, Tommy','001','TEXT')
INSERT INTO #Temp VALUES('Williams, Wendy','001','TEXT')
INSERT INTO #Temp VALUES('Williams, Wendy','002','TEXT')

然后将值更改为逗号分隔。

SELECT
    [NAME OF PERSON],
    STUFF((
        SELECT ', ' + t.CHARGENUMBER
        FROM #Temp t
        WHERE t.[NAME OF PERSON] = T1.[NAME OF PERSON]
        ORDER BY t.CHARGENUMBER
        FOR XMl PATH('')
    ),1,1,'') AS CHARGENUMBER
    ,STUFF((
        SELECT ', ' + t.CHARGEDESCRIPTION
        FROM #Temp t
        WHERE t.[NAME OF PERSON] = T1.[NAME OF PERSON]
        ORDER BY t.CHARGEDESCRIPTION
        FOR XMl PATH('')
    ),1,1,'') AS CHARGEDESCRIPTION
FROM #Temp T1
GROUP BY [NAME OF PERSON];

希望这会对你有所帮助。