Using DISTINCT for specific columns

前端 未结 3 1554
小鲜肉
小鲜肉 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

    What you request is impossible. You cannot select all the employee ids but have only distinct commission_pct and department_id.

    So think it over, what you want to show:

    • All distinct commission_pct, department_id only?
    • All distinct commission_pct, department_id and the number of relevant employees?
    • All distinct commission_pct, department_id and the relevant employees comma separated?
    • All employees, but with nulls when commission_pct and department_id are the same as in the line before?

    The first can be solved with DISTINCT. The second and third with GROUP BY (plus count or listagg). The last would be solved with the analytic function LAG.

提交回复
热议问题