Merge duplicate records into 1 field

后端 未结 3 853
陌清茗
陌清茗 2021-01-15 01:39

I have a database table that contains a list of contacts, some of those contacts might have multiple records, e.g.

CustomerID, CustomerName, Vehicle
1, Dan,          


        
3条回答
  •  心在旅途
    2021-01-15 02:18

    I'm pretty sure you can't do it via a single SQL query.

    If it's a regular requirement, a Function should be made which is called for each customer ID.

    SELECT   abc.CustomerID,
             dbo.udf_getCSVCustomerVehicles(abc.customerID)
    FROM     (SELECT DISTINCT CustomerID
              FROM   TABLE) abc
    ORDER BY abc.CustomerID
    

    Then just create a scalar function which coalesces the values together into a variable and returns the result.

提交回复
热议问题