1

Now the data and structure of source table(T1) looked like below:

NAME                    RECORD_NUMBER       APPROVERS                   AREA                        
MS COORDINATOR 1123     1                   EMEA_ARMSTROA               c                           
MS COORDINATOR 1123     3                   AMERICAS_SIMSS              c                           
MS COORDINATOR 1123     4                   ASIAPACIFIC_KUMSANGA        c                           
MS COORDINATOR 1123     5                   ASIAPACIFIC_MSAGE           c                           
MS COORDINATOR 1123     7                   ASIAPACIFIC_MSAGE           c   

then I want to copy data from the table above into a new table(T2) and remove duplicate rows and remove record_number gaps and sort approvers in alphabetic order and reset value of record_number, so the data in the target table should be looked like this:

NAME                    RECORD_NUMBER       APPROVERS                   AREA                        
MS COORDINATOR 1123     1                   AMERICAS_SIMSS              c                            
MS COORDINATOR 1123     2                   ASIAPACIFIC_KUMSANGA        c 
MS COORDINATOR 1123     3                   ASIAPACIFIC_MSAGE           c 
MS COORDINATOR 1123     4                   EMEA_ARMSTROA               c     

So, can anybody give me a one line sql solution? the primary key of T2 table is (name, record_number, area)

4

1 回答 1

2

使用ROWNUM生成record_number列。

尝试这个:

INSERT INTO t2
   SELECT name,
          ROWNUM AS record_number,
          approvers,
          area
     FROM ( SELECT DISTINCT name, approvers, area
               FROM t1
           ORDER BY approvers ) x
于 2013-11-01T03:06:27.157 回答