sql - MariaDB - GROUP BY with an order -
so have dataset, order based on strings order field(field_name, ...)
after order wan't group dataset based on column.
i have tried subquery, seems ignores order by
when gets subqueried.
this query group group setting_id
select * `setting_values` ((`owned_by_type` = 'app\\models\\utecca\\user' , `owned_by_id` = 1 or ((`owned_by_type` = 'app\\models\\utecca\\agreement' , `owned_by_id` = 1006))) or (`owned_by_type` = 'app\\models\\utecca\\employee' , `owned_by_id` = 1)) , `setting_values`.`deleted_at` null order field(owned_by_type, 'app\\models\\utecca\\employee', 'app\\models\\utecca\\agreement', 'app\\models\\utecca\\user')
the order works fine, cannot group based on order, selects 1 lowest primary key (id).
here attempt did not work.
select * ( select * `setting_values` ((`owned_by_type` = 'app\\models\\utecca\\user' , `owned_by_id` = 1 or ((`owned_by_type` = 'app\\models\\utecca\\agreement' , `owned_by_id` = 1006))) or (`owned_by_type` = 'app\\models\\utecca\\employee' , `owned_by_id` = 1)) , `setting_values`.`deleted_at` null order field(owned_by_type, 'app\\models\\utecca\\employee', 'app\\models\\utecca\\agreement', 'app\\models\\utecca\\user') ) t group setting_id;
what trying accomplish sample data 1 row id 3 row.
desired result set query should obey these rules
- 1 row each
setting_id
owned_by_type
owned_by_id
filtered following wayagreement = 1006
,user = 1
,employee = 1
.- when limiting 1 row each
setting_id
it should done following priority inowned_by_type
columnemployee, agreement, user
here sqlfiddle it.
running mariadb version 10.2.6-mariadb
first of all, optimizer free ignore inner order by. so, please describe further intent is.
getting past that, can use subquery:
select ... ( select ... group ... order ... -- lost unless followed : limit 9999999999 -- valid; or high (for all) ) x group ...
perhaps doing groupwise max ??
Comments
Post a Comment