SQL DISTINCT在除一列以外的多列上,合并结果中的最后一列

时间:2018-08-23 13:55:28

标签: sql sql-server sql-server-2014

我有一个学生数据库,我需要给每个家庭寄一封信,但地址上必须有姓氏。同一家庭中可能有多个姓氏,因此我需要分别提及每个姓氏,但仍只向该地址发送一封信。 SQL 2014。

样本数据:

    +------------+----------------+-------+-------+-------+
    |  LastName  |   Address 1    | City  | State |  Zip  |
    +------------+----------------+-------+-------+-------+
    |  Smith     | 123 Fake St.   |  NY   |  NY   | 12345 |
    |  Jones     | 123 Fake St.   |  NY   |  NY   | 12345 |
    |  Ball      | 123 North St.  |  NY   |  NY   | 12345 |
    |  Wood      | 123 South St.  |  NY   |  NY   | 12345 |
    |  Ball      | 123 South St.  |  NY   |  NY   | 12345 |
    +------------+----------------+-------+-------+-------+

我需要退货:

    +-------------------+----------------+-------+-------+-------+
    |  LastName         |   Address 1    | City  | State |  Zip  |
    +-------------------+----------------+-------+-------+-------+
    |  Smith & Jones    | 123 Fake St.   |  NY   |  NY   | 12345 |
    |  Ball             | 123 North St.  |  NY   |  NY   | 12345 |
    |  Wood & Ball      | 123 South St.  |  NY   |  NY   | 12345 |
    +-------------------+----------------+-------+-------+-------+

编辑-感谢@TanvirArjel,我使用了以下最终查询,该查询比我的原始帖子所建议的要复杂一些。希望这对其他人有帮助!

    SELECT STUFF((
        SELECT DISTINCT ',' + LastName
        FROM Users
        WHERE (
                CASE 
                    WHEN Student_MailUnitNumber IS NULL
                        THEN CONCAT (
                                Student_MailStreetNumber
                                ,' '
                                ,Student_MailStreetName
                                )
                    ELSE CONCAT (
                            Student_MailStreetNumber
                            ,' '
                            ,Student_MailStreetName
                            ,', '
                            ,Student_MailUnitNumber
                            )
                    END
                ) = a.Address1
            AND City = a.City
            AND [State] = a.[State]
            AND Zip = a.Zip
        FOR XML PATH('')
        ), 1, 1, '') AS LastName
,Address1
,City
,STATE
,Zip
    FROM (
SELECT LastName
    ,CASE 
        WHEN Student_MailUnitNumber IS NULL
            THEN CONCAT (
                    Student_MailStreetNumber
                    ,' '
                    ,Student_MailStreetName
                    )
        ELSE CONCAT (
                Student_MailStreetNumber
                ,' '
                ,Student_MailStreetName
                ,', '
                ,Student_MailUnitNumber
                )
        END AS [Address1]
    ,Student_MailCity AS [City]
    ,Student_MailState AS [State]
    ,Student_MailZip AS [Zip]
FROM users
WHERE usertype = 'Student'
    AND isActive = 1
    AND PriBuilding IS NOT NULL
    AND student_grade NOT LIKE '%K%'
    AND Student_Grade <> '1'
) AS A
    GROUP BY Address1
,City
,STATE
,Zip

1 个答案:

答案 0 :(得分:3)

尝试一下:

SELECT Address1,City,State,Zip,
     STUFF(
         (SELECT DISTINCT ',' + LastName
          FROM TableName
          WHERE Address1 = a.Address1 AND City= a.City AND State = a.State and Zip = a.Sip
          FOR XML PATH (''))
          , 1, 1, '')  AS LastName
FROM TableName AS a
GROUP BY Address1, City,State,Zip