Using DISTINCT for specific columns

前端 未结 3 1570
小鲜肉
小鲜肉 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 16:08

    You have to remove two columns before distinct

    select distinct commission_pct, department_id from
    employees; 
    

    Indeed, if your second query would work, what do you expect to see in the first two columns? Consider example data

    | employee_id | first_name | commission_pct | department_id |
    |     1       |     "x"    |      "b"       |       3       |
    |     2       |     "y"    |      "b"       |       3       |
    |     1       |     "x"    |      "c"       |       4       |
    |     2       |     "y"    |      "c"       |       4       |
    

    You expect to get only two row result like this

    | employee_id | first_name | commission_pct | department_id |
    |     ?       |      ?     |      "b"       |       3       |
    |     ?       |      ?     |      "c"       |       4       |
    

    But what do you expect in the first two column?

提交回复
热议问题