Redshift:不同值在分区上的总和

Redshift :Sum of distinct values over partition by

如何在 Redshift 的学生 table 中单独添加学生 ID 的不同标记?

在 oracle 中这是可行的,

SELECT SUM(distinct marks) OVER (PARTITION BY studentid) FROM student;

但这在 Redshift 中不起作用!我想解决这个问题而不用单独加入 SELECT 语句。

当 window 函数不可用时,您必须使用 JOIN 或相关查询。

关联查询(只选)

SELECT t.* ,
       (SELECT sum(distinct marks) FROM student s 
        WHERE s.studentid = t.studentid) as stud_sum
FROM student t

Window 函数在 Redshift 中很有名且有影响力。

由于 Redshift DB 是 Postgres 的一个分支,因此 Postgres 8.x 支持的大多数 Windows 函数都可以灵活使用。

为了给予 SQL,你可以写点东西

SELECT studentid, SUM(distinct marks) 
  OVER (PARTITION BY studentid) FROM student;

SQL 应该在 Redshift 中工作。这是所有 Window 函数支持的 documentation