简体   繁体   English

SQL 查询根据公共 id 组合行

[英]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 123 USA美国 Texas德克萨斯州 Houston休斯顿 Well Street井街 1 1 Japan日本 Tokyo东京 6 6 ET3 ET3
123 123 Germany德国 Bremen不来梅 Bremen不来梅 Nice Street尼斯街 4 4 Poland波兰 Warsaw华沙 9 9 ET67 ET67
456 456 France法国 Corsica科西嘉岛 Corsica科西嘉岛 Amz Street安兹街 3 3 Italy意大利 Milan米兰 8 8 AEC784 AEC784
456 456 UK英国 UK英国 London伦敦 G Street G街 2 2 Portugal葡萄牙 Lisbon里斯本 1 1 LP400 LP400

The desired outcome is:期望的结果是:

MID FromCountry来自国家 FromState从州 FromCity来自城市 FromAddress发件人地址 FromNumber发件人号码 FromApartment从公寓 ToCountry到国家 ToCity城市 ToAddress讲话 ToNumber到编号 ToApartment到公寓 FromCountry1来自Country1 FromState1从状态 1 FromCity1来自City1 FromAddress1发件人地址1 FromNumber1 FromNumber1 FromApartment1 FromApartment1 ToCountry1前往国家/地区1 ToCity1到城市1 ToAddress1到地址1 ToNumber1 ToNumber1 ToApartment1到Apartment1
123 123 USA美国 Texas德克萨斯州 Houston休斯顿 Well Street井街 1 1 Japan日本 Tokyo东京 6 6 ET3 ET3 Germany德国 Bremen不来梅 Bremen不来梅 Nice Street尼斯街 4 4 Poland波兰 Warsaw华沙 9 9 ET67 ET67
456 456 France法国 Corsica科西嘉岛 Corsica科西嘉岛 Amz Street安兹街 3 3 Italy意大利 Milan米兰 8 8 AEC784 AEC784 UK英国 UK英国 London伦敦 G Street G街 2 2 Portugal葡萄牙 Lisbon里斯本 1 1 LP400 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM