合并 SQL 服务器中的 table 列
Combining table column in SQL Server
当 Customer_address
相同 address_index
但不同 line_nbr
时,我必须合并它们:
CUSTOMER_NUM
CUSTOMER_CODE
CUSTOMER_ADDRSS
LINE_NBR
ADDRESS_INDEX
ADDRESS_CODE
31
GEW
3901 Castle Hayne Road
17
1
SHIP
30
GEW
Highway 117 N
18
1
SHIP
212
GEW
1005 2nd Street
19
2
SHIP
23
GEW
3901 Castle Hayne Road
20
3
SHIP
241
GEW
Highway 117 N
21
3
SHIP
360
GEW
465 McCarthy Road
1
4
SHIP
469
GEW
3901 Castle Hayne Road
1
5
SHIP
470
GEW
Highway 117 N
2
5
SHIP
我需要结合CUSTOMER_NUM 31和30阅读
Castle Hayne Road Highway 117 N
通过组合 Address_index = 1
与 Line_nbr
17 和 18,以及 Address_index = 3
与 line_nbr 20 和 21,以及 Address_index 5 与 line_nbr 1 和 2,等等......
SQL 的新手,正在尝试弄清楚如何 link 将两列放在一起并在一列而不是当前所在的两行中写出客户地址。
在 SQL Server 2017+ 上,STRING_AGG()
使这变得简单:
SELECT CUSTOMER_CODE,
MergedAddress = STRING_AGG(CUSTOMER_ADDRSS, ' ')
WITHIN GROUP (ORDER BY LINE_NBR)
FROM dbo.MyTable
GROUP BY CUSTOMER_CODE, ADDRESS_INDEX;
输出:
CUSTOMER_CODE
MergedAddress
GEW
3901 Castle Hayne Road Highway 117 N
GEW
1005 2nd Street
GEW
3901 Castle Hayne Road Highway 117 N
GEW
465 McCarthy Road
GEW
3901 Castle Hayne Road Highway 117 N
在旧版本上,您将不得不使用更麻烦且效率低下的方法,例如:
SELECT CUSTOMER_CODE,
MergedAddress = RTRIM((SELECT CUSTOMER_ADDRSS + ' '
FROM dbo.MyTable AS inside
WHERE inside.ADDRESS_INDEX = outside.ADDRESS_INDEX
ORDER BY LINE_NBR
FOR XML PATH(''), TYPE).value(N'./text()[1]', N'varchar(max)'))
FROM dbo.MyTable AS outside
GROUP BY CUSTOMER_CODE, ADDRESS_INDEX;
example db<>fiddle 显示了结果以及应该有助于解释为什么后者如此糟糕的计划。
当 Customer_address
相同 address_index
但不同 line_nbr
时,我必须合并它们:
CUSTOMER_NUM | CUSTOMER_CODE | CUSTOMER_ADDRSS | LINE_NBR | ADDRESS_INDEX | ADDRESS_CODE |
---|---|---|---|---|---|
31 | GEW | 3901 Castle Hayne Road | 17 | 1 | SHIP |
30 | GEW | Highway 117 N | 18 | 1 | SHIP |
212 | GEW | 1005 2nd Street | 19 | 2 | SHIP |
23 | GEW | 3901 Castle Hayne Road | 20 | 3 | SHIP |
241 | GEW | Highway 117 N | 21 | 3 | SHIP |
360 | GEW | 465 McCarthy Road | 1 | 4 | SHIP |
469 | GEW | 3901 Castle Hayne Road | 1 | 5 | SHIP |
470 | GEW | Highway 117 N | 2 | 5 | SHIP |
我需要结合CUSTOMER_NUM 31和30阅读
Castle Hayne Road Highway 117 N
通过组合 Address_index = 1
与 Line_nbr
17 和 18,以及 Address_index = 3
与 line_nbr 20 和 21,以及 Address_index 5 与 line_nbr 1 和 2,等等......
SQL 的新手,正在尝试弄清楚如何 link 将两列放在一起并在一列而不是当前所在的两行中写出客户地址。
在 SQL Server 2017+ 上,STRING_AGG()
使这变得简单:
SELECT CUSTOMER_CODE,
MergedAddress = STRING_AGG(CUSTOMER_ADDRSS, ' ')
WITHIN GROUP (ORDER BY LINE_NBR)
FROM dbo.MyTable
GROUP BY CUSTOMER_CODE, ADDRESS_INDEX;
输出:
CUSTOMER_CODE | MergedAddress |
---|---|
GEW | 3901 Castle Hayne Road Highway 117 N |
GEW | 1005 2nd Street |
GEW | 3901 Castle Hayne Road Highway 117 N |
GEW | 465 McCarthy Road |
GEW | 3901 Castle Hayne Road Highway 117 N |
在旧版本上,您将不得不使用更麻烦且效率低下的方法,例如:
SELECT CUSTOMER_CODE,
MergedAddress = RTRIM((SELECT CUSTOMER_ADDRSS + ' '
FROM dbo.MyTable AS inside
WHERE inside.ADDRESS_INDEX = outside.ADDRESS_INDEX
ORDER BY LINE_NBR
FOR XML PATH(''), TYPE).value(N'./text()[1]', N'varchar(max)'))
FROM dbo.MyTable AS outside
GROUP BY CUSTOMER_CODE, ADDRESS_INDEX;
example db<>fiddle 显示了结果以及应该有助于解释为什么后者如此糟糕的计划。