避免在一组记录中重复值

时间:2012-12-26 20:19:46

标签: sql oracle etl

我确信之前已经回答过,但问题是我不知道如何寻找解决方案。

我有一个Oracle查询,它给我这个结果:

ETA     Vessel   Line   POD   SZTP QTY
====    ======   ====   ===   ==== ===
26/12   MAEWIS   MAE    LIV   40RH  23
26/12   MAEWIS   MAE    PBL   40RH  12
26/12   APLMEN   APL    PTR   20DR  44
26/12   APLMEN   APL    TRU   20DR  22
27/12   APLMEN   APL    ECS   40RH   7
27/12   RICKEM   HPL    RTT   40RH  18

我需要的是:

ETA     Vessel   Line   POD   SZTP QTY
====    ======   ====   ===   ==== ===
26/12   MAEWIS   MAE    LIV   40RH  23
                        PBL   40RH  12
        APLMEN   APL    PTR   20DR  44
                        TRU   20DR  22
27/12   APLMEN   APL    ECS   40RH   7
        RICKEM   HPL    RTT   40RH  18

如果在ETA / VSL / LINE中有很多这样的话,可能也会为POD和SZTP做这件事

有没有办法做到这一点?

这是我的疑问:

select to_char(vv.eta, 'DY-DD/MM') eta,
  a.linevv_vv_vsl_id||'/'||vv.out_voy_nbr vessel,
  a.linevv_line_id line,
  a.discharge_port_id1 pod,
    b.sztp_id sztp,
    b.qty qty
from
  service_orders a,
  service_order_items b,
  vessel_visits vv 
where
   b.so_gkey = a.gkey and
   vv.vsl_id = a.linevv_vv_vsl_id and
   vv.out_voy_nbr = a.linevv_vv_out_voy_nbr and
   sub_type = 'VEPO' and
   ((vv.eta between sysdate and sysdate + 7) or (to_char(vv.ata, 'YYYY-MM-DD') =   to_char(sysdate, 'YYYY-MM-DD')))
order by to_char(vv.eta, 'YYYY-MM-DD')

我正在使用Pentaho ETL Tool Kettle来运行查询,并将数据流转换为XML。所以,如果解决方案在ETL上,我也可以在那里使用一些帮助来解决这个问题。

非常感谢你的帮助。

3 个答案:

答案 0 :(得分:6)

我更喜欢用所有键正确定义所有行。但是,您可以这样做,您只需要识别每个分组的第一行。

with t as (<your query here with `to_char(eta, 'YYYY-MM-DD') as eta_yyyymmdd` added>)
select (case when seqnum_eta = 1 then eta else '' end) as eta,
       (case when seqnum_vessel = 1 then vessel else '' end) as vessel,
       (case when seqnum_line = 1 then line else '' end) as line,
       pod, sztyp, qty
from (select t.*, 
             row_number() over (partition by eta order by vessel, line, pod, sztp, qty) as seqnum_eta,
             row_number() over (partition by eta, vessel order by line, pod, sztp, qty) as seqnum_vessel,
             row_number() over (partition by eta, vessel, line order by pod, sztp, qty) as seqnum_line
      from t
     ) t
order by eta_yyyymmdd, t.vessel, t.line, t.pod, t.sztp, t.qty

答案 1 :(得分:1)

记录是向后的,例如:

                                    40DR         5
                                    40OT        12
                                FOS 20OT         1
                                GOA 40DR         5
                                LVN 20DR       100
                                LVN 20OT         3
                                MOI 40RH        22
                                VLC 20DR        30
                            MFR ALG 20FR         1
WED-26/12   CATSCHU/934N    CMD GOA 40DR        70
THU-27/12   CAPMORE/027S    CHI GYE 40RH         4
                                    20DR        50
                                    40RH        50
                                    40RH        50
FRI-28/12   URSULRI/066S    MAE PCR 20DR        50

这是最终查询:

with t as (select to_char(vv.eta, 'DY-DD/MM') eta,
  to_char(vv.eta, 'YYYY-MM-DD') as eta_yyyymmdd,
  a.linevv_vv_vsl_id||'/'||vv.out_voy_nbr vessel,
  a.linevv_line_id line,
  a.discharge_port_id1 pod,
    b.sztp_id sztp,
    to_char(b.qty, '9G999') containers,
    case when
      a.notes is null then '   '
      else a.notes end notes
from
  service_orders a,
  service_order_items b,
  vessel_visits vv 
where
   b.so_gkey = a.gkey and
   vv.vsl_id = a.linevv_vv_vsl_id and
   vv.out_voy_nbr = a.linevv_vv_out_voy_nbr and
   sub_type = 'VEPO' and
   ((vv.eta between sysdate and sysdate + 7) or (to_char(vv.ata, 'YYYY-MM-DD') = to_char(sysdate, 'YYYY-MM-DD')))
order by to_char(vv.eta, 'YYYY-MM-DD'))
select (case when seqnum_eta = 1 then eta else '   ' end) as eta,
       (case when seqnum_vessel = 1 then vessel else '   ' end) as vessel,
       (case when seqnum_line = 1 then line else '   ' end) as line,
       (case when seqnum_pod = 1 then pod else '   ' end) as pod,       
       sztp,
       containers,
       notes
from (select t.*, 
             row_number() over (partition by eta order by vessel, line, pod, sztp, containers, notes) as seqnum_eta,
             row_number() over (partition by eta, vessel order by line, pod, sztp, containers, notes) as seqnum_vessel,
             row_number() over (partition by eta, vessel, line order by pod, sztp, containers, notes) as seqnum_line,
             row_number() over (partition by eta, vessel, line, pod order by sztp, containers, notes) as seqnum_pod
      from t
     ) t
order by eta_yyyymmdd , vessel, line, pod, sztp, containers, notes

答案 2 :(得分:0)

使用SQL Plus break可能是最简单,最有效的,而不是编写复杂的查询:

break on deptno
Select deptno, empno, ename
 From scott.emp
Order by 1