简体   繁体   中英

Pervasive SQL subquery with variable

To get the data I need in less than several minutes of execution, I have 2 separate queries:

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?)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM