简体   繁体   中英

SQL Case When Query Count Else Statement Values

In my query I have multiple Case When clauses where a unique string of numbers from one of the tables will dictate a unique string/sentence value in the output. My issue currently is that my Case When outputs are working with the total counts in the output, but since there are multiple unique strings that aren't in the 1st table that exist in the 2nd that should sum to a single total in the else statement, each count is on its own row.

I have tried doing a ROLLUP on the GROUP BY clause at the end of my query, but I don't get the expected output I'm looking for.

Here is what I'm working with right now:

SELECT 
  F.Identifier, F.Videogame, F.Developer,
CASE WHEN S.String='1581' THEN 'Made by billy'
     WHEN S.String='1903' THEN 'Made by bob'
     WHEN S.String='5849' THEN 'Made by lilly'
     ELSE 'worked on by someone else' END AS Final_Name,
     COUNT(distinct S.User_ID) as Count
FROM 
  table1 as F
JOIN 
  table2 as S
ON
  F.Identifier=S.Identifier
GROUP BY
  F.Identifier, F.Videogame, F.Developer, S.String

Here is the output I'm getting currently:

abcd | red dead | company1 | worked on by someone else | 1
abcd | red dead | company1 | Made by billy             | 1
defg | halo 3   | company2 | Made by bob               | 1
defg | halo 3   | company2 | worked on by someone else | 1
defg | halo 3   | company2 | worked on by someone else | 1
hijk | fortnite | company3 | Made by lilly             | 1

Here is the output I'm looking to achieve:

abcd | red dead | company1 | worked on by someone else | 1
abcd | red dead | company1 | Made by billy             | 1
defg | halo 3   | company2 | Made by bob               | 1
defg | halo 3   | company2 | worked on by someone else | 2
hijk | fortnite | company3 | Made by lilly             | 1

Below is for BigQuery Standard SQL

#standardSQL
SELECT F.Identifier, F.Videogame, F.Developer,
CASE WHEN S.String='1581' THEN 'Made by billy'
     WHEN S.String='1903' THEN 'Made by bob'
     WHEN S.String='5849' THEN 'Made by lilly'
     ELSE 'worked on by someone else' END AS Final_Name,
     COUNT(DISTINCT S.User_ID) AS COUNT
FROM `project.dataset.table1` AS F
JOIN `project.dataset.table2` AS S
ON F.Identifier=S.Identifier
GROUP BY F.Identifier, F.Videogame, F.Developer, Final_Name 

As you can see the only fix is in last line - instead of S.String you needed to use Final_Name

So, if to apply above to sampled data in your question - result is

Row Identifier  Videogame   Developer   Final_Name                  Count    
1   abcd        red dead    company1    worked on by someone else   1    
2   abcd        red dead    company1    Made by billy               1    
3   defg        halo 3      company2    Made by bob                 1    
4   defg        halo 3      company2    worked on by someone else   2    
5   hijk        fortnite    company3    Made by lilly               1    

Here's what you need.

SELECT 
  F.Identifier, F.Videogame, F.Developer,
CASE WHEN F.String='1581' THEN 'Made by billy'
     WHEN F.String='1903' THEN 'Made by bob'
     WHEN F.String='5849' THEN 'Made by lilly'
     ELSE 'worked on by someone else' END AS Final_Name,
     S.cnt as Count
FROM 
  table1 as F
JOIN 
  (select a.Identifier, b.Developer, count(1) cnt from table2  a
     join table1 b on b.Identifier = a.Identifier
    Group by String, b.Developer) as S
    ON
      F.Identifier=S.Identifier and S.Developer = F.Developer

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