1

嗨,我有两张表,一张有县信息,另一张有 RowData

Country 表看起来像

Country_ID  Region_ID   Country_Name              Abbreviation Priority
----------- ----------- ------------------------- ------------ -----------
1           1           United States             USA          1
2           1           US                        USA          1
3           1           Canada                    CA           2
4           2           United Kingdom            UK           1
5           2           Germany                   DE           1
6           2           France                    FR           1
7           2           Italy                     IT           1
8           2           Spain                     ES           1
9           2           The Netherlands           NL           1
10          2           Poland                    PL           2
11          2           Russia                    RU           2

和 RowData 就像

Tr_ID       Countrys
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
729541      Germany,United Kingdom
729553      Italy,Poland,Russia,Spain
729580      Austria,Finland,France,Germany,Ireland,Italy,Poland,Portugal,Romania,Russia,Slovakia,Spain,Sweden,Switzerland,The Netherlands,United Kingdom
729726      Italy,Spain,United Kingdom
729737      Austria,United Kingdom

我想根据县表中给出的优先级重新排列县

优先级 1 县应该出现在第一个铰接国家下一个用逗号重复 请帮我解决这个问题

谢谢

4

1 回答 1

2

SQL小提琴

MS SQL Server 2008 架构设置

create table Country
(
  Country_ID int,
  Region_ID int,
  Country_Name varchar(25),
  Abbreviation varchar(5),
  Priority int 
);

insert into Country values
(1 ,  1,   'United States',   'USA', 1),
(2 ,  1,   'US',              'USA', 1),
(3 ,  1,   'Canada',          'CA ', 2),
(4 ,  2,   'United Kingdom',  'UK ', 1),
(5 ,  2,   'Germany',         'DE ', 1),
(6 ,  2,   'France',          'FR ', 1),
(7 ,  2,   'Italy',           'IT ', 1),
(8 ,  2,   'Spain',           'ES ', 1),
(9 ,  2,   'The Netherlands', 'NL ', 1),
(10,  2,   'Poland',          'PL ', 2),
(11,  2,   'Russia',          'RU ', 2)

create table RowData
(
  Tr_ID int,
  Countrys varchar(200)
)

insert into RowData values
(729541,  'Germany,United Kingdom'),
(729553,  'Italy,Poland,Russia,Spain'),
(729580,  'Austria,Finland,France,Germany,Ireland,Italy,Poland,Portugal,Romania,Russia,Slovakia,Spain,Sweden,Switzerland,The Netherlands,United Kingdom'),
(729726,  'Italy,Spain,United Kingdom'),
(729737,  'Austria,United Kingdom')

查询 1

select R.Tr_ID,
       (
       select ','+C.Country_Name
       from Country as C
       where ','+R.Countrys+',' like '%,'+C.Country_Name+',%'
       order by C.Priority
       for xml path(''), type
       ).value('substring(text()[1], 2)', 'varchar(200)') as Countrys
from RowData as R

结果

|  TR_ID |                                                                COUNTRYS |
------------------------------------------------------------------------------------
| 729541 |                                                  United Kingdom,Germany |
| 729553 |                                               Italy,Spain,Poland,Russia |
| 729580 | United Kingdom,Germany,France,Italy,Spain,The Netherlands,Poland,Russia |
| 729726 |                                              United Kingdom,Italy,Spain |
| 729737 |                                                          United Kingdom |
于 2013-01-09T11:51:06.827 回答