2015-12-24 3 views
0

У меня есть этот запрос, который, похоже, работает в pgAdmin. Но когда я пытаюсь перевести его на использование scuttle, я получаю массивный фрагмент запроса, содержащий всю инструкцию case. Как мне реорганизовать этот запрос, чтобы я мог использовать его в Arel?Как реорганизовать запрос, когда я сортирую продукты по его отношениям из другой таблицы

SELECT products.*, case when 
(select custom_fields.value_text 
     from custom_fields 
     where custom_fields.custom_field_definition_id = 4 
     and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') is null 
then 'stopped' 
when 
(select custom_fields.value_text 
     from custom_fields 
     where custom_fields.custom_field_definition_id = 4 
     and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') is not null 
then (select custom_fields.value_text from custom_fields where custom_fields.custom_field_definition_id = 4 
      and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') 
end as sorted 
from products 
order by sorted 

Дополнительная информация: Я создал SQLite базу данных, которая показывает ожидаемое поведение и могут быть использованы для дальнейших экспериментов.

https://github.com/bigos/rails-sorting/blob/master/README.org

Первоначальный вывод: Я нашел способ, чтобы получить ожидаемые результаты, но больше не может использовать его с активными методами Record. К счастью, я нашел, как разбивать массив хешей.

Наилучшее решение: Я могу вернуть правильные отношения активной записи, если я поместил свой код в качестве подзапроса в порядке.

SELECT products.* 
FROM products 
ORDER BY (case when 
(select custom_fields.value_text 
from custom_fields 
where custom_fields.custom_field_definition_id = 4 
and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') is null 
then 'stopped' 
when 
(select custom_fields.value_text 
from custom_fields 
where custom_fields.custom_field_definition_id = 4 
and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') is not null 
then (select custom_fields.value_text from custom_fields where custom_fields.custom_field_definition_id = 4 
and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') 
end) 
+2

Пожалуйста, предоставьте исходные данные и ожидаемый результат. – vyegorov

+0

На данный момент я использовал объединение двух запросов. Через две недели у меня будет обзор кода, и тогда мы решим, что дальше. –

+0

этот, кажется, работает ap Product.all.order (% Q | coalesce ((SELECT "custom_fields". "Value_text" FROM "custom_fields" WHERE ("custom_fields". "Table_record_id" = "products". "Id" AND " custom_fields "." value_text "IS NOT NULL))," остановлен ") |) –

ответ

2

Я не знаю точно, что проблема вы столкнулись получать эту работу с других инструментов и/или Active Record, но ниже запросы должны быть эквивалентны и, вероятно, быстрее:

select p.*, coalesce(cf.value_text, stopped) as sorted 
from 
    products p 
    left outer join custom_fields cf 
     on  cf.table_record_id = p.id 
      and cf.belongs_to_table = 'product' 
      and cf.custom_field_definition_id = 4 
order by sorted 

Этот тип запроса является одной из причин, по которым рекомендуется избегать таких таблиц «настраиваемых полей», если это возможно.

Другая альтернатива, которая очень близка к вашему первоначальному подходу, заключается в использовании скалярного подзапроса внутри coalesce. Вы можете даже предпочесть этот путь, так как это вызовет ошибку, если есть какой-то способ для подзапроса вернуть более одного значения.

select 
    p.*, 
    coalesce(
     (
     select custom_fields.value_text 
     from custom_fields cf 
     where 
       f.table_record_id = p.id 
      and cf.custom_fields.belongs_to_table = 'product' 
      and cf.custom_field_definition_id = 4 
     ), 
     'stopped' 
    ) as sorted 
from products p 
order by sorted 

 Смежные вопросы

  • Нет связанных вопросов^_^