Using DISTINCT for specific columns

前端 未结 3 1556
小鲜肉
小鲜肉 2021-01-24 15:36
select distinct  employee_id, first_name,  commission_pct, department_id from
employees;

When I use the above query it results in distinct combination

3条回答
  •  春和景丽
    2021-01-24 15:44

    Can you try this one?

    SELECT
      NAME1,
      PH
    FROM
          (WITH T
               AS (SELECT
                        'mark' NAME1,
                        '1234567' PH
                  FROM
                        DUAL
                  UNION ALL
                  SELECT
                        'bailey',
                        '456789'
                  FROM
                        DUAL
                  UNION ALL
                  SELECT
                        'mark',
                        '987654'
                  FROM
                        DUAL)
           SELECT
                NAME1,
                PH,
                ROW_NUMBER ( ) OVER (PARTITION BY NAME1 ORDER BY NAME1) SEQ
           FROM
                T)
    WHERE
          SEQ = 1;
    

    If you dont care on a specific row, then use aggregate functions

    SELECT
          NAME1,
          MAX ( PH ) PH
    FROM
          T
    GROUP BY
          NAME1;
    

提交回复
热议问题