按字母顺序排列

时间:2018-10-30 18:02:31

标签: sql ruby-on-rails postgresql

我正在尝试基于我在内存中的i18n键订购一个查询。 所以我建立了这个查询:

SELECT  "workflow_tasks".* FROM "workflow_tasks"
ORDER BY CASE
WHEN type='Workflow::Tasks::EmailCloseBrothersAboutPersonalDetailsChange' THEN 'Email Close Brothers'
WHEN type='Workflow::Tasks::CaptureFinanceProposal' THEN 'Record Finance Status'
WHEN type='Workflow::Tasks::SubmitCreditCheck' THEN 'Submit Credit Check'
WHEN type='Workflow::Tasks::UpdateDriverBankDetail' THEN 'Update Driver''s Bank Details'
WHEN type='Workflow::Tasks::UpdateDriverPersonalDetail' THEN 'Update Driver''s Personal Details'
WHEN type='Workflow::Tasks::ValidateInsuranceCertificate' THEN 'Validate Driver''s Insurance Certificate'
END

我希望它能根据THEN

之后的字符串为我提供按字母顺序排列的任务列表

除了ValidateInsuranceCertificate位于UpdateDriverPersonalDetails之前,顺序几乎是正确的。我一定误解了CASEORDER BY的用法: enter image description here

2 个答案:

答案 0 :(得分:1)

我猜你以某种方式没有完全匹配。我将从以下内容开始:

SELECT wf.*,
       (CASE WHEN type = 'Workflow::Tasks::EmailCloseBrothersAboutPersonalDetailsChange' THEN 'Email Close Brothers'
             WHEN type = 'Workflow::Tasks::CaptureFinanceProposal' THEN 'Record Finance Status'
             WHEN type = 'Workflow::Tasks::SubmitCreditCheck' THEN 'Submit Credit Check'
             WHEN type = 'Workflow::Tasks::UpdateDriverBankDetail' THEN 'Update Driver''s Bank Details'
             WHEN type = 'Workflow::Tasks::UpdateDriverPersonalDetail' THEN 'Update Driver''s Personal Details'
             WHEN type = 'Workflow::Tasks::ValidateInsuranceCertificate' THEN 'Validate Driver''s Insurance Certificate'
         END) as sortkey
FROM "workflow_tasks" wf
ORDER BY sortkey;

答案 1 :(得分:0)

您的查询中有一个描述type的错字。

您有:

WHEN type='Workflow::Tasks::UpdateDriverBankDetail'

但是数据库屏幕快照中的类型为复数。所以你应该有:

WHEN type='Workflow::Tasks::UpdateDriverBankDetails'

可能会有更多的错别字,但屏幕截图会截断数据。