我有以下查询,我根据给定的动态日期范围(即 10-01-2024 - 10-28-2024)计算平均值。这是如下所示的第
SUM(ROUND(QTY_AVG.AVERAGE,2)) DATE_RANGE_AVERAGE
列。
我有另一个子查询用于连接,该子查询根据硬编码的日期范围计算年度平均值,并且我根据 ITEM_ID (
QTY_AVG_YEAR.ITEM_ID = rsl.ITEM_ID
) 联接回主查询,以便我可以将年度平均值显示为动态平均列旁边的一列。问题是,别名为“QTY_AVG_YEAR”的连接使 DATE_RANGE_AVERAGE
列发生倾斜,因为我相信它在整个年份期间获取更多行。
我怎样才能拥有两个查询,它们具有独立的结果,互不影响,但根据项目 ID 在结果中一起显示?
我本质上是想查询一个数据集以获取动态范围的平均值,并查询另一个数据集的当年平均值(独立地以免影响连接的行),然后根据 ITEM_ID 在同一行中输出作为附加栏目。
我希望这是有道理的。
select
esib.item_number inv_item_id, --:INV_ITEM_ID,
pla.item_description descr254_mixed
,SUM(ROUND(QTY_AVG.AVERAGE,2)) DATE_RANGE_AVERAGE
--,SUM(ROUND(QTY_AVG_YEAR.AVERAGE_YEAR,2)) YEAR_AVERAGE
from po_headers_all pha,
po_lines_all pla,
po_line_locations_all plla,
po_distributions_all pda,
rcv_shipment_headers rsh,
rcv_shipment_lines rsl,
rcv_transactions rt,
egp_system_items_b esib,
inv_org_parameters iop,
GL_CODE_COMBINATIONS GCC,
HR_ALL_ORGANIZATION_UNITS_F_VL HR,
INV_UOM_CONVERSIONS INTRACONV
, (SELECT SUM(LN.QUANTITY_RECEIVED)/(:P_TO_DATE - :P_FROM_DATE) AVERAGE, SHIPMENT_HEADER_ID, shipment_line_id
FROM rcv_shipment_lines LN
WHERE LN.CREATION_DATE >= :P_FROM_DATE
AND LN.CREATION_DATE <= :P_TO_DATE
GROUP BY SHIPMENT_HEADER_ID, shipment_line_id ) QTY_AVG
, (SELECT SUM(LN.QUANTITY_RECEIVED)/365 AVERAGE_YEAR, SHIPMENT_HEADER_ID, shipment_line_id, ITEM_ID
FROM rcv_shipment_lines LN
WHERE TO_CHAR(LN.CREATION_DATE, 'yyyy-mm-dd') >= '2023-10-23'
AND TO_CHAR(LN.CREATION_DATE, 'yyyy-mm-dd') <= '2024-09-30'
--AND LN.ITEM_ID = :INV_ITEM_ID --rsl.ITEM_ID
--AND ITEM_ID =100000030622932
GROUP BY SHIPMENT_HEADER_ID, shipment_line_id, ITEM_ID ) QTY_AVG_YEAR
WHERE pha.po_header_id = pla.po_header_id
and pla.po_line_id = plla.po_line_id
and plla.line_location_id = pda.line_location_id
and rt.po_header_id = pha.po_header_id
AND transaction_type = 'RECEIVE'
AND GCC.CODE_COMBINATION_ID = PDA.CODE_COMBINATION_ID
AND pha.SEGMENT1 = NVL(:P_PO_ID, pha.SEGMENT1)
AND HR.organization_id = rt.ORGANIZATION_ID
AND INTRACONV.UOM_CODE(+) = CASE WHEN esib.UNIT_OF_ISSUE IS NOT NULL THEN esib.UNIT_OF_ISSUE ELSE rsl.UOM_CODE END
AND INTRACONV.INVENTORY_ITEM_ID(+) = esib.INVENTORY_ITEM_ID
AND QTY_AVG.SHIPMENT_HEADER_ID = rsl.SHIPMENT_HEADER_ID
AND QTY_AVG.shipment_line_id = rsl.shipment_line_id
AND QTY_AVG_YEAR.ITEM_ID = rsl.ITEM_ID
AND YEAR_AVERAGE.ITEM_ID(+) = rsl.ITEM_ID
AND rt.transaction_date BETWEEN
to_date(nvl( to_char(:P_FROM_DATE,'yyyy-mm-dd') , to_char(sysdate - 30, 'YYYY-MM-DD')) ||' 00:00:00', 'YYYY-MM-DD HH24:Mi:SS')
AND
to_date(nvl( to_char(:P_TO_DATE,'yyyy-mm-dd') , to_char(sysdate - 1 , 'YYYY-MM-DD')) ||' 23:59:59', 'YYYY-MM-DD HH24:Mi:SS')
AND esib.item_number IN ('111090',
'112252',
'14438',
'20',
'112399',
'110844',
'221',
'85866',
'112304',
'219',
'112078' )
GROUP BY
esib.item_number ,
pla.item_description
仅使用动态日期范围列的正确结果:
INV_ITEM_ID DESCR254_MIXED DATE_RANGE_AVERAGE
------------------------------------------------------------
219 SALINE, 1000CC IRRIGATION 5.37
221 SALINE, 3000 CC IRRIGATION 5.36
14438 SOLN, SOD CHLOR .9% 250CC 1.64
112252 SOLUTION INTRAVENOUS SODIUM 2.41
112304 SOL IRRIG NACL 0.9% USP 3.56
动态日期范围和年份平均列的结果不正确:
INV_ITEM_ID DESCR254_MIXED DATE_RANGE_AVERAGE YEAR_AVERAGE
-----------------------------------------------------------------------------
219 SALINE, 1000CC IRRIGATION 1154.55 72.39
221 SALINE, 3000 CC IRRIGATION 766.48 105.1
14438 SOLN, SOD CHLOR .9% 250CC 354.24 30.25
112252 SOLUTION INTRAVENOUS SODIUM 245.82 11.52
112304 SOL IRRIG NACL 0.9% USP 202.92 7.6
这很难理解,因为根本没有示例数据,并且提供的 sql 不可读,但如果您的问题是如何从相同的源数据和同一行中获取日期范围日平均值和一年的日平均值那么一种选择是使用 Case 表达式的条件聚合。 如下所示:
-- S a m p l e D a t a :
Create Table rcv_shipment_lines ( SHIPMENT_HEADER_ID Number(6),
SHIPMENT_LINE_ID Number(6),
ITEM_ID Number(6),
QUANTITY_RECEIVED Number(12,3),
CREATION_DATE Date
);
Insert Into rcv_shipment_lines
Select 1, 11, 221, 1000, DATE '2024-01-01' From Dual Union All
Select 1, 11, 221, 1500, DATE '2024-02-01' From Dual Union All
Select 1, 12, 221, 2000, DATE '2024-04-01' From Dual Union All
Select 1, 12, 221, 2500, DATE '2024-05-01' From Dual Union All
Select 2, 13, 219, 7000, DATE '2024-04-01' From Dual Union All
Select 2, 14, 219, 1500, DATE '2024-05-01' From Dual Union All
Select 3, 33, 219, 1000, DATE '2024-08-01' From Dual Union All
Select 3, 44, 219, 6500, DATE '2024-09-01' From Dual;
...通过上述简化数据,您可以获得两个平均值...
-- S Q L :
Select r.ITEM_ID, 'Some mixed descriptions for item id ' || r.item_id "DESCR254_MIXED",
ROUND( Sum( Case When r.CREATION_DATE Between DATE '2024-03-28' And DATE '2024-05-10'
Then QUANTITY_RECEIVED
Else 0
End ) / ( DATE '2024-05-10' - DATE '2024-03-28' ), 2 ) "DATE_RANGE_AVERAGE",
ROUND( Sum( Case When EXTRACT(Year From r.CREATION_DATE) = EXTRACT(Year From DATE '2024-05-10')
Then QUANTITY_RECEIVED
Else 0
End ) / 365, 2 ) "YEAR_AVERAGE"
From rcv_shipment_lines r
Group By r.ITEM_ID
Order By r.ITEM_ID
结果:
ITEM_ID | DESCR254_MIXED | DATE_RANGE_AVERAGE | YEAR_AVERAGE |
---|---|---|---|
219 | 项目 ID 219 的一些混合描述 | 197.67 | 43.84 |
221 | 项目 ID 221 的一些混合描述 | 104.65 | 19.18 |
注意:
Case 表达式中用作条件的 DATE 应替换为绑定变量 :P_FROM_DATE 和 :P_TO_DATE ...