如何只汇总表中的唯一值

时间:2019-06-07 08:56:14

标签: sql

我有一个表shipping_manifest,其中有contain client_idorder_idpallet_idpallet_volume

在第二张表Order_header中,我有client_idorder_idorder_volume。我需要计算pallet_volume + the order_volume。但是,shipping_manifest表中可以有一个订单的多个托盘,或者shipping_manifest表中可以有多个托盘。 2个示例订单

CLIENT_ID ORDER_ID   PALLET_ID          PALLET_VOLUME ORDER_VOLUME
A         TESTJBDN11 373943762002480563 0.12          18
A         TESTJBDN11 373943762002480549 0.12          18
A         TESTJBDN11 H876               0.12          18
A         TESTJBDN4  EXT-5422081        0.1           0.15
A         TESTJBDN4  EXT-5422081        0.1           0.15
A         TESTJBDN4  EXT-5422081        0.1           0.15

对于每个唯一的total_pallet_volumeorder_value +应该是pallet_volume pallet_id

因此对于订单TESTJBDN11,应为18 + (3 * 0.12) = 18.36 对于订单TESTJBDN4,应为0.15 + 0.1 = 0.25

我尝试过尝试做过:

示例A

SELECT sm.client_id,sm.order_id,sm.pallet_id, sm.pallet_volume,oh.order_volume,SUM(sm.pallet_volume)+oh.order_volume as "total_pallet_volume"
    FROM Shipping_Manifest sm
         LEFT JOIN order_header oh ON oh.order_id = sm.order_id AND oh.client_id = sm.client_id
    WHERE sm.client_id = 'A' AND
        sm.ORDER_ID = 'TESTJBDN4'
        GROUP BY sm.client_id, sm.order_id,sm.pallet_id, sm.pallet_volume, oh.order_volume;

示例B:

select sm.client_id,sm.order_id, sm.pallet_volume,oh.order_volume,
(select sum(sm.pallet_volume )+( oh.order_volume) 
    from Shipping_Manifest sm
         left join order_header oh on oh.order_id = sm.order_id and oh.client_id = sm.client_id
    WHERE sm.client_id = 'A' and
        sm.ORDER_ID = 'TESTJBDN11'
        group by sm.pallet_volume,oh.order_volume) AS "total_pallet_volume"
        from Shipping_Manifest sm
        left join order_header oh on oh.order_id = sm.order_id and oh.client_id = sm.client_id
WHERE sm.client_id = 'A' and
sm.ORDER_ID = 'TESTJBDN11';

示例C:

SELECT sm.client_id,sm.order_id,sm.pallet_id, sm.pallet_volume,oh.order_volume,sm.pallet_volume +oh.order_volume as "total_pallet_volume"
    FROM Shipping_Manifest sm
         LEFT JOIN order_header oh ON oh.order_id = sm.order_id AND oh.client_id = sm.client_id
    WHERE sm.client_id = 'A' AND
        sm.ORDER_ID IN ('TESTJBDN11','TESTJBDN4')
        GROUP BY sm.client_id, sm.order_id, sm.pallet_id, sm.pallet_volume, oh.order_volume, 
sm.pallet_volume +oh.order_volume;

因此对于订单TESTJBDN11,应为18 + (3 * 0.12) = 18.36

使用选择示例A-实际输出为18.12

使用选择示例B-实际输出为18.36(这是正确的,但不适用于其他情况)

使用选择示例C-实际输出为18.12

对于订单TESTJBDN4,应为0.15 + 0.1 = 0.25

使用选择示例A-实际输出为0.45

使用选择示例B-实际输出为0.45

使用选择示例C-实际输出为0.25(这是正确的,但不适用于其他情况)

由于表中有多个订单,因此我需要一个对两个订单都适用的语句。

3 个答案:

答案 0 :(得分:0)

您也可以尝试以下方法:

new
CLIENT_ID | ORDER_ID   | PALLET_ID          | PALLET_VOLUME | ORDER_VOLUME
:-------- | :--------- | :----------------- | ------------: | -----------:
A         | TESTJBDN11 | 373943762002480563 |           .12 |           18
A         | TESTJBDN11 | 373943762002480549 |           .12 |           18
A         | TESTJBDN11 | H876               |           .12 |           18
A         | TESTJBDN4  | EXT-5422081        |            .1 |          .15
A         | TESTJBDN4  | EXT-5422081        |            .1 |          .15
A         | TESTJBDN4  | EXT-5422081        |            .1 |          .15
old
CLIENT_ID | ORDER_ID   | PALLET_ID          | PALLET_VOLUME | ORDER_VOLUME | TOTAL
:-------- | :--------- | :----------------- | ------------: | -----------: | ---------:
A         | TESTJBDN11 | 373943762002480549 |           .12 |           18 |      18.36
A         | TESTJBDN11 | 373943762002480563 |           .12 |           18 |      18.36
A         | TESTJBDN11 | H876               |           .12 |           18 |      18.36
A         | TESTJBDN4  | EXT-5422081        |            .1 |          .15 |        .25

Compiled

答案 1 :(得分:0)

所以基本上,如果您的shipping_manifest表shipping_manifest和order_header order_header看起来像这样:


您可以运行以下查询:

select distinct s.client_id, s.order_id, pallet_id, pallet_volume * s.count_unique_pallet_id + s2.order_volumne as result
from shipping_manifest o left join (
SELECT  a.client_id, a.order_id, count(distinct a.pallet_id) as count_unique_pallet_id
from shipping_manifest a
group by a.client_id, a.order_id) s
on o.client_id = s.client_id and o.order_id = s.order_id
left join (select client_id, order_id, order_volumne from order_header) s2
on o.client_id = s2.client_id and o.order_id = s2.order_id 


它给出以下结果: Result

答案 2 :(得分:0)

这是一种方法。

此方法处理两个极端情况:没有货盘但有订单量的情况(LEFT JOIN)和同一个货盘具有不同值的情况(使用MAX(pallet_volume) )。可能这两种情况都不可行,但是我一直喜欢皮带和悬挂带保护。

设置:

create table shipping_manifest(
  manifest_id integer,
  client_id varchar2(20),
  order_id varchar2(20),
  pallet_id varchar2(20),
  pallet_volume number
);
create table order_header(
  client_id varchar2(20),
  order_id varchar2(20),
  order_volume number
);
insert into order_header values('A','TESTJBDN11', 18);
insert into order_header values('A','TESTJBDN4', 0.15);
insert into shipping_manifest values (1,'A','TESTJBDN11','373943762002480563', 0.12);
insert into shipping_manifest values (2,'A','TESTJBDN11','373943762002480549', 0.12);
insert into shipping_manifest values (3,'A','TESTJBDN11','H876', 0.12);
insert into shipping_manifest values (4,'A','TESTJBDN4','EXT-5422081', 0.1);
insert into shipping_manifest values (5,'A','TESTJBDN4','EXT-5422081', 0.1);
insert into shipping_manifest values (6,'A','TESTJBDN4','EXT-5422081', 0.1);
COMMIT;

查询:

with pallets as(
  select client_id, order_id, pallet_id, max(pallet_volume) as max_pallet_volume
    from shipping_manifest sm
    group by client_id, order_id, pallet_id
)
select oh.client_id, oh.order_id, oh.order_volume + sum(p.max_pallet_volume) volume
from order_header oh
left join pallets p on p.client_id = oh.client_id and p.order_id = oh.order_id
group by oh.client_id, oh.order_id, order_volume;