SQL - 删除重复的元组,即使值不正常

时间:2016-03-22 00:52:33

标签: sql oracle

假设我有一张这样的表:

package demo.mapas;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
//import android.support.v4.app.FragmentManager;
import android.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.usuario.mapas.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

/**
 * Created by USUARIO on 3/12/2016.
 */
public class opcion1Fragment extends Fragment  {
    private SupportMapFragment mapFragment;




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_option1, container, false);

        return rootView;

    }

    @Override

    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        FragmentManager fm = getChildFragmentManager();
        mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
        if(mapFragment == null){
            mapFragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map, mapFragment).commit();
        } else {
            mapFragment.getMapAsync(new OnMapReadyCallback() {

                @Override
                public void onMapReady(GoogleMap googleMap) {
                    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

                }
            });
        }
    }

}

我想摆脱重复,所以我留下了这个:

name_1 name_2 value
-------------------
john   alex    6
alex   john    6
bob    rick    7
rick   bob     7

name_1 name_2 value ------------------- john alex 6 rick bob 7 是否有效?如果是这样,我将如何应用它?

修改

我并不关心决赛桌中名字的顺序。我正在寻找名字对。所以我将distinct视为与john alex相同。因此,我想摆脱那些“重复”

3 个答案:

答案 0 :(得分:5)

以下是使用least greatestdistinct的一个选项:

select distinct least(name_1, name_2) name_1,
       greatest(name_1, name_2) name_2,
       value
from yourtable

答案 1 :(得分:2)

SQL Fiddle

Oracle 11g R2架构设置

create table table_name (name1, name2, value) AS
SELECT 'john', 'alex', 6 FROM DUAL UNION ALL
SELECT 'alex', 'john', 6 FROM DUAL UNION ALL
SELECT 'bob',  'rick', 7 FROM DUAL UNION ALL
SELECT 'rick', 'bob',  7 FROM DUAL UNION ALL
SELECT 'alice','carol',7 FROM DUAL UNION ALL
SELECT 'carol','alice',7 FROM DUAL UNION ALL
SELECT 'david','david',5 FROM DUAL;

查询1

SELECT name1,
       name2,
       value
FROM   (
  SELECT t.*,
         ROW_NUMBER()
           OVER ( PARTITION BY LEAST( NAME1, NAME2 ),
                               GREATEST( NAME1, NAME2 ),
                               VALUE
                  ORDER BY ROWNUM ) AS RN
  FROM   table_name t
)
WHERE  RN = 1

<强> Results

| NAME1 | NAME2 | VALUE |
|-------|-------|-------|
|  john |  alex |     6 |
| alice | carol |     7 |
|   bob |  rick |     7 |
| david | david |     5 |

删除重复项

DELETE FROM table_name
WHERE ROWID IN (
  SELECT rid
  FROM   (
    SELECT ROWID AS rid,
           ROW_NUMBER()
             OVER ( PARTITION BY LEAST( name1, name2 ),
                                 GREATEST( name1, name2 ),
                                 VALUE
                    ORDER BY ROWNUM ) AS rn
    FROM   table_name
  )
  WHERE rn > 1
);

查询1

SELECT * FROM table_name

<强> Results

| NAME1 | NAME2 | VALUE |
|-------|-------|-------|
|  john |  alex |     6 |
|   bob |  rick |     7 |
| alice | carol |     7 |
| david | david |     5 |

答案 2 :(得分:0)

使用WITH子句和ROW_NUMBER()函数

的另一种方法
WITH C AS(
    SELECT  ROW_NUMBER() OVER(PARTITION BY value ORDER BY Value) rank_n
            ,name_1
            ,name_2
            ,value
    FROM yourtable)
SELECT name_1, name_2, Value
FROM C
WHERE rank_n = 1

SQL Fiddle