小编典典

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

sql

表 2:trip_delivery_sales_lines

+-------+---------------------+------------+----------+------------+-------------+--------+--+
| 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 个销售的 product_qty 总和 [可以通过 desc order_date 选择]

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

34567 – Serial number [ 1,8] 
34570 – Serial number [ 7,9] 
34569 – Serial number [6] 

表 3:从表 1,2 中获得的结果

+------------+-------+
| Partner_id | count |
+------------+-------+
|      34567 |     5 |
|      34569 |     1 |
|      34570 |     6 |
|            |       |
+------------+-------+

从表 4 中我们要找到上面的 partner_ids 叶数

表 4 :coupon_leaf

+------------+-------+
| 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

我想要一个查询来完成所有这些操作

可以通过以下方式找到不同的partner_id:从表1

SELECT  DISTINCT partner_id 
FROM trip_delivery_sales ts 
WHERE ts.route_id='152' 
GROUP BY ts.partner_id

阅读 188

收藏
2021-05-30

共1个答案

小编典典

这回答了问题的原始版本。

您似乎想在汇总表2和表3之后比较总计。我不知道这table1是为了什么。它似乎没有任何作用。

所以:

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
2021-05-30