简体   繁体   English

在Teradata中达到QUALIFY ROW_NUMBER

[英]QUALIFY ROW_NUMBER in teradata

below is the oracle SQL and i want to change it in Teradata format. 下面是oracle SQL,我想将其更改为Teradata格式。

SELECT branch_code,
    branch_no,
    c_no,
    cd_type
FROM (
    SELECT branch_code,
        branch_no,
        c_no,
        cd_type,
        * * RANK() OVER (
            PARTITION BY c_no ORDER BY cd_type
            ) RANK * *
    FROM (
        SELECT branch_code,
            branch_no,
            c_no,
            MIN(cd_type) cd_type
        FROM EMPLOYEE
        WHERE S_CODE = 'C'
            AND (branch_no) NOT IN (
                SELECT branch_code
                FROM DEPARTMENT
                WHERE branch_code = 'ABC'
                )
        )
    )
WHERE RANK = 1

I have used QUALIFY for RANK as below . 我已经将QUALIFY用于RANK,如下所示。

SELECT branch_code,
    branch_no,
    c_no,
    cd_type
FROM (
    SELECT branch_code,
        branch_no,
        c_no,
        cd_type,
        * * QUALIFY ROW_NUMBER() OVER (
            PARTITION BY c_no ORDER BY cd_type
            ) * * RANK
    FROM (
        SELECT branch_code,
            branch_no,
            c_no,
            MIN(cd_type) cd_type
        FROM EMPLOYEE
        WHERE S_CODE = 'C'
            AND (branch_no) NOT IN (
                SELECT branch_code
                FROM DEPARTMENT
                WHERE branch_code = 'ABC'
                )
        )
    )
WHERE RANK = 1

But getting error that "Expected something between , and QUALIFY. 但是却收到错误消息“期望在和合格之间。

Can we put QUALIFY in select statement ? 我们可以在选择语句中放入QUALIFY吗?

Normally, analytic values like RANK are calculated second to last, after joining and filtering and GROUP BY and HAVING . 通常,在加入和过滤以及GROUP BYHAVING之后,倒数第二个计算值,例如RANK The only thing done after analytic values is ORDER BY . 解析值之后唯一要做的就是ORDER BY That's why in Oracle you need to put the RANK into an inner query and then test its value in an outer query. 这就是为什么在Oracle中,您需要将RANK放入内部查询中,然后在外部查询中测试其值。 In Teradata, QUALIFY is executed after the analytic functions and before the ORDER BY , meaning you don't need the outer query to test the RANK value. 在Teradata中, QUALIFY在分析函数之后和ORDER BY之前执行,这意味着您不需要外部查询即可测试RANK值。

I don't have access to Teradata today, so this query isn't tested but I like to think it's close: 我今天没有访问Teradata的权限,因此该查询未经测试,但我想认为它已经接近了:

SELECT branch_code,
    branch_no,
    c_no,
    cd_type
FROM (
    SELECT branch_code,
        branch_no,
        c_no,
        MIN(cd_type) cd_type
    FROM EMPLOYEE
    WHERE S_CODE = 'C'
        AND (branch_no) NOT IN (
            SELECT branch_code
            FROM DEPARTMENT
            WHERE branch_code = 'ABC'
        )
)
QUALIFY ROW_NUMBER() OVER (PARTITION BY c_no ORDER BY cd_type) = 1

You probably simplified your existing query as this is no valid Oracle SQL (there's no GROUP BY in the Inline View). 您可能简化了现有查询,因为这不是有效的Oracle SQL(内联视图中没有GROUP BY)。 Plus you compare branch_no to branch_code in the NOT IN. 另外,您可以在NOT IN branch_nobranch_code进行比较。

Otherwise Ed Gibbs' answer can be further simplified to: 否则,Ed Gibbs的答案可以进一步简化为:

SELECT branch_code,
    branch_no,
    c_no,
    MIN(cd_type) cd_type
FROM EMPLOYEE
WHERE S_CODE = 'C'
    AND (branch_no) NOT IN (
        SELECT branch_no
        FROM DEPARTMENT
        WHERE branch_code = 'ABC'
        )
GROUP BY branch_code,
    branch_no,
    c_no
QUALIFY
   RANK()
   OVER (PARTITION BY c_no 
         ORDER BY MIN(cd_type) = 1

If branch_no is defined as NULLable you should also rewrite NOT IN to NOT EXISTS 如果branch_no被定义为NULLable,则还应该将NOT IN重写为NOT EXISTS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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