简体   繁体   English

在SQL Google BigQuery中计算CASE

[英]Counting CASE in SQL Google BigQuery

SELECT
SUM(CASE WHEN TipPercentage < 0 THEN 1 ELSE 0 END) AS 'No Tip'
SUM(CASE WHEN TipPercentage BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS 'Less but still a Tip'
SUM(CASE WHEN TipPercentage BETWEEN 5 AND 10 THEN 1 ELSE 0 END) AS 'Decent Tip'
SUM(CASE WHEN TipPercentage > 10 THEN 1 ELSE 0 END) AS 'Good Tip'
SUM(ELSE ) AS 'Something different'
END AS TipRange,
TipPercentage,
Tipbin)
FROM
(SELECT
case when tip_amount=0 then 'No Tip'
when (tip_amount > 0 and tip_amount <=5) then '0-5'
when (tip_amount > 5 and tip_amount <=10) then '5-10'
when (tip_amount > 10 and tip_amount <=20) then '10-20'
when tip_amount > 20 then '> 20'
else 'other'
end as Tipbin,
SUM(tip_amount) as Tips,
ROUND(avg((tip_amount)/(total_amount-tip_amount))*100,3) as TipPercentage
FROM `bigquery-public-data.new_york.tlc_yellow_trips_2015`
WHERE trip_distance >0
AND fare_amount/trip_distance BETWEEN 2 AND 10
AND dropoff_datetime > pickup_datetime
group by 1,2,3,tip_amount,tipbin)

I was trying to get data from Google Bigquery where the sum of each 'No Tip', 'Less but still a Tip', 'Decent Tip', 'Good Tip' and 'Something Different' would be returned based on the counts of each. 我正在尝试从Google Bigquery获取数据,其中将基于每个计数返回“无提示”,“少但仍是提示”,“体面提示”,“良好提示”和“某些不同”的总和。 However, I got a syntax error saying that the string 'No Tip' was unexpected. 但是,我收到一个语法错误,说字符串'No Tip'是意外的。

Could someone guide me on this? 有人可以指导我吗?

Thank you! 谢谢!

Edit: 编辑:

The error code I got using Standard SQL was 我使用标准SQL收到的错误代码是

Error: Syntax error: Unexpected string literal 'No Tip' at [2:55]

When I tried running it with Legacy SQL I got this: 当我尝试使用旧版SQL运行它时,我得到了:

Error: Encountered " "AS" "AS "" at line 2, column 52. Was expecting: <EOF>

Based on your description, you seem to essentially want your subquery. 根据您的描述,您似乎本质上想要您的子查询。 Here it is cleaned up a bit with syntax errors fixed: 在这里,它已修复一些语法错误:

SELECT (case when tip_amount = 0 then 'No Tip'
             when tip_amount > 0 and tip_amount <= 5 then '0-5'
             when tip_amount > 5 and tip_amount <= 10 then '5-10'
             when tip_amount > 10 and tip_amount <= 20 then '10-20'
             when tip_amount > 20 then '> 20'
             else 'other'
        end) as Tipbin,
       COUNT(*) as num,
       SUM(tip_amount) as Tips,
       ROUND(avg((tip_amount)/(total_amount-tip_amount))*100,3) as TipPercentage
FROM `bigquery-public-data.new_york.tlc_yellow_trips_2015`
WHERE trip_distance > 0 AND
      fare_amount/trip_distance BETWEEN 2 AND 10 AND
      dropoff_datetime > pickup_datetime
GROUP BY TIpBin
ORDER BY MIN(tip_amount);

Your missing a bunch of commas after each sum you need: 在您需要每笔款项后,您缺少一堆逗号:

SELECT
SUM(CASE WHEN TipPercentage < 0 THEN 1 ELSE 0 END) AS 'No Tip',
SUM(CASE WHEN TipPercentage BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS 'Less but still a Tip',
SUM(CASE WHEN TipPercentage BETWEEN 5 AND 10 THEN 1 ELSE 0 END) AS 'Decent Tip',
SUM(CASE WHEN TipPercentage > 10 THEN 1 ELSE 0 END) AS 'Good Tip',
-- SUM(ELSE ) AS 'Something different'//this line is missing something in 
-- the sum function
END AS TipRange,
TipPercentage,
Tipbin
FROM
(SELECT
case when tip_amount=0 then 'No Tip'
when (tip_amount > 0 and tip_amount <=5) then '0-5'
when (tip_amount > 5 and tip_amount <=10) then '5-10'
when (tip_amount > 10 and tip_amount <=20) then '10-20'
when tip_amount > 20 then '> 20'
else 'other'
end as Tipbin,
SUM(tip_amount) as Tips,
ROUND(avg((tip_amount)/(total_amount-tip_amount))*100,3) as TipPercentage
FROM `bigquery-public-data.new_york.tlc_yellow_trips_2015`
WHERE trip_distance >0
AND fare_amount/trip_distance BETWEEN 2 AND 10
AND dropoff_datetime > pickup_datetime
group by 1,2,3,tip_amount,tipbin) T --All derived tables must have an alias

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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