Google BigQuery CASE function

后端 未结 2 448
南笙
南笙 2020-12-09 06:11

I\'m trying to run a query like what\'s answered here, SQL Selecting multiple sums?

SELECT  SUM(CASE WHEN order_date >= \'01/01/09\' THEN quantity ELSE 0          


        
相关标签:
2条回答
  • 2020-12-09 06:35

    2013 update: BigQuery supports CASE:

    SELECT CASE WHEN x=1 THEN 'one' WHEN x=2 THEN 'two' ELSE 'more' END 
    FROM (SELECT 1 AS x) 
    
    'one'
    
    0 讨论(0)
  • 2020-12-09 06:44

    The way to do this in BigQuery is to use the if(test,then,else) function. For example: SELECT sum(if (revision_id > 10, num_characters, 0)) FROM [publicdata:samples.wikipedia] or similar to your second query:

    SELECT if (revision_id == 1, 'one', (if (revision_id == 2, 'two', 'more'))) FROM [publicdata:samples.wikipedia] limit 100

    0 讨论(0)
提交回复
热议问题