distinct displays unique columns, how to display the lines where they are?
|
2 answers
Provided that id
is unique:
SELECT id, name, action FROM table WHERE id = ANY( SELECT min(id) FROM table GROUP BY action )
- What you need =) !!! - Mikhail Kolomiets
- Effective query bad. There is no need to make a subquery (second reading from table) in this case. The task should be solved and solved easier. - pegoopik
- SELECT MIN (id) id, MIN (name) name, action FROM table GROUP BY action does not preserve integrity. SELECT id, name, action FROM (SELECT MIN (id) OVER (PARTITION BY action) min_id, * FROM table) T WHERE id = min_id - window functions, in this case, are even more inefficient. especially if there is an index by id (which, I think, is obvious). - bmsdave
- test (remote postgresql9.4 with a table of 50,000 records) with group by - 35mc, with a window function - 80mc. - bmsdave
|
SELECT MIN(id)id, MIN(name)name, action FROM table GROUP BY action
Or if it is necessary for the id to match the name, then:
SELECT id, name, action FROM( SELECT MIN(id)OVER(PARTITION BY action)min_id, * FROM table )T WHERE id = min_id
|
select * from table group by action
not suitable. However, if you donβt have mysql, then you should take aggregate functions from all those who are not participating in the group by, for example min (id) for the minimum id for this action - Mike