2013-04-02 95 views
1

我有以下SQL查詢:一個SQL查詢與NULL的行替換的值,如果這個值等於從以前行的值

選擇供應商的名稱,日期,營業額 從成交 秩序通過供應商

supplier  name  date  turnover 
    --------  --------- ---------- ---------- 
     001  supplier1 01.01.2012  129,67 
     001  supplier1 02.05.2012 12.145,89 
     001  supplier1 03.07.2012 6.830,90 
     002  supplier2 01.01.2012  -11,55 
     002  supplier2 03.09.2012 4.500,61 

是否有可能用NULL代替重複的值,所以結果表看起來像這樣(見下文)?

supplier  name  date  turnover 
    --------  --------- ---------- ---------- 
     001  supplier1 01.01.2012  129,67 
          02.05.2012 12.145,89 
          03.07.2012 6.830,90 
     002  supplier2 01.01.2012  -11,55 
          03.09.2012 4.500,61 

任何意見將不勝感激!

回答

0

您可以使用分析函數LAGNULLIF函數。喜歡的東西

SQL> ed 
Wrote file afiedt.buf 

    1 select nullif(deptno, 
    2    lag(deptno) over (order by deptno, ename)) formatted_deptno, 
    3   ename 
    4 from emp 
    5* order by deptno, ename 
SQL>/

FORMATTED_DEPTNO ENAME 
---------------- ---------- 
       10 CLARK 
       KING 
       MILLER 
       20 ADAMS 
       FORD 
       JONES 
       SCOTT 
       SMITH 
       30 ALLEN 
       BLAKE 
       JAMES 
       MARTIN 
       TURNER 
       WARD 

14 rows selected. 
+0

謝謝你,賈斯汀 – Irina

1

你可以使用row_number

select case row when 1 then supplier_orig else null end as supplier, 
    case row when 1 then name_orig else null end as name, 
    date, turnover 
from (
    select supplier AS supplier_orig, name AS name_orig, date, turnover, 
    row_number() over (partition by supplier order by date) as row 
    from turnover 
) 
order by supplier_orig, date 

SQL Fiddle here

+0

非常感謝,傑夫 – Irina

相關問題