[英]Pervasive SQL subquery with variable
To get the data I need in less than several minutes of execution, I have 2 separate queries:为了在不到几分钟的执行时间内获得我需要的数据,我有 2 个单独的查询:
select
' ' as POHI_ACD,
LTRIM(RTRIM(OePo_Header.PO_ID)) as POHI_PONUM,
' ' as POHI_POTYP,
' ' as POHI_VND,
LTRIM(RTRIM(OePo_Header.CLIENT_ID)) as POHI_CO,
'B1' as POHI_WA,
'n/a' as PODI_COMMNT, -- Comment filled in separate query
' ' as POHI_CUSER,
' ' as POHI_TSCRT,
' ' as POHI_PGMUP,
' ' as POHI_USRUP,
'01/01/0001 00:00:00' as POHI_TSLUP,
' ' as POHI_FILEPATH
from (
OePo_Header
inner join OePo_Item
on OePo_Header.PO_ID = OePo_Item.PO_ID
)
left join OePo_Remarks
on OePo_Header.WHSE_INST_ID = OePo_Remarks.AUTOKEY
and OePo_Header.PO_ID = OePo_Remarks.PO_ID
where (
(OePo_Item.UNITQTY_ORDER = 1)
and (OePo_Header.SHIPTO_ID like '%*B1')
and(OePo_Item.CLOSED_FLG = 'N')
and(OePo_Item.INVOICED_COUNT = 0)
and(OePo_Item.ITEM_TYPE = '2')
and(OePo_Item.VENDOR_ID <> 'WHSE')
)
group by
LTRIM(RTRIM(OePo_Header.PO_ID)),
LTRIM(RTRIM(OePo_Header.CLIENT_ID)),
'B1',
if(TEXT = Null, '', TEXT),
'',
'',
OEPO_HEADER.PO_ID
order by
OEPO_HEADER.PO_ID;
and和
select
OePo_Remarks.TEXT as POHI_COMMNT
from (
OePo_Remarks
inner join OePo_Header
on OePo_Remarks.AUTOKEY = OePo_Header.WHSE_INST_ID
and OePo_Remarks.PO_ID = OePo_Header.PO_ID
)
where
OePo_Remarks.PO_ID like '%208672%' -- fill in with appropriate PO num from outer code, 208672 is just an example. (PO_IDs can be padded, hence the %s)
OePo_Remarks.zflags like '%w%'
Instead of combining the data in the code which calls both queries, how would I set up a subquery which uses data from the outer query to get what I'm looking for?不是在调用两个查询的代码中组合数据,我将如何设置一个子查询,它使用来自外部查询的数据来获取我正在寻找的内容? Something like:
就像是:
select
' ' as POHI_ACD,
LTRIM(RTRIM(OePo_Header.PO_ID)) as POHI_PONUM,
' ' as POHI_POTYP,
' ' as POHI_VND,
LTRIM(RTRIM(OePo_Header.CLIENT_ID)) as POHI_CO,
'B1' as POHI_WA,
-- subquery start
( select
OePo_Remarks.TEXT
from (
OePo_Remarks
inner join OePo_Header
on OePo_Remarks.AUTOKEY = OePo_Header.WHSE_INST_ID
and OePo_Remarks.PO_ID = OePo_Header.PO_ID
)
where (
OePo_Remarks.PO_ID like CONCAT('%', POHI_PONUM, '%') -- Here is where I need the correct PONUM from the outer query.
and OePo_Remarks.zflags like '%w%'
)
) as POHI_COMMNT, --subquery end
' ' as POHI_CUSER,
' ' as POHI_TSCRT,
' ' as POHI_PGMUP,
' ' as POHI_USRUP,
'01/01/0001 00:00:00' as POHI_TSLUP,
' ' as POHI_FILEPATH
from (
OePo_Header
inner join OePo_Item
on OePo_Header.PO_ID = OePo_Item.PO_ID
)
left join OePo_Remarks
on OePo_Header.WHSE_INST_ID = OePo_Remarks.AUTOKEY
and OePo_Header.PO_ID = OePo_Remarks.PO_ID
where (
(OePo_Item.UNITQTY_ORDER = 1)
and (OePo_Header.SHIPTO_ID like '%*B1')
and(OePo_Item.CLOSED_FLG = 'N')
and(OePo_Item.INVOICED_COUNT = 0)
and(OePo_Item.ITEM_TYPE = '2')
and(OePo_Item.VENDOR_ID <> 'WHSE')
)
group by
LTRIM(RTRIM(OePo_Header.PO_ID)),
LTRIM(RTRIM(OePo_Header.CLIENT_ID)),
'B1',
if(OePo_Remarks.TEXT = Null, '', OePo_Remarks.TEXT),
'',
'',
OePo_Header.PO_ID
order by
OePo_Header.PO_ID;
Using a single much less efficient query, I can get the data I need in a single query, but it takes several minutes to execute in this query, and could take orders of magnitude longer in a separate query which looks for similar (but not same) data.使用一个效率低得多的查询,我可以在单个查询中获取我需要的数据,但在此查询中执行需要几分钟的时间,并且在寻找相似(但不相同)的单独查询中可能需要更长的时间) 数据。 Is there a way to have this work with variables?
有没有办法让这个工作与变量? (Unless there is a
join
combination which will allow me to collect the right data in less than several minutes?) (除非有一个
join
组合可以让我在不到几分钟的时间内收集正确的数据?)
You would need to default a load of column values in to make them match but I would try using a union to make your two selects come out as a single result set.您需要默认加载列值以使它们匹配,但我会尝试使用联合来使您的两个选择作为单个结果集出现。 Its not uncommon to find two simpler queries combined in a union more quicker than one complicated query in my experience.
根据我的经验,找到两个更简单的查询组合在一起比一个复杂的查询更快的情况并不少见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.