Create a view with a total count of counts for a fixed set of values

I want to create a BQ View on this table:

tybae
id |
s1 value | 21
s2 | 31
s3 | 71

View should count each of the above lines (id) in each of the 10 fixed milestones when the value is <= milestoneValue

The resulting ten millionth row view for mytable would be:

milestoneValue | Number
100 | 3
90 | 3 (all s1 s2 s3)
80 | 3
70 | 2 (s1, s2)
60 ..
30 | 1
20 | 1
10 | 1

I have not found a suitable function to calculate this. I could add 10 binary flags as columns in the original data in the table, which I could SUM, but I see no way to convert that to 10 digits as a string. I tried:

SELECT  id, value,   
IF(value <= 10 , 1, 0) as M10,   
IF(value <= 20 , 1, 0) as M20,    
 ...   
IF(value <= 90 , 1, 0) as M90,   
IF(value <= 100 , 1, 0) as M100    
FROM mytable ;  

      

Help rate, Thanks

+3


source to share


1 answer


SELECT bucket, SUM(word_count>bucket)
FROM [publicdata:samples.shakespeare] a
CROSS JOIN (
  SELECT bucket FROM (SELECT 10 bucket), (SELECT 20 bucket), (SELECT 30 bucket), (SELECT 40 bucket)
) b
GROUP BY bucket

      



results

+1


source







All Articles