0

我正在尝试在 Netezza Aginity SQL 中重新创建一些 excel 公式,以便可以在数据库级别完成处理,但会遇到计数问题。

公式大致为:

If( Countifs( policycolumn, policy, matchcolumn, "Match", codecolumn, code) >0, "true", "false")

因此,如果有任何行匹配策略和“匹配”和代码,它将大于 0 并且为真。我只是在策略部分苦苦挣扎,因为要计算的策略是该行中的策略。

人们有什么方法可以模仿sql中的countifs吗?

编辑:举一些例子,我的数据集看起来像:(对不起,我的格式不好):

policycolumn  |  matchcolumn  |  codecolumn

12345         | match         | c

12345         | no match      | d

9876          | match         | c

9876          | no match      | c

我想要一个额外的专栏来显示

policycolumn  |  matchcolumn  |  codecolumn | yesno

12345         | match         | c           | yes

12345         | no match      | d           | no

9876          | match         | c           | yes

9876          | match         | d           | no

第 1 行是肯定的,因为它计算 12345 出现的次数,带有“匹配”和“c”。此行匹配,因此将 >0 并触发 IF 规则为“是”

ROw 2 不会是肯定的,因为尽管它的保单编号为 12345,但它是“不匹配”和“d”。

第 3 行是肯定的,因为行策略编号 9876 是“匹配”和“c”。

第 4 行不是“是”,因为该行策略编号 9876 是“不匹配”。

必须满足所有条件(Match Column = match 和 Codecolumn = c)才能使该行为真,并将新列设置为“yes”。

4

1 回答 1

0

该 SQL 应该按照您的要求执行。

select policycolumn, matchcolumn, codecolumn, 
case when matchcolumn = 'match' and codecolumn = 'c' then 'yes' else 'no' end yesno
from <your table>

测试结果

with test_data_set as(
  select 12345 policycolumn, 'match' matchcolumn, 'c' codecolumn union all
  select 12345, 'no match', 'd' union all
  select 9876, 'match', 'c' union all
  select 9876, 'match', 'd')
select policycolumn, matchcolumn, codecolumn, 
case when matchcolumn = 'match' and codecolumn = 'c' then 'yes' else 'no' end yesno
from test_data_set;

*returns*
policycolumn matchcolumn codecolumn yesno
12345   match   c   yes
12345   no match    d   no
9876    match   c   yes
9876    match   d   no

请让我知道它是否有帮助

于 2019-06-03T23:08:36.417 回答