如何在查询中完成多个连接?

polhcujo  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(388)

我想在下面的查询中完成一个额外的内部联接,但是在创建临时表和内部联接之后遇到语法错误。

WITH myPos
AS (
    SELECT row_number() OVER (
            PARTITION BY pr_emp_id ORDER BY db_last_upd
            ) AS RowNum
        ,pos.*
    FROM master.s_postn pos
    )

SELECT act.row_id
    ,People.SalesID
FROM master.s_evt_act act
INNER JOIN (
    SELECT myPos.RowNum AS update_count
        ,myPos.db_last_upd
        ,myPos.bu_id
        ,myPos.ou_id
        ,myPos.Name
        ,myPos.pr_emp_id
        ,regexp_extract(myPos.Name, '(\\d+)', 1) AS SalesID
    FROM myPos
    INNER JOIN (
        SELECT max(rowNum) maxRowNum
            ,pr_emp_id
        FROM myPos
        GROUP BY pr_emp_id
        ) AS maxPos ON myPos.pr_emp_id = maxPos.pr_emp_id
        AND myPos.rowNum = maxPos.maxRowNum
    WHERE lower(myPos.name) LIKE '% specifictitle %'
    ) AS People ON People.pr_emp_id = act.owner_per_id

使用以下查询中捕获的其他数据:

SELECT sr_num
        ,owner_per_id
        ,x_cs_mgr_txt
    FROM mastertable.s_srv_req AS req
    WHERE req.x_cs_mgr_txt IS NOT NULL
        AND req.sr_stat_id <> 'Closed'

此附加联接将位于req.owner\u per\u id=act.owner\u per\u id上
我做了很多尝试和错误,我想我需要一点帮助。
谢谢!

am46iovg

am46iovg1#

很难说什么比试试这个:

WITH myPos
AS (
    SELECT row_number() OVER (
            PARTITION BY pr_emp_id ORDER BY db_last_upd
            ) AS RowNum
        ,pos.*
    FROM master.s_postn pos
    )

SELECT act.row_id
    ,People.SalesID
FROM master.s_evt_act act
INNER JOIN (
    SELECT myPos.RowNum AS update_count
        ,myPos.db_last_upd
        ,myPos.bu_id
        ,myPos.ou_id
        ,myPos.Name
        ,myPos.pr_emp_id
        ,regexp_extract(myPos.Name, '(\\d+)', 1) AS SalesID
    FROM myPos
    INNER JOIN (
        SELECT max(rowNum) maxRowNum
            ,pr_emp_id
        FROM myPos
        GROUP BY pr_emp_id
        ) AS maxPos ON myPos.pr_emp_id = maxPos.pr_emp_id
        AND myPos.rowNum = maxPos.maxRowNum
    WHERE lower(myPos.name) LIKE '% specifictitle %'
    ) AS People ON People.pr_emp_id = act.owner_per_id
    INNER JOIN (
    SELECT sr_num
        ,owner_per_id
        ,x_cs_mgr_txt
    FROM mastertable.s_srv_req AS req
    WHERE req.x_cs_mgr_txt IS NOT NULL
        AND req.sr_stat_id <> 'Closed'
    ) AS req  ON req.owner_per_id = act.owner_per_id

相关问题