sql Teradata的DENSE_RANK

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql Teradata的DENSE_RANK相关的知识,希望对你有一定的参考价值。

-- Via http://forums.teradata.com/forum/database/dense-rank

SELECT
    department_number,
    /*** Syntax not implemented in Teradata ***/
    DENSE_RANK() OVER (PARTITION BY department_number ORDER BY salary_amount),
    last_name,
    salary_amount
FROM employee;

-- Same result using Teradata SQL:
SELECT
    e.department_number,
    dt.rnk AS "DENSE_RANK",
    e.last_name,
    e.salary_amount
FROM cs_views.employee e
JOIN (
    SELECT
        salary_amount,
        RANK() OVER (ORDER BY salary_amount) AS rnk
FROM cs_views.employee GROUP BY 1
) dt
    ON e.salary_amount = dt.salary_amount
ORDER BY rnk, e.salary_amount;

--Probably more efficient:
SELECT
    salary_amount,
    rnk,
    SUM(x) OVER (ORDER BY salary_amount, x DESC ROWS UNBOUNDED PRECEDING) AS "DENSE_RANK"
FROM (
    SELECT
        salary_amount,
        RANK() OVER (ORDER BY salary_amount) AS rnk,
        CASE
            WHEN salary_amount
                = MIN(salary_amount) OVER (ORDER BY salary_amount ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)
            THEN 0
            ELSE 1
        END AS x
    FROM employee
) dt;

SELECT
    salary_amount,
    SUM(x) OVER (ORDER BY salary_amount, x DESC ROWS UNBOUNDED PRECEDING) AS "DENSE_RANK"
FROM (
    SELECT
        salary_amount,
        CASE WHEN ROW_NUMBER() OVER (PARTITION BY salary_amount ORDER BY salary_amount) = 1
            THEN 1
            ELSE 0
        END AS x
    FROM employee
) dt;

以上是关于sql Teradata的DENSE_RANK的主要内容,如果未能解决你的问题,请参考以下文章

SQL/Teradata:如何返回特定值及其前面的行?

sql teradata 过滤日期 - 数据库版本 Teradata 15.10.06.02 和提供程序版本 Teradata.Net 15.11.0.0

在 teradata 的 sql 语句中使用字符串立即执行

如何在(Teradata)仅使用 ANSI SQL 时实现重置?

sql Teradata Sql Scratchpad - 查看Raw Data.sql

SQL - 一起使用dense_rank和group by