sql cutomize列名称在不同的大小写和分组条件之后

sql cutomize列名称在不同的大小写和分组条件之后,sql,group-by,google-bigquery,Sql,Group By,Google Bigquery,我的第1列有不同的值(a、b、c)。我需要根据column2(1,2)和column3(true,false)的值计算该列中的每个值 最后,我需要有一个输出列,它的名称是不同组合的名称(numberofu a_和_1_和_true,numberofu a_和_1_和_false,numberofu a_和_2_和_true,numberofu b_和_2_和_true等) 我该怎么做呢?我曾考虑过使用countif,但它太长了。这里有一种方法可以做到这一点 select column1

我的第1列有不同的值(a、b、c)。我需要根据column2(1,2)和column3(true,false)的值计算该列中的每个值

最后,我需要有一个输出列,它的名称是不同组合的名称(numberofu a_和_1_和_true,numberofu a_和_1_和_false,numberofu a_和_2_和_true,numberofu b_和_2_和_true等)


我该怎么做呢?我曾考虑过使用countif,但它太长了。

这里有一种方法可以做到这一点

select column1
       ,count(case when column1='a' and column2=1 and column3=true then 1 end) 
       ,count(case when column1='a' and column2=2 and column3=true then 1 end) 
       ,count(case when column1='a' and column2=1 and column3=false then 1 end) 
       ,count(case when column1='a' and column2=2 and column3=false then 1 end)
       --Add similar conditions for column1=b and c
  from table 
group by column1

在BigQuery中,您将使用
countif()


添加您的样本和预期的输出数据。为什么要为每个组合创建新列?您已经可以使用group by了。@Samorix…样本数据和所需结果会有所帮助。
select name,
       countif(col1 = 'a' and col2 = 1 and col3) as a_1_true,
       countif(col1 = 'a' and col2 = 1 and not col3) as a_1_false,
       countif(col1 = 'a' and col2 = 2 and col3) as a_2_true,
       countif(col1 = 'a' and col2 = 2 and not col3) as a_2_false,
       countif(col1 = 'b' and col2 = 1 and col3) as b_1_true,
       countif(col1 = 'b' and col2 = 1 and not col3) as b_1_false,
       countif(col1 = 'b' and col2 = 2 and col3) as a_b_true,
       countif(col1 = 'b' and col2 = 2 and not col3) as b_2_false,
       countif(col1 = 'c' and col2 = 1 and col3) as c_1_true,
       countif(col1 = 'c' and col2 = 1 and not col3) as c_1_false,
       countif(col1 = 'c' and col2 = 2 and col3) as c_2_true,
       countif(col1 = 'c' and col2 = 2 and not col3) as c_2_false
from t
group by name;