我有这个python函数我试图在Postgresql 9.2中转换成sql。
def calc_open_order(xOrder):
temp_row = xOrder.so_reportL[0]
open_closed_status = 'OPEN'
order_status = temp_row[3]
if order_status == 'FAILED':
open_closed_status = 'CLOSED'
elif order_status == 'REMOVED':
open_closed_status = 'CLOSED'
elif order_status == 'NO_PENDING_ACTION':
open_closed_status = 'CLOSED'
elif order_status == 'PENDING_REMOVED':
open_closed_status = 'CLOSED'
else:
# check the shipping group status
any_open_sg = False
for sd_row in xOrder.so_reportL:
if sd_row[45] == 'PENDING_SHIPMENT':
any_open_sg = True
elif sd_row[45] == 'PROCESSING':
any_open_sg = True
elif sd_row[45] == 'INITIAL':
any_open_sg = True
elif sd_row[45] == 'PENDING_MERCHANT_ACTION':
any_open_sg = True
if any_open_sg == False:
open_closed_status = 'CLOSED'
#
if open_closed_status == 'CLOSED':
ord_open_ind = 'False'
else:
ord_open_ind = 'True'
return ord_open_ind,
我已使用案例陈述将此逻辑转换为sql:
SELECT CASE WHEN (
CASE WHEN CASE WHEN ord_status IN ('FAILED','REMOVED','NO_PENDING_ACTION', 'NO_PEDNING_ACTION', 'PENDING_REMOVED') THEN 'CLOSED'
WHEN ord_status NOT IN ('FAILED','REMOVED','NO_PENDING_ACTION', 'NO_PEDNING_ACTION', 'PENDING_REMOVED') THEN 'OPEN'
WHEN SHIPPING_STATUS IN ('PENDING_SHIPMENT','PROCESSING','INITIAL','PENDING_MERCHANT_ACTION') THEN 'True'
ELSE 'False' END = 'False' THEN 'CLOSED' AS OPEN_ORDER_IND
请注意,sd_row [45]与Shipping_status字段相同。 我的个案陈述是对的吗?
答案 0 :(得分:0)
可能(未经测试):
SELECT CASE WHEN ord_status IN ('FAILED','REMOVED','NO_PENDING_ACTION', 'PENDING_REMOVED')
OR shipping_status NOT IN ('PENDING_SHIPMENT','PROCESSING','INITIAL','PENDING_MERCHANT_ACTION')
THEN 'False'::text
ELSE 'True' END AS ord_open_ind
返回类型最好应该是boolean
,但这不在您的问题中。