sum (case when shipment_type = '0' then no_of_awbs else 0 end) as c2k_exp_awb;这是一条sparksql,在使用sum 的时候会报错,请修正
时间: 2024-05-03 20:19:09 浏览: 155
这个错误可能是由于 `no_of_awbs` 列的数据类型不是数值类型(如整数或浮点数)导致的。你可以尝试将该列转换为数值类型,例如使用 `cast` 函数:
```
sum(case when shipment_type = '0' then cast(no_of_awbs as double) else 0 end) as c2k_exp_awb
```
或者你也可以使用 `if` 函数代替 `case when` 子句:
```
sum(if(shipment_type = '0', no_of_awbs, 0)) as c2k_exp_awb
```
这样就可以避免因数据类型不匹配而引起的错误了。
相关问题
SELECT PIS.SHOW_FLT_DETAIL AS SHOW_FLT_DETAIL -- new , PIS.SHOW_AWB_DETAIL AS SHOW_AWB_DETAIL -- new , PIS.DISPLAY_AIRLINE_CODE AS CARRIER_CODE , DECODE(PIS.REVERT_FLOW,'N',PIS.FLOW_TYPE,DECODE(PIS.FLOW_TYPE,'I','E','I')) AS FLOW_TYPE , PIS.SHIP_TO_LOCATION AS SHIP_TO_LOCATION , PIS.INVOICE_SEQUENCE AS INVOICE_SEQUENCE , PFT.FLIGHT_DATE AS FLIGHT_DATE , PFT.FLIGHT_CARRIER_CODE AS FLIGHT_CARRIER_CODE , PFT.FLIGHT_SERIAL_NUMBER AS FLIGHT_SERIAL_NUMBER , PFT.FLOW_TYPE AS AIRCRAFT_FLOW , FAST.AIRCRAFT_SERVICE_TYPE AS AIRCRAFT_SERVICE_TYPE , PPT.AWB_NUMBER AS AWB_NUMBER , PPT.WEIGHT AS WEIGHT , PPT.CARGO_HANDLING_OPERATOR AS CARGO_HANDLING_OPERATOR , PPT.SHIPMENT_PACKING_TYPE AS SHIPMENT_PACKING_TYPE , PPT.SHIPMENT_FLOW_TYPE AS SHIPMENT_FLOW_TYPE , PPT.SHIPMENT_BUILD_TYPE AS SHIPMENT_BUILD_TYPE , PPT.SHIPMENT_CARGO_TYPE AS SHIPMENT_CARGO_TYPE , PPT.REVENUE_TYPE AS REVENUE_TYPE , PFT.JV_FLIGHT_CARRIER_CODE AS JV_FLIGHT_CARRIER_CODE , PPT.PORT_TONNAGE_UID AS PORT_TONNAGE_UID , PPT.AWB_UID AS AWB_UID , PIS.INVOICE_SEPARATION_UID AS INVOICE_SEPARATION_UID , PFT.FLIGHT_TONNAGE_UID AS FLIGHT_TONNAGE_UID FROM PN_FLT_TONNAGES PFT , FZ_AIRLINES FA , PN_TONNAGE_FLT_PORTS PTFP , PN_PORT_TONNAGES PPT , FF_AIRCRAFT_SERVICE_TYPES FAST , SR_PN_INVOICE_SEPARATIONS PIS --new , SR_PN_INVOICE_SEP_DETAILS PISD--new , SR_PN_INV_SEP_PORT_TONNAGES PISPT --new WHERE PFT.FLIGHT_OPERATION_DATE >= trunc( CASE :rundate WHEN TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN ADD_MONTHS(SYSDATE,-1) ELSE ADD_MONTHS(:rundate,-1) END, 'MON') AND PFT.FLIGHT_OPERATION_DATE < trunc( CASE :rundate WHEN TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN TRUNC(SYSDATE) ELSE TRUNC(:rundate) END, 'MON') AND PFT.TYPE IN ('C', 'F') AND PFT.RECORD_TYPE = 'M' AND (PFT.TERMINAL_OPERATOR NOT IN ('X', 'A') OR (PFT.TERMINAL_OPERATOR <> 'X' AND FA.CARRIER_CODE IN (SELECT * FROM SPECIAL_HANDLING_AIRLINE) AND PPT.REVENUE_TYPE IN (SELECT * FROM SPECIAL_REVENUE_TYPE) AND PPT.SHIPMENT_FLOW_TYPE IN (SELECT * FROM SPECIAL_SHIPMENT_FLOW_TYPE) AND PFT.FLIGHT_OPERATION_DATE >= (select EFF_DATE from SPECIAL_HANDLING_EFF_DATE) )) AND PFT.DELETING_DATETIME IS NULL AND FA.AIRLINE_UID = PFT.AIRLINE_UID AND FA.DELETING_DATETIME IS NULL AND PTFP.FLIGHT_TONNAGE_UID = PFT.FLIGHT_TONNAGE_UID AND PTFP.RECORD_TYPE = 'M' AND PTFP.DELETING_DATETIME IS NULL AND PPT.TONNAGE_FLIGHT_PORT_UID (+)= PTFP.TONNAGE_FLIGHT_PORT_UID AND PPT.RECORD_TYPE (+)= 'M' AND PPT.DISCREPANCY_TYPE (+)= 'NONE' AND PPT.ADJUSTMENT_INC_FLAG (+)= 'Y' AND PPT.DELETING_DATETIME (+) IS NULL AND FAST.AIRCRAFT_SERVICE_TYPE_UID = PFT.AIRCRAFT_SERVICE_TYPE_UID AND FAST.DELETING_DATETIME IS NULL AND PIS.TEMPORAL_NAME = TO_CHAR((CASE :rundate --new WHEN TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN TRUNC(SYSDATE) ELSE TRUNC(:rundate) END ), 'YYYYMM') || '00' AND PIS.INVOICE_SEPARATION_UID = PISD.INVOICE_SEPARATION_UID --new AND PISD.INVOICE_SEP_DETAIL_UID = PISPT.INVOICE_SEP_DETAIL_UID --new AND PISPT.PORT_TONNAGE_UID = PPT.PORT_TONNAGE_UID --new AND PIS.PRINT_SUPPORTING_DOC = 'Y';上面是oracle的写法,请转成spark SQL的写法。
以下是转换为 Spark SQL 查询的代码:
```
SELECT
PIS.SHOW_FLT_DETAIL AS SHOW_FLT_DETAIL,
PIS.SHOW_AWB_DETAIL AS SHOW_AWB_DETAIL,
PIS.DISPLAY_AIRLINE_CODE AS CARRIER_CODE,
CASE
WHEN PIS.REVERT_FLOW = 'N' THEN PIS.FLOW_TYPE
ELSE CASE
WHEN PIS.FLOW_TYPE = 'I' THEN 'E'
ELSE 'I'
END
END AS FLOW_TYPE,
PIS.SHIP_TO_LOCATION AS SHIP_TO_LOCATION,
PIS.INVOICE_SEQUENCE AS INVOICE_SEQUENCE,
PFT.FLIGHT_DATE AS FLIGHT_DATE,
PFT.FLIGHT_CARRIER_CODE AS FLIGHT_CARRIER_CODE,
PFT.FLIGHT_SERIAL_NUMBER AS FLIGHT_SERIAL_NUMBER,
PFT.FLOW_TYPE AS AIRCRAFT_FLOW,
FAST.AIRCRAFT_SERVICE_TYPE AS AIRCRAFT_SERVICE_TYPE,
PPT.AWB_NUMBER AS AWB_NUMBER,
PPT.WEIGHT AS WEIGHT,
PPT.CARGO_HANDLING_OPERATOR AS CARGO_HANDLING_OPERATOR,
PPT.SHIPMENT_PACKING_TYPE AS SHIPMENT_PACKING_TYPE,
PPT.SHIPMENT_FLOW_TYPE AS SHIPMENT_FLOW_TYPE,
PPT.SHIPMENT_BUILD_TYPE AS SHIPMENT_BUILD_TYPE,
PPT.SHIPMENT_CARGO_TYPE AS SHIPMENT_CARGO_TYPE,
PPT.REVENUE_TYPE AS REVENUE_TYPE,
PFT.JV_FLIGHT_CARRIER_CODE AS JV_FLIGHT_CARRIER_CODE,
PPT.PORT_TONNAGE_UID AS PORT_TONNAGE_UID,
PPT.AWB_UID AS AWB_UID,
PIS.INVOICE_SEPARATION_UID AS INVOICE_SEPARATION_UID,
PFT.FLIGHT_TONNAGE_UID AS FLIGHT_TONNAGE_UID
FROM
PN_FLT_TONNAGES PFT
JOIN FZ_AIRLINES FA ON FA.AIRLINE_UID = PFT.AIRLINE_UID AND FA.DELETING_DATETIME IS NULL
JOIN PN_TONNAGE_FLT_PORTS PTFP ON PTFP.FLIGHT_TONNAGE_UID = PFT.FLIGHT_TONNAGE_UID AND PTFP.RECORD_TYPE = 'M' AND PTFP.DELETING_DATETIME IS NULL
LEFT JOIN PN_PORT_TONNAGES PPT ON PPT.TONNAGE_FLIGHT_PORT_UID = PTFP.TONNAGE_FLIGHT_PORT_UID AND PPT.RECORD_TYPE = 'M' AND PPT.DISCREPANCY_TYPE = 'NONE' AND PPT.ADJUSTMENT_INC_FLAG = 'Y' AND PPT.DELETING_DATETIME IS NULL
JOIN FF_AIRCRAFT_SERVICE_TYPES FAST ON FAST.AIRCRAFT_SERVICE_TYPE_UID = PFT.AIRCRAFT_SERVICE_TYPE_UID AND FAST.DELETING_DATETIME IS NULL
JOIN SR_PN_INVOICE_SEPARATIONS PIS ON PIS.TEMPORAL_NAME = CONCAT(YEAR(:rundate), RIGHT(CONCAT('0', MONTH(:rundate)), 2), '00') AND PIS.INVOICE_SEPARATION_UID = PISD.INVOICE_SEPARATION_UID AND PIS.PRINT_SUPPORTING_DOC = 'Y'
JOIN SR_PN_INVOICE_SEP_DETAILS PISD ON PISD.INVOICE_SEP_DETAIL_UID = PISPT.INVOICE_SEP_DETAIL_UID
JOIN SR_PN_INV_SEP_PORT_TONNAGES PISPT ON PISPT.PORT_TONNAGE_UID = PPT.PORT_TONNAGE_UID
WHERE
PFT.FLIGHT_OPERATION_DATE >= trunc(CASE
WHEN :rundate = TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN ADD_MONTHS(SYSDATE, -1)
ELSE ADD_MONTHS(:rundate, -1)
END, 'MON')
AND PFT.FLIGHT_OPERATION_DATE < trunc(CASE
WHEN :rundate = TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN TRUNC(SYSDATE)
ELSE TRUNC(:rundate)
END, 'MON')
AND PFT.TYPE IN ('C', 'F')
AND PFT.RECORD_TYPE = 'M'
AND (
PFT.TERMINAL_OPERATOR NOT IN ('X', 'A')
OR (
PFT.TERMINAL_OPERATOR <> 'X'
AND FA.CARRIER_CODE IN (SELECT * FROM SPECIAL_HANDLING_AIRLINE)
AND PPT.REVENUE_TYPE IN (SELECT * FROM SPECIAL_REVENUE_TYPE)
AND PPT.SHIPMENT_FLOW_TYPE IN (SELECT * FROM SPECIAL_SHIPMENT_FLOW_TYPE)
AND PFT.FLIGHT_OPERATION_DATE >= (SELECT EFF_DATE FROM SPECIAL_HANDLING_EFF_DATE)
)
)
AND PFT.DELETING_DATETIME IS NULL;
```
请注意,需要将 `TRUNC` 和 `CONCAT` 函数替换为 Spark SQL 中的相应函数,并使用 `JOIN` 语句替换 `LEFT JOIN` 语句。还需要更改零散的子查询,以便在 Spark SQL 中使用。
ifdef USE_ALGO_AWB alg_awb #(BITS) alg_awb_i0(pclk, rst_n, stat_awb_done, stat_awb_pix_cnt, stat_awb_sum_r, stat_awb_sum_g+(^stat_awb_hist_data)/*XXX DEBUG*/, stat_awb_sum_b, wb_rgain, wb_ggain, wb_bgain); `else
这是一个Verilog中的条件编译语句,用于根据宏定义来选择是否实例化模块。
`ifdef USE_ALGO_AWB` 表示如果宏定义 `USE_ALGO_AWB` 被定义,则执行下面的,否则跳过下面的代码。
`alg_awb #(BITS) alg_awb_i0(...)` 表示实例化模块 `alg_awb`,并命名为 `alg_awb_i0`。`#(BITS)` 表示模块的参数列表,其中 `BITS` 是一个参数。
`pclk, rst_n, stat_awb_done, stat_awb_pix_cnt, stat_awb_sum_r, stat_awb_sum_g+(^stat_awb_hist_data)/*XXX DEBUG*/, stat_awb_sum_b, wb_rgain, wb_ggain, wb_bgain` 表示将这些信号连接到 `alg_awb` 模块的输入端口。
`` `else` `` 表示如果宏定义 `USE_ALGO_AWB` 没有被定义,则执行下面的代码。
综上,该语句用于根据宏定义 `USE_ALGO_AWB` 的值来选择是否实例化 `alg_awb` 模块,并将顶层模块的信号连接到该模块的输入端口。如果 `USE_ALGO_AWB` 被定义,则实例化该模块,否则不实例化该模块。
阅读全文