Skip to content

Instantly share code, notes, and snippets.

@YarikST
Created October 10, 2019 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YarikST/3bd229189842dd53e4dc3326a6038cb2 to your computer and use it in GitHub Desktop.
Save YarikST/3bd229189842dd53e4dc3326a6038cb2 to your computer and use it in GitHub Desktop.
CREATE TEMP TABLE task
as
select *
from (
VALUES
(1 :: int, 'Xліб' :: VARCHAR),
(2 :: int, 'Xліб' :: VARCHAR),
(3 :: int, 'Xліб' :: VARCHAR),
(4 :: int, 'Вино' :: VARCHAR),
(5 :: int, 'Вино' :: VARCHAR),
(6 :: int, 'Ноутбук' :: VARCHAR),
(7 :: int, 'Ноутбук' :: VARCHAR),
(8 :: int, 'Ноутбук' :: VARCHAR)
) as t (id, category_name);
select
task.*,
sort_catgory.sort_priority
from task
INNER JOIN (select x.*
from (VALUES ('Xліб', 2), ('Вино', 1)) x(category_name, sort_priority)) sort_catgory
on sort_catgory.category_name = task.category_name
order by sort_catgory.sort_priority asc
INNER JOIN or LEFT OUTER JOIN - аби фільтрувати лише
VALUES - можна динамічно генерити або якось придумати визначену структуру для запитів - тіпа - build json object
NETWORK
https://stackoverflow.com/questions/29775500/rails-order-on-columns-values-priority-column
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment