将查询从 Sybase IQ 转换为 impala

Posted

技术标签:

【中文标题】将查询从 Sybase IQ 转换为 impala【英文标题】:Converting a query from Sybase IQ to impala 【发布时间】:2019-02-17 16:58:55 【问题描述】:
SELECT f.exch 
FROM   ( 
              SELECT 
                     CASE 
                            WHEN sourcedesk IN ('GOBUS_NY', 
                                                'GOBUS_UK', 
                                                'PETRDAES', 
                                                'PEETRAD') THEN clientid 
                            WHEN sourcedesk = 'AESLDN' 
                            AND    clientid != '' THEN clientid 
                            ELSE owner 
                     END                               AS aclient, 
                     clientid                          AS client, 
                     orderid                           AS gaotag, 
                     exchange                          AS exch, 
                     side                              AS side, 
                     securityid                        AS sym, 
                     cumulativequantity*executionprice AS value, 
                     cumulativequantity                AS qdone, 
                     fillid                            AS gaftag, 
                     executionexchangetimestamplcl     AS xstamp, 
                     executionprice                    AS lprice, 
                     executionexchangeid               AS xftag, 
                     serverid                          AS srvid, 
                     reasoncode                        AS rescode, 
                     rootfillid                        AS rgaftag, 
                     miccode                           AS miccode, 
                     mifidtradeflags                   AS mifidtradeflags, 
                     sourcedesk                        AS desk 
              FROM   fillsastextdatestring 
              WHERE  agoralocaltradedate = '190205' 
              AND    exchange = 'VEX' 
              AND    sourceservicename LIKE 'vexprod%' 
              AND    securityid LIKE '%.%' 
              UNION ALL 
              SELECT 
                     CASE 
                            WHEN sourcedesk IN ('GOBUS_NY', 
                                                'GOBUS_UK', 
                                                'PETRDAES', 
                                                'PEETRAD', 
                                                'VEXSVC', 
                                                'LLSOR') THEN clientid 
                            WHEN sourcedesk = 'AESLDN' 
                            AND    clientid != '' THEN clientid 
                            ELSE owner 
                     END                               AS aclient, 
                     clientid                          AS client, 
                     orderid                           AS gaotag, 
                     exchange                          AS exch, 
                     side                              AS side, 
                     securityid                        AS sym, 
                     cumulativequantity*executionprice AS value, 
                     cumulativequantity                AS qdone, 
                     fillid                            AS gaftag, 
                     executionexchangetimestamplcl     AS xstamp, 
                     executionprice                    AS lprice, 
                     executionexchangeid               AS xftag, 
                     serverid                          AS srvid, 
                     reasoncode                        AS rescode, 
                     rootfillid                        AS rgaftag, 
                     miccode                           AS miccode, 
                     mifidtradeflags                   AS mifidtradeflags, 
                     sourcedesk                        AS desk 
              FROM   fillsastextdatestring 
              WHERE  agoralocaltradedate = '190205' 
              AND    sourceservicename IN ('SORUK', 
                                           'PRODSorBkFlr', 
                                           'PRODFixBkFlr', 
                                           'PRODFixBkFlr1', 
                                           'PRODFixBkFlr2', 
                                           'PRODSmgBkFlr', 
                                           'ECGaia2Agora') 
              AND    owner NOT LIKE 'tree%' 
              AND    securityid LIKE '%.%' 
              UNION ALL 
              SELECT 
                     CASE 
                            WHEN sourcedesk IN ('GOBUS_NY', 
                                                'GOBUS_UK', 
                                                'PETRDAES', 
                                                'PEETRAD', 
                                                'VEXSVC', 
                                                'LLSOR') THEN clientid 
                            WHEN sourcedesk = 'AESLDN' 
                            AND    clientid != '' THEN clientid 
                            ELSE owner 
                     END                               AS aclient, 
                     clientid                          AS client, 
                     orderid                           AS gaotag, 
                     exchange                          AS exch, 
                     side                              AS side, 
                     securityid                        AS sym, 
                     cumulativequantity*executionprice AS value, 
                     cumulativequantity                AS qdone, 
                     fillid                            AS gaftag, 
                     executionexchangetimestamplcl     AS xstamp, 
                     executionprice                    AS lprice, 
                     executionexchangeid               AS xftag, 
                     serverid                          AS srvid, 
                     reasoncode                        AS rescode, 
                     rootfillid                        AS rgaftag, 
                     miccode                           AS miccode, 
                     mifidtradeflags                   AS mifidtradeflags, 
                     sourcedesk                        AS desk 
              FROM   fillsastextdatestring 
              WHERE  agoralocaltradedate = '190205' 
              AND    securityid LIKE '%.%' 
              AND    exchange = 'BENCHMKX' 
              AND    sourceservicename LIKE 'BENCHM_%' 
              UNION ALL 
              SELECT 
                     CASE 
                            WHEN sourcedesk IN ('GOBUS_NY', 
                                                'GOBUS_UK', 
                                                'PETRDAES', 
                                                'PEETRAD', 
                                                'VEXSVC', 
                                                'LLSOR') THEN clientid 
                            WHEN sourcedesk = 'AESLDN' 
                            AND    clientid != '' THEN clientid 
                            ELSE owner 
                     END                               AS aclient, 
                     clientid                          AS client, 
                     orderid                           AS gaotag, 
                     exchange                          AS exch, 
                     side                              AS side, 
                     securityid                        AS sym, 
                     cumulativequantity*executionprice AS value, 
                     cumulativequantity                AS qdone, 
                     fillid                            AS gaftag, 
                     executionexchangetimestamplcl     AS xstamp, 
                     executionprice                    AS lprice, 
                     executionexchangeid               AS xftag, 
                     serverid                          AS srvid, 
                     reasoncode                        AS rescode, 
                     rootfillid                        AS rgaftag, 
                     miccode                           AS miccode, 
                     mifidtradeflags                   AS mifidtradeflags, 
                     sourcedesk                        AS desk 
              FROM   fillsastextdatestring 
              WHERE  agoralocaltradedate = '190205' 
              AND    sourceservicename = 'ECGaia2Agora' 
              AND    owner NOT LIKE 'tree%' 
              AND    exchange IN ('BATS', 
                                  'CHIX') 
              AND    reasoncode = 'NT' 
              AND    side = 'B' 
              UNION ALL 
              SELECT 
                     CASE 
                            WHEN sourcedesk IN ('GOBUS_NY', 
                                                'GOBUS_UK', 
                                                'PETRDAES', 
                                                'PEETRAD', 
                                                'VEXSVC', 
                                                'LLSOR') THEN clientid 
                            WHEN sourcedesk = 'AESLDN' 
                            AND    clientid != '' THEN clientid 
                            ELSE owner 
                     END                               AS aclient, 
                     clientid                          AS client, 
                     orderid                           AS gaotag, 
                     exchange                          AS exch, 
                     side                              AS side, 
                     securityid                        AS sym, 
                     cumulativequantity*executionprice AS value, 
                     cumulativequantity                AS qdone, 
                     fillid                            AS gaftag, 
                     executionexchangetimestamplcl     AS xstamp, 
                     executionprice                    AS lprice, 
                     executionexchangeid               AS xftag, 
                     serverid                          AS srvid, 
                     reasoncode                        AS rescode, 
                     rootfillid                        AS rgaftag, 
                     miccode                           AS miccode, 
                     mifidtradeflags                   AS mifidtradeflags, 
                     sourcedesk                        AS desk 
              FROM   fillsastextdatestring 
              WHERE  agoralocaltradedate = '190205' 
              AND    sourceservicename LIKE 'tars_%' ) AS f, 
       ( 
              SELECT orderid             AS gaotag, 
                     serverid            AS srvid, 
                     securityidasentered AS enteredsym, 
                     securityid          AS sym, 
                     rootorderid         AS rgaotag, 
                     timeinforce         AS tif, 
                     settletype          AS settletype, 
                     sourceservicename   AS appl, 
                     account             AS acct 
              FROM   ordersastextdatestring 
              WHERE  agoralocaltradedate = '190205' 
              AND    destinationdesk IN ('BENCHX', 
                                         'VEXSVC', 
                                         'SORMKT', 
                                         'MAIA', 
                                         'LLSOR', 
                                         'TARSSVC')) AS o, 
where  f.gaotag = o.gaotag

上述查询失败,在 impala 中出现以下错误:

AnalysisException:第 4 行中的语法错误:undefined: where f.gaotag = o.gaotag ^ 遇到:WHERE Expected: DEFAULT, IDENTIFIER CAUSED BY: Exception: Syntax error

我浏览了以下 cloudera 链接: https://www.cloudera.com/documentation/enterprise/5-4-x/topics/impala_subqueries.html 但无法完成这项工作。

我是 Sybase IQ 和 Imapala 的新手。

【问题讨论】:

至少尝试以可读的方式格式化代码 @deb 。 . .简化!尝试在 Impala 中逐个、逐列、逐表地构建查询。 【参考方案1】:

你可以尝试用下面的查询替换最后一个查询并尝试运行它 -

SELECT f.exch 
FROM   (SELECT CASE 
                 WHEN sourcedesk IN ( 'GOBUS_NY', 'GOBUS_UK', 'PETRDAES', 
                                      'PEETRAD' ) 
                       THEN 
                 clientid 
                 WHEN sourcedesk = 'AESLDN' 
                      AND clientid != '' THEN clientid 
                 ELSE owner 
               END                                 AS aclient, 
               clientid                            AS client, 
               orderid                             AS gaotag, 
               exchange                            AS exch, 
               side                                AS side, 
               securityid                          AS sym, 
               cumulativequantity * executionprice AS value, 
               cumulativequantity                  AS qdone, 
               fillid                              AS gaftag, 
               executionexchangetimestamplcl       AS xstamp, 
               executionprice                      AS lprice, 
               executionexchangeid                 AS xftag, 
               serverid                            AS srvid, 
               reasoncode                          AS rescode, 
               rootfillid                          AS rgaftag, 
               miccode                             AS miccode, 
               mifidtradeflags                     AS mifidtradeflags, 
               sourcedesk                          AS desk 
        FROM   fillsastextdatestring 
        WHERE  agoralocaltradedate = '190205' 
               AND exchange = 'VEX' 
               AND sourceservicename LIKE 'vexprod%' 
               AND securityid LIKE '%.%' 
        UNION ALL 
        SELECT CASE 
                 WHEN sourcedesk IN ( 'GOBUS_NY', 'GOBUS_UK', 'PETRDAES', 
                                      'PEETRAD', 
                                      'VEXSVC', 'LLSOR' ) THEN clientid 
                 WHEN sourcedesk = 'AESLDN' 
                      AND clientid != '' THEN clientid 
                 ELSE owner 
               END                                 AS aclient, 
               clientid                            AS client, 
               orderid                             AS gaotag, 
               exchange                            AS exch, 
               side                                AS side, 
               securityid                          AS sym, 
               cumulativequantity * executionprice AS value, 
               cumulativequantity                  AS qdone, 
               fillid                              AS gaftag, 
               executionexchangetimestamplcl       AS xstamp, 
               executionprice                      AS lprice, 
               executionexchangeid                 AS xftag, 
               serverid                            AS srvid, 
               reasoncode                          AS rescode, 
               rootfillid                          AS rgaftag, 
               miccode                             AS miccode, 
               mifidtradeflags                     AS mifidtradeflags, 
               sourcedesk                          AS desk 
        FROM   fillsastextdatestring 
        WHERE  agoralocaltradedate = '190205' 
               AND sourceservicename IN ( 'SORUK', 'PRODSorBkFlr', 
                                          'PRODFixBkFlr', 
                                          'PRODFixBkFlr1', 
                                          'PRODFixBkFlr2', 'PRODSmgBkFlr', 
                                          'ECGaia2Agora' ) 
               AND owner NOT LIKE 'tree%' 
               AND securityid LIKE '%.%' 
        UNION ALL 
        SELECT CASE 
                 WHEN sourcedesk IN ( 'GOBUS_NY', 'GOBUS_UK', 'PETRDAES', 
                                      'PEETRAD', 
                                      'VEXSVC', 'LLSOR' ) THEN clientid 
                 WHEN sourcedesk = 'AESLDN' 
                      AND clientid != '' THEN clientid 
                 ELSE owner 
               END                                 AS aclient, 
               clientid                            AS client, 
               orderid                             AS gaotag, 
               exchange                            AS exch, 
               side                                AS side, 
               securityid                          AS sym, 
               cumulativequantity * executionprice AS value, 
               cumulativequantity                  AS qdone, 
               fillid                              AS gaftag, 
               executionexchangetimestamplcl       AS xstamp, 
               executionprice                      AS lprice, 
               executionexchangeid                 AS xftag, 
               serverid                            AS srvid, 
               reasoncode                          AS rescode, 
               rootfillid                          AS rgaftag, 
               miccode                             AS miccode, 
               mifidtradeflags                     AS mifidtradeflags, 
               sourcedesk                          AS desk 
        FROM   fillsastextdatestring 
        WHERE  agoralocaltradedate = '190205' 
               AND securityid LIKE '%.%' 
               AND exchange = 'BENCHMKX' 
               AND sourceservicename LIKE 'BENCHM_%' 
        UNION ALL 
        SELECT CASE 
                 WHEN sourcedesk IN ( 'GOBUS_NY', 'GOBUS_UK', 'PETRDAES', 
                                      'PEETRAD', 
                                      'VEXSVC', 'LLSOR' ) THEN clientid 
                 WHEN sourcedesk = 'AESLDN' 
                      AND clientid != '' THEN clientid 
                 ELSE owner 
               END                                 AS aclient, 
               clientid                            AS client, 
               orderid                             AS gaotag, 
               exchange                            AS exch, 
               side                                AS side, 
               securityid                          AS sym, 
               cumulativequantity * executionprice AS value, 
               cumulativequantity                  AS qdone, 
               fillid                              AS gaftag, 
               executionexchangetimestamplcl       AS xstamp, 
               executionprice                      AS lprice, 
               executionexchangeid                 AS xftag, 
               serverid                            AS srvid, 
               reasoncode                          AS rescode, 
               rootfillid                          AS rgaftag, 
               miccode                             AS miccode, 
               mifidtradeflags                     AS mifidtradeflags, 
               sourcedesk                          AS desk 
        FROM   fillsastextdatestring 
        WHERE  agoralocaltradedate = '190205' 
               AND sourceservicename = 'ECGaia2Agora' 
               AND owner NOT LIKE 'tree%' 
               AND exchange IN ( 'BATS', 'CHIX' ) 
               AND reasoncode = 'NT' 
               AND side = 'B' 
        UNION ALL 
        SELECT CASE 
                 WHEN sourcedesk IN ( 'GOBUS_NY', 'GOBUS_UK', 'PETRDAES', 
                                      'PEETRAD', 
                                      'VEXSVC', 'LLSOR' ) THEN clientid 
                 WHEN sourcedesk = 'AESLDN' 
                      AND clientid != '' THEN clientid 
                 ELSE owner 
               END                                 AS aclient, 
               clientid                            AS client, 
               orderid                             AS gaotag, 
               exchange                            AS exch, 
               side                                AS side, 
               securityid                          AS sym, 
               cumulativequantity * executionprice AS value, 
               cumulativequantity                  AS qdone, 
               fillid                              AS gaftag, 
               executionexchangetimestamplcl       AS xstamp, 
               executionprice                      AS lprice, 
               executionexchangeid                 AS xftag, 
               serverid                            AS srvid, 
               reasoncode                          AS rescode, 
               rootfillid                          AS rgaftag, 
               miccode                             AS miccode, 
               mifidtradeflags                     AS mifidtradeflags, 
               sourcedesk                          AS desk 
        FROM   fillsastextdatestring 
        WHERE  agoralocaltradedate = '190205' 
               AND sourceservicename LIKE 'tars_%') AS f 
       INNER JOIN (SELECT orderid             AS gaotag, 
                          serverid            AS srvid, 
                          securityidasentered AS enteredsym, 
                          securityid          AS sym, 
                          rootorderid         AS rgaotag, 
                          timeinforce         AS tif, 
                          settletype          AS settletype, 
                          sourceservicename   AS appl, 
                          account             AS acct 
                   FROM   ordersastextdatestring 
                   WHERE  agoralocaltradedate = '190205' 
                          AND destinationdesk IN ( 'BENCHX', 'VEXSVC', 'SORMKT', 
                                                   'MAIA' 
                                                   , 
                                                   'LLSOR', 'TARSSVC' )) o 
               ON f.gaotag = o.gaotag;

【讨论】:

您能否运行每个单独的查询并告诉我们哪个特定查询有问题.. 取 union all 的每个部分并尝试获取结果 问题似乎出在内部联接或 where 子句上,其余的查询在单独运行时运行良好。 问题是我添加的连接语法,你现在可以再检查一下吗

以上是关于将查询从 Sybase IQ 转换为 impala的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate + Sybase Iq

PreparedStatement 不适用于 Java 中的 Sybase IQ

Sybase IQ使用过程中注意事项

linux中执行crontab定时任务时无法连接sybase iq15.1数据库,shell脚本中的sybase iq的环境变量怎么设置

Oracle 到 Impala 查询转换

如何使用 unix shell 脚本将 impala 查询输出日志转换为变量?