[英]Filter by a value in two columns
My table looks like this我的桌子看起来像这样
Team![]() |
Language![]() |
People![]() |
---|---|---|
Team A ![]() |
English![]() |
3 ![]() |
Team B ![]() |
English![]() |
4 ![]() |
Team B ![]() |
Spanish![]() |
3 ![]() |
Team Spanish![]() |
Spanish![]() |
4 ![]() |
Team C![]() |
Portuguese![]() |
4 ![]() |
If you notice, Team B
handles English and Spanish and there's also a Team Spanish
.如果您注意到,
Team B
处理英语和西班牙语,还有一个Team Spanish
。
I want to combine Team Spanish
with Team B - Spanish
, so it only shows one row with the sum of 7 people.我想将
Team Spanish
与Team B - Spanish
结合起来,所以它只显示一行,总和为 7 人。
My expected result:我的预期结果:
Team![]() |
Language![]() |
People![]() |
---|---|---|
Team Spanish![]() |
Spanish![]() |
7 ![]() |
My workaround is to create an extra column, combine the Team
column with the Language
column and then sum the results我的解决方法是创建一个额外的列,将
Team
列与Language
列结合起来,然后对结果求和
SUMIF(people, aux_col IN ('team B - Spanish', 'Team Spanish - Spanish')) AS new_people_count
Never mind the last code, it's just pseudocode别管最后的代码,它只是伪代码
i made my calculation using google sheet.我使用谷歌表格进行了计算。 I think you must complete your goal in two steps: First step Create a table like your original table.
我认为你必须分两步完成你的目标: 第一步 创建一个像你原来的表一样的表。 This is the original table in google sheet Original Table .
这是 google sheet Original Table中的原始表格。 In the new table write the headers and the under the TEAM column write this formula =if (B3<>"Spanish";A3;"Team Spanish") copy this formula in rows from 4 to 7. Under the column LANGUAGE and PEOPLE copy the exact value of the original table example for LANGUAGE column write =B3 and copy from this formula in rows from 4 to 7. example for PEOPLE column write =C3 and copy from this formula in rows from 4 to 7. At the end you will have this table: Table step 1
在新表中写入标题,并在 TEAM 列下写入此公式 =if (B3<>"Spanish";A3;"Team Spanish") 将此公式复制到第 4 到 7 行中。在 LANGUAGE 和 PEOPLE 列下复制LANGUAGE 列的原始表示例的确切值写入 =B3 并从此公式中复制第 4 到 7 行。PEOPLE 列的示例写入 =C3 并从此公式中复制第 4 到 7 行。最后,您将有这个表:表步骤 1
finally you have to pivot the previous table.最后你必须上表 pivot。 in the rows put TEAM and LANGUAGE.
在行中放置 TEAM 和 LANGUAGE。 Don't pu anything in column SUM of PEOPLE.
不要在 PEOPLE 的 SUM 列中添加任何内容。 And here you have your final table Final Table
在这里你有你的决赛桌决赛桌
Let me know if that is what you wanted to do.让我知道这是否是您想要做的。 bye
再见
Using your query which has a condition changes all Team B-Spanish to Team Spanish I also added the count function that added (sum()) of people and produced the Expected output:使用具有条件的查询将所有 Team B-Spanish 更改为 Team Spanish 我还添加了计数 function,该计数添加了 (sum()) 人员并产生了预期的 output:
WITH Sample AS
(SELECT 'Team A' as Team, "English" as Language, 3 as People UNION ALL
SELECT 'Team B', "English", 4 UNION ALL
SELECT 'Team B', "Spanish", 3 UNION ALL
SELECT 'Team Spanish', "Spanish", 4 UNION ALL
SELECT 'Team C', "Portuguese", 4)
SELECT Language, CASE
WHEN CONCAT(Team, '-', Language) = 'Team B-Spanish' THEN 'Team Spanish'
ELSE Team
END AS Team,
SUM (People) as People
From Sample where Language = "Spanish" GROUP BY Team, Language
I was overcomplicating my idea... This code solves the issue我的想法过于复杂了......这段代码解决了这个问题
CASE
WHEN CONCAT(team, '-', language) = 'Team B-Spanish' THEN 'Team Spanish'
ELSE team
END AS team,
in SQL you can run a query like this:在 SQL 中,您可以运行如下查询:
select case when Language="Spanish" then "Team Spanish" else Team end as Team, Language, sum(People) from myTable group by Team, Language select case when Language="Spanish" then "Team Spanish" else Team end as Team, Language, sum(People) from myTable group by Team, Language
Let me know if this can be a solution for you.让我知道这是否可以成为您的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.