Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid repeating values in a group of records

I'm sure this has been answered before, but the thing is I don't know how to look for the solution.

I have a Oracle query that gives me this results:

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

And what I need is this:

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

Maybe also doing it for POD and SZTP, if there are many of those within ETA/VSL/LINE

Is there any way of doing this?

This is my query:

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')

I'm using Pentaho ETL Tool Kettle to run the query, and transform the data stream into an XML. So, if the solution is on the ETL, I could use some help on how to do it there too.

Many thanks for your help.

like image 354
Martin Ocando Avatar asked Sep 11 '25 12:09

Martin Ocando


1 Answers

I much prefer having all rows defined correctly with all the keys. However, you can do this, you just need to identify the first rows of each grouping.

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
like image 143
Gordon Linoff Avatar answered Sep 14 '25 04:09

Gordon Linoff