[英]SQL query combine rows based on common id
I have a table with the below structure:我有一个具有以下结构的表:
MID![]() |
FromCountry![]() |
FromState![]() |
FromCity![]() |
FromAddress![]() |
FromNumber![]() |
FromApartment![]() |
ToCountry![]() |
ToCity![]() |
ToAddress![]() |
ToNumber![]() |
ToApartment![]() |
---|---|---|---|---|---|---|---|---|---|---|---|
123 ![]() |
USA![]() |
Texas![]() |
Houston![]() |
Well Street![]() |
1 ![]() |
Japan![]() |
Tokyo![]() |
6 ![]() |
ET3 ![]() |
||
123 ![]() |
Germany![]() |
Bremen![]() |
Bremen![]() |
Nice Street![]() |
4 ![]() |
Poland![]() |
Warsaw![]() |
9 ![]() |
ET67 ![]() |
||
456 ![]() |
France![]() |
Corsica![]() |
Corsica![]() |
Amz Street![]() |
3 ![]() |
Italy![]() |
Milan![]() |
8 ![]() |
AEC784 ![]() |
||
456 ![]() |
UK![]() |
UK![]() |
London![]() |
G Street ![]() |
2 ![]() |
Portugal![]() |
Lisbon![]() |
1 ![]() |
LP400 ![]() |
The desired outcome is:期望的结果是:
MID![]() |
FromCountry![]() |
FromState![]() |
FromCity![]() |
FromAddress![]() |
FromNumber![]() |
FromApartment![]() |
ToCountry![]() |
ToCity![]() |
ToAddress![]() |
ToNumber![]() |
ToApartment![]() |
FromCountry1![]() |
FromState1![]() |
FromCity1![]() |
FromAddress1![]() |
FromNumber1 ![]() |
FromApartment1 ![]() |
ToCountry1![]() |
ToCity1![]() |
ToAddress1![]() |
ToNumber1 ![]() |
ToApartment1![]() |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
123 ![]() |
USA![]() |
Texas![]() |
Houston![]() |
Well Street![]() |
1 ![]() |
Japan![]() |
Tokyo![]() |
6 ![]() |
ET3 ![]() |
Germany![]() |
Bremen![]() |
Bremen![]() |
Nice Street![]() |
4 ![]() |
Poland![]() |
Warsaw![]() |
9 ![]() |
ET67 ![]() |
||||
456 ![]() |
France![]() |
Corsica![]() |
Corsica![]() |
Amz Street![]() |
3 ![]() |
Italy![]() |
Milan![]() |
8 ![]() |
AEC784 ![]() |
UK![]() |
UK![]() |
London![]() |
G Street ![]() |
2 ![]() |
Portugal![]() |
Lisbon![]() |
1 ![]() |
LP400 ![]() |
What I am trying to achieve is to bring multiple rows in 1 table, which have the same MID, under 1 row, regardless if there are columns with empty values.我想要实现的是将具有相同 MID 的 1 个表中的多行置于 1 行之下,无论是否存在具有空值的列。
I think that i over complicated the solution to this as I was trying something like this (and of course the outcome is not the desired one):我认为我在尝试这样的事情时过于复杂了解决方案(当然结果不是预期的):
select [MID],
STUFF(
(select concat('', [FromCountry])
FROM test i
where i.[MID] = o.[MID]
for xml path ('')),1,1,'') as FromCountry
,stuff (
(select concat('', [FromState])
FROM test i
where i.[MID] = o.[MID]
for xml path ('')),1,1,'') as FromState
,stuff (
(select concat('', [FromCity])
FROM test i
where i.[MID] = o.[MID]
for xml path ('')),1,1,'') as FromCity
,stuff (
(select concat('', [FromAddress])
FROM test i
where i.[MID] = o.[MID]
for xml path ('')),1,1,'') as FromAddress
FROM test o
group by [MID]
...
Is there any way to achieve this?有什么办法可以做到这一点?
On the assumption there are no more than 2 rows per MID
then you can implement a simple row_number() solution.假设每个
MID
不超过 2 行,那么您可以实现一个简单的 row_number() 解决方案。
You need to join one row for each MID to the other, so assign a unique value to each using row_number - there's nothing I can immediately see that indicates which row should be the "second" row - this is assigning row numbers based on the FromCountry - amend as necessary.您需要将每个 MID 的一行连接到另一行,因此使用 row_number 为每个行分配一个唯一值 - 我无法立即看到表明哪一行应该是“第二”行 - 这是根据 FromCountry 分配行号- 根据需要进行修改。
I'm not reproducing all the columns here but you get the idea, rinse and repeat for each column.我没有复制这里的所有列,但你明白了,冲洗并重复每一列。
with m as (
select *, Row_Number() over(partition by Mid order by FromCountry) seq
from t
)
select m.Mid,
m.fromcountry, m.fromstate,
m2.fromcountry FromCountry1, m2.fromstate FromState1
from m
join m m2 on m.mid = m2.mid and m2.seq = 2
where m.seq = 1;
See example fiddle参见示例小提琴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.