Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

sql - Flexible Search Query UNION

I would like to add a UNION to this query , Where exactly Should I put any UNION (doesn't matter the code of the UNION , I just want to know where can I put it) in the following Flexible Search Query (I'm not familiar with the Syntax)

   SELECT DISTINCT {o:pk} FROM 
( 
    {{ 
        SELECT 
            MAX({h.startTime}) AS startTime 
        FROM {CronJobHistory AS h JOIN CronJobResult AS r ON {h.result} = {r.pk} } 
        WHERE {h.cronJobCode} = 'ordersCronJob' AND {r.code} = 'SUCCESS' 
    }} 
) LAST, 
( 
    {{ 
        SELECT 
            MAX({h.startTime}) AS startTime 
        FROM {CronJobHistory as h} 
        WHERE {h.cronJobCode} = 'ordersCronJob' 
    }} 
) CURRENT, {Order       AS o 
       JOIN PaymentMode AS pm ON {pm.pk} = {o:paymentMode} 
       JOIN BaseStore AS b ON {o.store} = {b.PK} 
       JOIN OrderProcess AS op ON {o.pk} = {op.order}
    } 
WHERE (({pm:code} != 'asm'  AND  {op:creationtime} > LAST.startTime        AND {op:creationtime} <= CURRENT.startTime) 
    OR ({pm:code} =  'asm'  AND  {o:asmActivationTime} > LAST.startTime   AND {o:asmActivationTime} <= CURRENT.startTime) )  
    AND {o:originalVersion} IS NULL 
    AND 'rows-eu,rows-es' LIKE CONCAT( '%', CONCAT( {b.uid} , '%' ) ) 
AND {op.processDefinitionName} LIKE 'order-process%'

I've tried putting it in the last line , but it doesn't compile.

Any hint?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For UNION queries or INNER queries, you will need to wrap the respective queries between double curly braces.

{{..query1..}} UNION {{..query2..}}

Check below example for flexible query union sample.

SELECT uniontable.PK, uniontable.CODE FROM
(
   {{
      SELECT {c:PK} as PK, {c:code} AS CODE FROM {Chapter AS c}
      WHERE {c:PUBLICATION} LIKE ?pk
   }}
   UNION ALL
   {{
      SELECT {p:PK} as PK, {p:code} AS CODE FROM {Page AS p}
      WHERE {p:PUBLICATION} LIKE ?pk
   }}
) uniontable

You can find FlexibleSearch Tips and Tricks at https://help.sap.com/viewer/d0224eca81e249cb821f2cdf45a82ace/1905/en-US/8bc36ba986691014b48be171221d1f4f.html

Hope it helps!

Fixed the first half of your query...

  SELECT tbl.startTime FROM 
( 
    {{ 
        SELECT 
            MAX({h.startTime}) AS startTime 
        FROM {CronJobHistory AS h JOIN CronJobResult AS r ON {h.result} = {r.pk} } 
        WHERE {h.cronJobCode} = 'ordersCronJob' AND {r.code} = 'SUCCESS' 
    }} 
 UNION 

    {{ 
        SELECT 
            MAX({h.startTime}) AS startTime 
        FROM {CronJobHistory as h} 
        WHERE {h.cronJobCode} = 'ordersCronJob' 
    }} 
) tbl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...