简体   繁体   中英

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 .

I want to combine Team Spanish with Team B - Spanish , so it only shows one row with the sum of 7 people.

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

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 . 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

finally you have to pivot the previous table. in the rows put TEAM and LANGUAGE. Don't pu anything in column SUM of PEOPLE. 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:

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 

Output: 在此处输入图像描述

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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