2012-12-26 80 views
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 

或許也這樣做了POD和SZTP,如果有很多的那些在ETA/VSL/LINE內

有沒有這樣做的方法?

這是我的查詢:

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工具水壺運行查詢和數據流轉換爲XML。所以,如果解決方案在ETL上,我可以在那裏使用一些幫助。

非常感謝您的幫助。

+4

在我看來,這應該呈現在演示級別,而不是在數據庫查詢... – PinnyM

+0

演示文稿是一個XML文件被注入到HTML中。我正在使用Pentaho Kettle ETL來創建XML。我正在考慮如何在水壺上做到這一點,但我無法弄清楚。 –

+0

然後將其重新命名爲pentaho/kettle問題。我對他們的產品並不熟悉,但肯定有一種方法可以對數據進行「分組」,然後將其輸出爲XML。 – PinnyM

回答

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

太棒了!謝謝 –

+0

你是什麼意思「倒置」?列的順序相同,第一行應該有值,後續行應該是空白的。 –

+0

我明白爲什麼。問題在於,列正在使用相同名稱的select子句中重新定義。我只是編輯它,所以'order by'使用子查詢中的值而不是計算值。 –

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

聰明。看到我的編輯和評論。 –

0

使用SQL Plus中打破可能是最簡單,最有效的,而不是寫作複雜的查詢:

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