【问题标题】:How to concatenate multiple result rows of one row into one, based on FK如何将一行的多个结果行连接成一个,基于FK
【发布时间】:2021-02-03 07:15:13
【问题描述】:

我正在寻找一种将表格的结果连接到一行的方法。

我有 4 张桌子;

供应商表

+--+----------------+----------------+
|id|name            |hook_name       |
+--+----------------+----------------+
|1 |724             |724             |
|2 |Air             |air             |
|3 |Akustik         |akustik         |     
|4 |Almira          |almira          |
+--+----------------+----------------+

供应商办公室; (label 列代表接送字符串)

+---+-----------+----------+------------+
|id |supplier_id| zip_code | label      |  
+---+-----------+----------------+------+
|95 |24         |25325     |  344       | <- supplier_id 24 has office location 77,98 (label pickup)
|96 |24         |9535      |  93        | <- same. only label different (label dropoff) 
|97 |1          |2858      |  95        | 
|98 |1          |50285     |  954       | 
|99 |1          |10094     |  24        |
|100|1          |4353      |  59        |
+---+-----------+----------------+------+

办公地点(数据透视表)

+------------------+-----------+
|supplier_office_id|location_id|
+------------------+-----------+
|95                |77         | <- location I want to concatenate `supplier_id = 24` (istanbul)
|96                |98         | <- location I want to concatenate `supplier_id = 24` (london)
|97                |77         | 
|98                |77         | 
+------------------+-----------+

地点

+---------------+
|id |name       |
+---------------+
|77 |istanbul   |
|96 |berlin     |
|97 |newyork    |
|98 |london     |
+---------------+

我想找到给定位置信息的办公室。 我还没有设法创建关于标签的自定义列。 如果我想访问位置 1 和 2 的办公室信息,我想得到这样的东西;

+---------------+------------+----------------+
| supplier_id | pickup_label | dropoff_label  |
+-------------+--------------+----------------+
| 95          | 344          | 93             |
+-------------+--------------+----------------+

我现在已经能够使用我的 Postgresl SQL 做到这一点。

SELECT supplier_offices.id,                   
       supplier_offices.supplier_id
FROM "supplier_offices"
         INNER JOIN suppliers on supplier_offices.supplier_id = suppliers.id
         INNER JOIN office_locations on supplier_offices.id = office_locations.supplier_office_id
AND ("office_locations"."location_id" IN (77, 98)

【问题讨论】:

    标签: sql postgresql join


    【解决方案1】:

    如果我理解您的意思,此代码将有效。如果我理解你的意思,这段代码就可以工作。当然对于 sql,但稍作改动,我认为它也可以在 Postgresl SQL 中工作

    select supplier_offices.id as supplier_id, max(supplier_offices.label) as pickup_label, min(supplier_offices.label) as dropoff_label 
    from supplier_offices 
    inner join suppliers on supplier_offices.supplier_id = suppliers.id
    inner join office_locations on supplier_offices.id = office_locations.supplier_office_id 
    where office_locations.location_id in (77,98) 
    group by supplier_id
    

    【讨论】:

      【解决方案2】:

      我认为您需要某种聚合。完全不清楚锄头拾取位置与下落位置的区别。但是,像这样:

      SELECT so.supplier_id,
             ARRAY_AGG(location_id) FILTER (WHERE so.label in (344)) as pickup,
             ARRAY_AGG(location_id) FILTER (WHERE so.label not in (344)) as dropoff
      FROM "supplier_offices" so JOIN
           office_locations ol
           ON so.id = ol.supplier_office_id AND
              ol.location_id IN (77, 98)
      GROUP BY so.supplier_id;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-23
        • 2013-03-28
        • 1970-01-01
        • 2019-12-17
        • 2016-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多