在具有 DISTINCT 列的 where 子句中无法识别别名 RANK() 函数
The alias name RANK() function is not recognized in the where clause with DISTINCT columns
我有 2 个 table 列(客户、职位、产品、sales_cycle、call_count、cntry_cd、owner_cd、cr8)和我面临如下所述的一些挑战 请帮我解决这个问题
我的要求
我有 2 tables 测试。table1 和测试。table2
我需要通过 "test.table1" 执行 select 来从 "test.table2" 中插入值。但是我遇到了一个问题,即我在将数据加载到 "test.table2"
时得到了一些重复项
我在 table 中总共有 8 列,但是在加载时我需要在这些列的唯一值(客户、职位、产品)的条件下 "call_count" 列的最高排名,sales_cycle)
查询我试过的
select
distinct (customer, position, product ,sales_cycle),
rank () over (order by call_count desc) rnk,
cntry_cd,
owner_cd,
cr8
from test.table1
where rnk=1
我在上面的查询中遇到了一些挑战(我使用的数据库是 RedShift)
1.I 不能仅对几列进行区分
2.The 别名 "rnk" 在 where 子句
中无法识别
请帮我解决这个问题,谢谢
您不能在引入列别名的同一级别上使用它。您需要将查询包装在派生的 table 中。如果您使用 rank()
,如图所示的 distinct
也是无用的
select customer, position, product, sales_cycle,
cntry_cd, owner_cd, cr8
from (
select customer, position, product, sales_cycle,
cntry_cd, owner_cd, cr8,
rank () over (order by call_count desc) rnk
from test.table1
) t
where rnk=1;
派生的 table 不会增加处理时间的开销。在这种情况下,它只是允许您引用列别名的语法糖。
我有 2 个 table 列(客户、职位、产品、sales_cycle、call_count、cntry_cd、owner_cd、cr8)和我面临如下所述的一些挑战 请帮我解决这个问题
我的要求
我有 2 tables 测试。table1 和测试。table2
我需要通过 "test.table1" 执行 select 来从 "test.table2" 中插入值。但是我遇到了一个问题,即我在将数据加载到 "test.table2"
时得到了一些重复项我在 table 中总共有 8 列,但是在加载时我需要在这些列的唯一值(客户、职位、产品)的条件下 "call_count" 列的最高排名,sales_cycle)
查询我试过的
select
distinct (customer, position, product ,sales_cycle),
rank () over (order by call_count desc) rnk,
cntry_cd,
owner_cd,
cr8
from test.table1
where rnk=1
我在上面的查询中遇到了一些挑战(我使用的数据库是 RedShift)
1.I 不能仅对几列进行区分
2.The 别名 "rnk" 在 where 子句
中无法识别请帮我解决这个问题,谢谢
您不能在引入列别名的同一级别上使用它。您需要将查询包装在派生的 table 中。如果您使用 rank()
distinct
也是无用的
select customer, position, product, sales_cycle,
cntry_cd, owner_cd, cr8
from (
select customer, position, product, sales_cycle,
cntry_cd, owner_cd, cr8,
rank () over (order by call_count desc) rnk
from test.table1
) t
where rnk=1;
派生的 table 不会增加处理时间的开销。在这种情况下,它只是允许您引用列别名的语法糖。