3

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

我有一个 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 上,我也可以使用一些帮助来了解如何在 ETL 上进行操作。

非常感谢您的帮助。

4

3 回答 3

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
于 2012-12-26T20:32:22.963 回答
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
于 2012-12-26T21:21:25.243 回答
0

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

break on deptno
Select deptno, empno, ename
 From scott.emp
Order by 1
于 2012-12-27T14:35:25.483 回答