PostgresSql:比较两个表并获得其结果,然后将其与第三个表进行比较

PostgresSql:比较两个表并获得其结果,然后将其与第三个表进行比较,sql,postgresql,Sql,Postgresql,表2:行程、交付、销售线 +-------+---------------------+------------+----------+------------+-------------+--------+--+ | Sl no | Order_date | Partner_id | Route_id | Product_id | Product qty | amount | | +-------+---------------------+------------+---

表2:行程、交付、销售线

+-------+---------------------+------------+----------+------------+-------------+--------+--+
| Sl no |     Order_date      | Partner_id | Route_id | Product_id | Product qty | amount |  |
+-------+---------------------+------------+----------+------------+-------------+--------+--+
|     1 | 2020-08-01 04:25:35 |      34567 |      152 |        432 |           2 |    100 |  |
|     2 | 2021-09-11 02:25:35 |      34572 |      130 |        312 |           4 |    150 |  |
|     3 | 2020-05-10 04:25:35 |      34567 |      152 |        432 |           3 |    123 |  |
|     4 | 2021-02-16 01:10:35 |      34572 |      130 |        432 |           5 |    123 |  |
|     5 | 2020-02-19 01:10:35 |      34567 |      152 |        432 |           2 |    600 |  |
|     6 | 2021-03-20 01:10:35 |      34569 |      152 |        123 |           1 |    123 |  |
|     7 | 2021-04-23 01:10:35 |      34570 |      152 |        432 |           4 |    200 |  |
|     8 | 2021-07-08 01:10:35 |      34567 |      152 |        432 |           3 |     32 |  |
|     9 | 2019-06-28 01:10:35 |      34570 |      152 |        432 |           2 |    100 |  |
|    10 | 2018-11-14 01:10:35 |      34570 |      152 |        432 |           5 |     20 |  |
|       |                     |            |          |            |             |        |  |
+-------+---------------------+------------+----------+------------+-------------+--------+--+
从表2中:我们必须找到route=152中的合作伙伴,并找到最近2次销售的产品数量之和[可通过描述订单日期选择]

。我们可以在表3中找到结果

34567–序列号[1,8] 34570–序列号[7,9] 34569–序列号[6] 表3:根据表1,2得出的结果

+------------+-------+
| Partner_id | count |
+------------+-------+
|      34567 |     5 |
|      34569 |     1 |
|      34570 |     6 |
|            |       |
+------------+-------+
从表4中,我们想找到上面的partner_id叶数

表4:优惠券页

+------------+-------+
| Partner_id | Leaf  |
+------------+-------+
|      34567 | XYZ1  |
|      34569 | XYZ2  |
|      34569 | DDHC  |
|      34567 | DVDV  |
|      34570 | DVFDV |
|      34576 | FVFV  |
|      34567 | FVV   |
|            |       |
+------------+-------+

从中我们可以发现结果如下:

34567 – 3 34569-2 34570 -1 表5:根据表4得出的结果

+------------+-------+
| Partner_id | count |
+------------+-------+
|      34567 |     3 |
|      34569 |     2 |
|      34570 |     1 |
|            |       |
+------------+-------+
现在我们要比较表3和表5

If partner_id count [table 3] > partner_id count [table 4]
        Print partner_id
我需要一个查询来完成所有这些操作

不同的合作伙伴id可通过以下方式找到:从表1

选择不同的合作伙伴id 来自trip\u delivery\u sales ts 其中ts.route_id='152' 按ts.partner\u id分组
这回答了问题的原始版本

在汇总表2和表3之后,您似乎想要比较总计。我不知道表1是做什么用的。它似乎没有任何作用

因此:


样本数据最好以如下方式表示。有关如何创建美观的表格的一些提示,请参见。编辑过的表格数据您能解释一下您想做什么吗?这个步骤非常详细,但它失去了整体意图。那么你的例子就没有意义了。我看到34567在表2中有四行,不仅仅是1和8,而不是5。非常混乱。你的标题也表明有3个表格,但问题有5个。更令人困惑的是。34567在表2中有四行,而不仅仅是1行和8行,而不是5行……因为我们只想从表1中选择最后2个销售产品数量计数,我们将获得partner_id Allen to route=152……使用该partner_id我们想查询表2@Shahal . . . 表2中没有路由id。但是,您应该能够调整这个逻辑,它回答了您在这里提出的问题。route_id colum在表2中…我已经更改了上面的代码。。。。。只想比较旅行、送货、销售和优惠券table@Shahal . . . 修改问题使答案无效是不友好的。
select *
from (select partner_id, sum(quantity) as sum_quantity
      from (select tdsl.*,
                   row_number() over (partition by t2.partner_id order by order_date) as seqnum
            from trip_delivery_sales_lines tdsl
           ) tdsl
      where seqnum <= 2
      group by tdsl.partner_id
     ) tdsl left join
     (select cl.partner_id, count(*) as leaf_cnt
      from coupon_leaf cl
      group by cl.partner_id
     ) cl
     on cl.partner_id = tdsl.partner_id
where leaf_cnt is null or sum_quantity > leaf_cnt