我正在使用以下查询:
SELECT contract,
source_ref1 AS "ORDER_NO",
part_no,
ifsapp.inventory_part_api.Get_description( contract, part_no ) as "DESCRIPTION",
ifsapp.inventory_part_api.Get_unit_meas( contract, part_no ) as "UNIT_MEAS",
date_applied,
(SELECT work_center_no FROM shop_order_operation where inventory_transaction_hist2.source_ref1 = shop_order_operation.order_no) AS "WORK_CENTER",
direction,
SUM( quantity ) AS "QUANTITY",
( CASE
WHEN ( ( To_char( date_time_created, 'HH24:MI ' ) BETWEEN '05:45 ' AND '17:44' ) AND
date_applied = date_created ) THEN 'SHIFT1'
WHEN (( ( To_char( date_time_created, 'HH24:MI' ) BETWEEN '17:45' AND '24:00' ) OR
( To_char( date_time_created, 'HH24:MI' ) BETWEEN '00:00' AND '05:44' ) AND
date_applied = date_created )) THEN 'SHIFT2'
ELSE 'MANUAL_ADJUST'
END ) AS "SHIFT"
FROM ifsapp.INVENTORY_TRANSACTION_HIST2
WHERE part_no IN (select pc.part_no from ifsapp.HH_INV_PART_CHARS pc where pc.accounting_group = '400') AND
contract IN ( 'DE1', 'DE2' ) AND
transaction_code LIKE '%REC%' AND
direction IN ( '+', '-' ) AND
source_ref_type = 'Shop Order' AND
To_char( date_applied, 'YYYY-MM-DD' ) BETWEEN '2017-01-01' AND '2017-12-26'
GROUP BY contract,source_ref1, part_no, ifsapp.inventory_part_api.Get_description( contract, part_no ),
ifsapp.inventory_part_api.Get_unit_meas( contract, part_no ), location_no, date_applied, direction,
date_time_created, date_created
ORDER BY date_applied
此查询返回18292次点击。
如果我删除CASE语句,并从GROUP BY部分删除DATE_TIME_CREATED和DATE_CREATED,则查询将返回1356次命中。
我们使用CASE声明来确定我们的产品产生了什么样的转变。 date_time_created字段基本上是时间戳。它是该表上唯一可用于确定产品生产转移的字段。
但是,它也导致我们获得的命中率是没有它的10倍。
以ORDER_NO 349321为例。如图所示运行查询,我得到61次点击。删除CASE语句,并从GROUP BY部分删除DATE_TIME_CREATED和DATE_CREATED,我得到两个命中。
我们没有两班完成的订单。因此,我不需要知道ORDER_NO 349321的每个事务的时间戳,而且我不需要知道每个时间戳是在Shift 1还是Shift 2.我只需要知道该订单的交易是否是在Shift 1或Shift 2中完成。
有没有办法聚合我从该案例陈述中得到的信息来解决这个问题?我尝试在我的case语句中使用MAX和AVG,但它返回了相同数量的结果。
如果您不需要投影DATE_TIME_CREATED和DATE_CREATED列,请在进行聚合处理之前尝试在子查询中执行“SHIFT”计算。像这样的东西:
SELECT
contract,
source_ref1 AS "ORDER_NO",
part_no,
ifsapp.inventory_part_api.Get_description( contract, part_no ) AS "DESCRIPTION",
ifsapp.inventory_part_api.Get_unit_meas( contract, part_no ) AS "UNIT_MEAS",
date_applied,
(SELECT work_center_no FROM shop_order_operation WHERE inventory_transaction_hist2.source_ref1 = shop_order_operation.order_no) AS "WORK_CENTER",
direction,
SUM(quantity) AS "QUANTITY",
"SHIFT"
FROM
(
SELECT contract, source_ref1, part_no, date_applied, direction, quantity,
( CASE
WHEN ( ( TO_CHAR( date_time_created, 'HH24:MI ' ) BETWEEN '05:45 ' AND '17:44' ) AND
date_applied = date_created ) THEN 'SHIFT1'
WHEN (( ( TO_CHAR( date_time_created, 'HH24:MI' ) BETWEEN '17:45' AND '24:00' ) OR
( TO_CHAR( date_time_created, 'HH24:MI' ) BETWEEN '00:00' AND '05:44' ) AND
date_applied = date_created )) THEN 'SHIFT2'
ELSE 'MANUAL_ADJUST'
END ) AS "SHIFT"
FROM ifsapp.INVENTORY_TRANSACTION_HIST2
WHERE part_no IN (SELECT pc.part_no FROM ifsapp.HH_INV_PART_CHARS pc WHERE pc.accounting_group = '400')
AND contract IN ( 'DE1', 'DE2')
AND transaction_code LIKE '%REC%'
AND direction IN ( '+', '-')
AND source_ref_type = 'Shop Order'
AND TO_CHAR( date_applied, 'YYYY-MM-DD') BETWEEN '2017-01-01' AND '2017-12-26'
) src
GROUP BY
contract,
source_ref1,
part_no,
ifsapp.inventory_part_api.Get_description(contract, part_no),
ifsapp.inventory_part_api.Get_unit_meas(contract, part_no),
location_no,
date_applied,
direction
-- ,date_time_created,
-- date_created
ORDER BY date_applied
我没有任何DDL /数据来测试查询,所以你肯定会得到一些语法错误。但试一试,让我知道它是怎么回事。
另外,检查你的“SHIFT”计算,看起来你有一个没有括号的浮动“OR”。