【问题标题】:ERROR: syntax error at or near "settlement_date"错误:“结算日期”或附近的语法错误
【发布时间】:2015-04-07 16:39:27
【问题描述】:

错误:在“结算日期”或附近出现语法错误 第 4 行:如果结算日期 > '2015-01-01' ^ ********** 错误**********

错误:在“结算日期”或附近出现语法错误

SQL 状态:42601

字符:50

 update  "Recon".ship_error 
  set 
  if settlement_date > '2015-01-01'
              then
                        shipping_fee  = case

            when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                    when shipping_zone = 'NATIONAL' and total_weight <= 0.5 and order_status !='return_completed' then -55
                when shipping_zone = 'LOCAL' and total_weight <= 0.5 and order_status !='return_completed' then -29.4
                when shipping_zone = 'ZONAL' and total_weight <= 0.5 and order_status !='return_completed' then -55

                when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                when shipping_zone = 'NATIONAL' and total_weight >= 0.5 and total_weight <= 1  and order_status !='return_completed' then -55
                when shipping_zone = 'LOCAL' and total_weight >= 0.5 and total_weight <=1 order_status !='return_completed' then -29.4
                when shipping_zone = 'ZONAL' and total_weight >= 0.5 and total_weight <=1 order_status !='return_completed' then -55
        end
        end
        if settlement_date <= '2015-01-01'
          then
                            when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                    when shipping_zone = 'NATIONAL' and total_weight <= 0.5 and order_status !='return_completed' then -43.4
                when shipping_zone = 'LOCAL' and total_weight <= 0.5 and order_status !='return_completed' then -24.3
                when shipping_zone = 'ZONAL' and total_weight <= 0.5 and order_status !='return_completed' then -43.4

                when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                when shipping_zone = 'NATIONAL' and total_weight >= 0.5 and total_weight <= 1  and order_status !='return_completed' then -86.8
                when shipping_zone = 'LOCAL' and total_weight >= 0.5 and total_weight <=1 order_status !='return_completed' then -58.3
                when shipping_zone = 'ZONAL' and total_weight >= 0.5 and total_weight <=1 order_status !='return_completed' then -86.8
            end
      end 
     from "Recon".ship_error;

或者我也试过这个代码

   update  "Recon".ship_error 
  set shipping_fee  = case

  when settlement_date > '2015-01-01'
              then
                        --shipping_fee  = case

            when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                    when shipping_zone = 'NATIONAL' and total_weight <= 0.5 and order_status !='return_completed' then -55
                when shipping_zone = 'LOCAL' and total_weight <= 0.5 and order_status !='return_completed' then -29.4
                when shipping_zone = 'ZONAL' and total_weight <= 0.5 and order_status !='return_completed' then -55

                when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                when shipping_zone = 'NATIONAL' and total_weight >= 0.5 and total_weight <= 1  and order_status !='return_completed' then -55
                when shipping_zone = 'LOCAL' and total_weight >= 0.5 and total_weight <=1 order_status !='return_completed' then -29.4
                when shipping_zone = 'ZONAL' and total_weight >= 0.5 and total_weight <=1 order_status !='return_completed' then -55
        end
        end
     when   settlement_date <= '2015-01-01'
          then
                            when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                    when shipping_zone = 'NATIONAL' and total_weight <= 0.5 and order_status !='return_completed' then -43.4
                when shipping_zone = 'LOCAL' and total_weight <= 0.5 and order_status !='return_completed' then -24.3
                when shipping_zone = 'ZONAL' and total_weight <= 0.5 and order_status !='return_completed' then -43.4

                when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                when shipping_zone = 'NATIONAL' and total_weight >= 0.5 and total_weight <= 1  and order_status !='return_completed' then -86.8
                when shipping_zone = 'LOCAL' and total_weight >= 0.5 and total_weight <=1 order_status !='return_completed' then -58.3
                when shipping_zone = 'ZONAL' and total_weight >= 0.5 and total_weight <=1 order_status !='return_completed' then -86.8
            end
      end 
   from  "Recon".ship_error;

【问题讨论】:

  • 是有问题还是您只是为了娱乐而发布随机错误消息?
  • PostgreSQL 不支持if 子句。不过,您可以使用嵌套的 CASE 子句。
  • 我也试过嵌套的 case 子句@SimoKivistö

标签: postgresql-9.1


【解决方案1】:
update  "Recon".ship_error 
  set shipping_fee  = case



                        --shipping_fee  = case

            when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                    when shipping_zone = 'NATIONAL' and total_weight <= 0.5 and settlement_date > '2015-01-01' and order_status !='return_completed' then -55
                when shipping_zone = 'LOCAL' and total_weight <= 0.5 and settlement_date > '2015-01-01' and order_status !='return_completed' then -29.4
                when shipping_zone = 'ZONAL' and total_weight <= 0.5 and settlement_date > '2015-01-01' and order_status !='return_completed' then -55

                when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                when shipping_zone = 'NATIONAL' and total_weight >= 0.5 and total_weight <= 1  and settlement_date > '2015-01-01' and order_status !='return_completed' then -55
                when shipping_zone = 'LOCAL' and total_weight >= 0.5 and total_weight <=1 and settlement_date > '2015-01-01' and order_status !='return_completed' then -29.4
                when shipping_zone = 'ZONAL' and total_weight >= 0.5 and total_weight <=1 and settlement_date > '2015-01-01' and order_status !='return_completed' then -55
                    when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                    when shipping_zone = 'NATIONAL' and total_weight <= 0.5 and settlement_date <= '2015-01-01' and order_status !='return_completed' then -43.4
                when shipping_zone = 'LOCAL' and total_weight <= 0.5 and settlement_date <= '2015-01-01' and order_status !='return_completed' then -29.4
                when shipping_zone = 'ZONAL' and total_weight <= 0.5 and settlement_date <= '2015-01-01' and order_status !='return_completed' then -43.4

                when shipping_zone = 'NA' and  order_status !='return_completed' then 0
                when shipping_zone = 'NATIONAL' and total_weight >= 0.5 and total_weight <= 1  and settlement_date <= '2015-01-01' and order_status !='return_completed' then -86.8
                when shipping_zone = 'LOCAL' and total_weight >= 0.5 and total_weight <=1 and settlement_date <= '2015-01-01' and order_status !='return_completed' then -58.8
                when shipping_zone = 'ZONAL' and total_weight >= 0.5 and total_weight <=1 and settlement_date <= '2015-01-01' and order_status !='return_completed' then -86.8
            end ;

【讨论】:

    【解决方案2】:

    您的查询中有几个错误(有几次缺少and 和额外的end 等)。这是更正的查询:

    UPDATE "Recon".ship_error
    SET shipping_fee = CASE 
            WHEN settlement_date > '2015-01-01'
                THEN
                    CASE 
                        WHEN shipping_zone = 'NA'
                            AND order_status != 'return_completed'
                            THEN 0
                        WHEN shipping_zone = 'NATIONAL'
                            AND total_weight <= 0.5
                            AND order_status != 'return_completed'
                            THEN - 55
                        WHEN shipping_zone = 'LOCAL'
                            AND total_weight <= 0.5
                            AND order_status != 'return_completed'
                            THEN - 29.4
                        WHEN shipping_zone = 'ZONAL'
                            AND total_weight <= 0.5
                            AND order_status != 'return_completed'
                            THEN - 55
                        WHEN shipping_zone = 'NA'
                            AND order_status != 'return_completed'
                            THEN 0
                        WHEN shipping_zone = 'NATIONAL'
                            AND total_weight >= 0.5
                            AND total_weight <= 1
                            AND order_status != 'return_completed'
                            THEN - 55
                        WHEN shipping_zone = 'LOCAL'
                            AND total_weight >= 0.5
                            AND total_weight <= 1
                            AND order_status != 'return_completed'
                            THEN - 29.4
                        WHEN shipping_zone = 'ZONAL'
                            AND total_weight >= 0.5
                            AND total_weight <= 1
                            AND order_status != 'return_completed'
                            THEN - 55
                        END
            WHEN settlement_date <= '2015-01-01'
                THEN CASE 
                        WHEN shipping_zone = 'NA'
                            AND order_status != 'return_completed'
                            THEN 0
                        WHEN shipping_zone = 'NATIONAL'
                            AND total_weight <= 0.5
                            AND order_status != 'return_completed'
                            THEN - 43.4
                        WHEN shipping_zone = 'LOCAL'
                            AND total_weight <= 0.5
                            AND order_status != 'return_completed'
                            THEN - 24.3
                        WHEN shipping_zone = 'ZONAL'
                            AND total_weight <= 0.5
                            AND order_status != 'return_completed'
                            THEN - 43.4
                        WHEN shipping_zone = 'NA'
                            AND order_status != 'return_completed'
                            THEN 0
                        WHEN shipping_zone = 'NATIONAL'
                            AND total_weight >= 0.5
                            AND total_weight <= 1
                            AND order_status != 'return_completed'
                            THEN - 86.8
                        WHEN shipping_zone = 'LOCAL'
                            AND total_weight >= 0.5
                            AND total_weight <= 1
                            AND order_status != 'return_completed'
                            THEN - 58.3
                        WHEN shipping_zone = 'ZONAL'
                            AND total_weight >= 0.5
                            AND total_weight <= 1
                            AND order_status != 'return_completed'
                            THEN - 86.8
                        END
            END;
    

    【讨论】:

      猜你喜欢
      • 2015-07-24
      • 2017-02-20
      • 1970-01-01
      • 2017-07-15
      • 2010-12-24
      • 2016-07-10
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多