当{someCase}然后是json类型列时,Postgresql按大小写顺序

当{someCase}然后是json类型列时,Postgresql按大小写顺序,postgresql,Postgresql,我需要通过几种方式选择订单结果。 当它是表TenderItem中的某个列时,它就工作了。 但如果它是json类型列TenderItem.ItemInfo中的某个键,则不起作用 按项目信息->>“名称”;-在简单选择中工作 **在这个字符串上,我有一个消息错误:大小写类型bigint和text不能匹配ItemId是bigint,而i.ItemInfo->“ABCSegment”是文本,它们是不兼容的类型,不能进行排序 尝试将该值显式转换为BIGINT,即 或者,如果由于无效的bigint值而导致上

我需要通过几种方式选择订单结果。 当它是表TenderItem中的某个列时,它就工作了。 但如果它是json类型列TenderItem.ItemInfo中的某个键,则不起作用

按项目信息->>“名称”;-在简单选择中工作

**在这个字符串上,我有一个消息错误:大小写类型bigint和text不能匹配

ItemId是bigint,而i.ItemInfo->“ABCSegment”是文本,它们是不兼容的类型,不能进行排序

尝试将该值显式转换为BIGINT,即

或者,如果由于无效的bigint值而导致上述操作失败,则将i.ItemId设置为文本

ItemId是BIGINT,i.ItemInfo->>“ABCSegment”是文本,它们是不兼容的排序类型

尝试将该值显式转换为BIGINT,即

或者,如果由于无效的bigint值而导致上述操作失败,则将i.ItemId设置为文本


不清楚如何根据ItemInfo段对itemID进行排序,除非这指向一个itemID,因为它们不都是文本值,如果它们都是文本,但有些是文本字符串,如“12345”,则不希望使用文本排序,因为“100”将出现在“99”之前。您可能希望它们是单独的排序条件,以便在排序时提供更大的灵活性:

with sortingParams (columnName, isAsc) AS (VALUES ('ItemId', true))
select *
FROM "TenderItem" i, sortingParams
WHERE i."TenderId" = 1
AND  i."ItemInfo" ->> 'Name' like '%Transcend%'
ORDER BY
case
WHEN columnName like '%ItemId%' THEN i."ItemId"::bigint end asc nulls last --puts things with an itemID ahead of those without, or could use nulls first
--if two items have same item id, then sort by segment
, case  
WHEN columnName like '%ABCSegment%' THEN i."ItemInfo" ->> 'ABCSegment' 
end desc;

请注意,每个排序条件必须为要计算的每一行提供相同的数据类型!这就是您描述的错误的原因,其中case语句为ItemId提供了一个biting,为ItemInfo提供了一个文本值-->>“ABCSegment”

不清楚如何根据ItemInfo段对ItemId进行排序,除非这指向一个ItemId,因为它们不是所有的文本值,如果它们都是文本,但有些是文本字符串,如“12345”然后,您不想使用文本排序,因为此时“100”将出现在“99”之前。您可能希望它们是单独的排序条件,以便在排序时提供更大的灵活性:

with sortingParams (columnName, isAsc) AS (VALUES ('ItemId', true))
select *
FROM "TenderItem" i, sortingParams
WHERE i."TenderId" = 1
AND  i."ItemInfo" ->> 'Name' like '%Transcend%'
ORDER BY
case
WHEN columnName like '%ItemId%' THEN i."ItemId"::bigint end asc nulls last --puts things with an itemID ahead of those without, or could use nulls first
--if two items have same item id, then sort by segment
, case  
WHEN columnName like '%ABCSegment%' THEN i."ItemInfo" ->> 'ABCSegment' 
end desc;

请注意,每个排序条件必须为要计算的每一行提供相同的数据类型!这就是您描述的错误的原因,其中case语句为ItemId提供了一个biting,为ItemInfo提供了一个文本值-->>“ABCSegment”

@belousovdmitry:不客气。如果你认为答案帮助了你,请考虑接受它,这样它也会帮助别人。请阅读:如果你认为答案帮助了你,请考虑接受它,这样它也会帮助别人。请阅读:
i."ItemId"::TEXT
with sortingParams (columnName, isAsc) AS (VALUES ('ItemId', true))
select *
FROM "TenderItem" i, sortingParams
WHERE i."TenderId" = 1
AND  i."ItemInfo" ->> 'Name' like '%Transcend%'
ORDER BY
case
WHEN columnName like '%ItemId%' THEN i."ItemId"::bigint end asc nulls last --puts things with an itemID ahead of those without, or could use nulls first
--if two items have same item id, then sort by segment
, case  
WHEN columnName like '%ABCSegment%' THEN i."ItemInfo" ->> 'ABCSegment' 
end desc;