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)
我试图从Google Bigquery获取数据,其中每个'无提示','少但仍然提示','体面提示','好提示'和'某些不同'的总和将基于每个人的数量。但是,我收到一个语法错误,说字符串'No Tip'是意料之外的。
有人可以指导我吗?
谢谢!
编辑:
我使用标准SQL获得的错误代码是
Error: Syntax error: Unexpected string literal 'No Tip' at [2:55]
当我尝试使用旧版SQL运行时,我得到了这个:
Error: Encountered " "AS" "AS "" at line 2, column 52. Was expecting: <EOF>
答案 0 :(得分:1)
根据您的描述,您似乎基本上想要您的子查询。在这里,它被修复了一些语法错误:
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);
答案 1 :(得分:0)
在你需要的每笔款项之后,你遗漏了一堆逗号:
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