Google bigquery 如何在BigQuerySQL中使用混合(度量和求和(度量))条件?
我有一张这样的桌子: 首先,我想创建两个表,它们按1:Google bigquery 如何在BigQuerySQL中使用混合(度量和求和(度量))条件?,google-bigquery,Google Bigquery,我有一张这样的桌子: 首先,我想创建两个表,它们按1:Activity和2:`Model分组,如下所示: 然后: 现在我想从这两个表中创建第三个表,共有4列: 表1(活动表)中的分数>=0.3*总和(分数)=>这里的分数是总分 表2(模型表)中的分数>=0.3*总和(分数)=>这里的分数是总分 表1(活动表)中的分数>=0.3*总和(分数)=>这里的分数是总分 表2(模型表)中的分数>=0.3*总和(分数)=>这里的分数是总分 对前两个表的查询我很清楚: 从表格中选择活动、总和(分数)、总和
Activity
和2:`Model分组,如下所示:
然后:
现在我想从这两个表中创建第三个表,共有4列:
Date Activity Model Score Points
01-Dec-20 Football T2SSHEDF 20 0
02-Dec-20 Cricket R45SFDVG 60 10
03-Dec-20 Badminton W348DVG 80 2
04-Dec-20 BasketBall GH43ERH 90 40
05-Dec-20 Cricket T2SSHEDF 10 10
06-Dec-20 Football R45SFDVG 40 10
07-Dec-20 BasketBall W348DVG 32 30
08-Dec-20 Badminton GH43ERH 28 20
下面是BigQuery标准SQL
#standardSQL
with `project.dataset.table` as (
select '01-Dec-20' Date, 'Football' Activity, 'T2SSHEDF' Model, 20 Score, 0 Points union all
select '02-Dec-20', 'Cricket', 'R45SFDVG', 60, 10 union all
select '03-Dec-20', 'Badminton', 'W348DVG', 80, 2 union all
select '04-Dec-20', 'BasketBall', 'GH43ERH', 90, 40 union all
select '05-Dec-20', 'Cricket', 'T2SSHEDF', 10, 10 union all
select '06-Dec-20', 'Football', 'R45SFDVG', 40, 10 union all
select '07-Dec-20', 'BasketBall', 'W348DVG', 32, 30 union all
select '08-Dec-20', 'Badminton', 'GH43ERH', 28, 20
), table1 as (
select Activity, sum(Score) Score, sum(Points) Points
from `project.dataset.table`
group by Activity
), table2 as (
select Model, sum(Score) Score, sum(Points) Points
from `project.dataset.table`
group by Model
)
select Dimention, sum(Score) as Score, sum(Points) as Points
from (
select 'Activity' as Dimention, Score, Points, Score >= 0.3 * sum(Score) over() as qualified
from table1 union all
select 'Row Labels', Score, Points, Score >= 0.3 * sum(Score) over()
from table2
)
where qualified
group by Dimention
最终结果/输出
下面是BigQuery标准SQL
#standardSQL
with `project.dataset.table` as (
select '01-Dec-20' Date, 'Football' Activity, 'T2SSHEDF' Model, 20 Score, 0 Points union all
select '02-Dec-20', 'Cricket', 'R45SFDVG', 60, 10 union all
select '03-Dec-20', 'Badminton', 'W348DVG', 80, 2 union all
select '04-Dec-20', 'BasketBall', 'GH43ERH', 90, 40 union all
select '05-Dec-20', 'Cricket', 'T2SSHEDF', 10, 10 union all
select '06-Dec-20', 'Football', 'R45SFDVG', 40, 10 union all
select '07-Dec-20', 'BasketBall', 'W348DVG', 32, 30 union all
select '08-Dec-20', 'Badminton', 'GH43ERH', 28, 20
), table1 as (
select Activity, sum(Score) Score, sum(Points) Points
from `project.dataset.table`
group by Activity
), table2 as (
select Model, sum(Score) Score, sum(Points) Points
from `project.dataset.table`
group by Model
)
select Dimention, sum(Score) as Score, sum(Points) as Points
from (
select 'Activity' as Dimention, Score, Points, Score >= 0.3 * sum(Score) over() as qualified
from table1 union all
select 'Row Labels', Score, Points, Score >= 0.3 * sum(Score) over()
from table2
)
where qualified
group by Dimention
最终结果/输出
您可能希望将示例数据包含为文本,这将使复制和设置演示更加容易。另外,我建议显示您的实际预期输出,而不是单独的描述。您好,谢谢您的建议,我已经相应地更新了问题!您可能希望将示例数据包含为文本,这将使复制和设置演示更加容易。另外,我建议显示您的实际预期输出,而不是单独的描述。您好,谢谢您的建议,我已经相应地更新了问题!我只建造了它,直到一群人为了理解而建造。我只建造了它,直到一群人为了理解而建造