简体   繁体   English

计算红移中不同的多列

[英]Count distinct multiple columns in redshift

I am trying to count rows which have a distinct combination of 2 columns in Amazon redshift.我正在尝试计算在 Amazon redshift 中具有 2 列不同组合的行。 The query I am using is -我正在使用的查询是 -

select count(distinct col1, col2)
from schemaname.tablename
where some filters

It is throwing me this error -它向我抛出了这个错误 -

Amazon Invalid operation: function count(character varying, bigint) does not exist`亚马逊无效操作:函数计数(字符变化,bigint)不存在`

I tried casting bigint to char but it didn't work.我尝试将bigintchar但没有用。

you can use sub-query and count您可以使用子查询和计数

select count(*) from (
  select distinct col1, col2 
 from schemaname.tablename
  where some filter
) as t

A little late to the party but anyway: you can also try to concatenate columns using||聚会有点晚,但无论如何:您也可以尝试使用||连接列operator . 运营商 It might be inefficient so I wouldn't use it in prod code, but for ad-hoc analysis should be fine.可能效率低下,所以我不会在生产代码中使用它,但对于临时分析应该没问题。

select count(distinct col1 || '_' || col2)
from schemaname.tablename
where some filters

Note separator choice might matter, ie both 'foo' || '_' || 'bar_baz'注意分隔符的选择可能很重要,即'foo' || '_' || 'bar_baz' 'foo' || '_' || 'bar_baz' 'foo' || '_' || 'bar_baz' and 'foo_bar' || '_' || 'baz' 'foo' || '_' || 'bar_baz''foo_bar' || '_' || 'baz' 'foo_bar' || '_' || 'baz' 'foo_bar' || '_' || 'baz' yield 'foo_bar_baz' and are thus equal. 'foo_bar' || '_' || 'baz'产生'foo_bar_baz' ,因此是相等的。 In some cases this might be concern, in some it's so insignificant you can skip separator completely.在某些情况下,这可能是一个问题,在某些情况下,它是如此微不足道,您可以完全跳过分隔符。

You can use您可以使用

select col1,col2,count(*) from schemaname.tablename
where -- your filter
group by col1,col2

If you are just trying to do count(distinct) then Zaynul's answer is correct.如果您只是想count(distinct)那么 Zaynul 的答案是正确的。 If you want other aggregations as well, here is another method:如果您还需要其他聚合,这是另一种方法:

select . . ., 
       sum(case when seqnum = 1 then 1 else 0 end) as col1_col2_unique_count
from (select t.*,
             row_number() over (partition by col1, col2 order by col1) as seqnum
      from schemaname.tablename t
      where some filters
     ) c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM