将excel公式转换为PostgreSQL查询

cbjzeqam  于 2023-04-07  发布在  PostgreSQL
关注(0)|答案(1)|浏览(115)

我有这个Excel公式,其中包含值。
enter image description here

STUDENT NAME    START DATE  TEACHER NAME
X   01/01/2023  A
Y   17/02/2023  A
Z   20/03/2023  A
R   01/01/2023  B
S   17/02/2023  B
T   20/03/2023  B
DISPATCH DATE   TEACHER NAME
09/03/2023  A
26/01/2023  A
24/02/2023  A
09/03/2023  A
25/03/2023  A
24/03/2023  A
09/03/2023  B
24/03/2023  B

有人能转换这个公式吗?

=FILTER(A:A,C:C=G3,B:B<>"", B:B=MAX(FILTER(B:B,C:C=G3,B:B<>"",B:B<=F3)))

PostgreSQL中如何join获取某个时间范围内的学生姓名
我已经尝试使用常规连接,但结果仍然是一个双精度或错误。
正确的结果应该是

STUDENT NAME
Y
X
Y
Y
Z
Z
S
T
sg24os4d

sg24os4d1#

根据你分享的Excel公式
根据以下条件过滤A列中的值:

Column C should have a value equal to G3
Column B should not be empty/null
Column B should have a value less than or equal to F3
Column B should have the maximum value among the values in column B that meet the first three conditions

这是一个等价的SQL逻辑,

SELECT A
FROM postgres_table
WHERE C = 'G3' 
  AND B <> '' 
  AND B <= 'F3'
  AND B = (SELECT MAX(B) 
           FROM postgres_table
           WHERE C = 'G3' 
           AND B <> '' 
           AND B <= 'F3')

用实际的表名替换postgres_table,并让我知道这是否有效。

相关问题