Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sqlgooglebigquery中的计数案例_Sql_Google Bigquery - Fatal编程技术网

sqlgooglebigquery中的计数案例

sqlgooglebigquery中的计数案例,sql,google-bigquery,Sql,Google Bigquery,当我尝试使用遗留SQL运行它时,我得到了以下结果: Error: Syntax error: Unexpected string literal 'No Tip' at [2:55] 您需要的每一笔金额后都缺少一组逗号: Error: Encountered " "AS" "AS "" at line 2, column 52. Was expecting: <EOF> 您需要的每一笔金额后都缺少一组逗号: Error: Encountered " "AS" "AS "" at

当我尝试使用遗留SQL运行它时,我得到了以下结果:

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

您需要的每一笔金额后都缺少一组逗号:

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

您需要的每一笔金额后都缺少一组逗号:

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

根据您的描述,您似乎本质上想要子查询。这里对其进行了一些清理,修复了语法错误:

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

根据您的描述,您似乎本质上想要子查询。这里对其进行了一些清理,修复了语法错误:

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

你能给出准确的错误代码吗?在你的问题中添加google bigquery标签。谢谢你能给出准确的错误代码吗?在你的问题中添加google bigquery标签。谢谢嗨,是的,这就是我要找的。非常感谢@本。你的子查询实际上非常接近。嗨,是的,这就是我要找的。非常感谢@本。您的子查询实际上非常接近。谢谢!我不知道如何去解释其他案例,因为我发现的在线案例只有一个案例。。。在里面。谢谢!我不知道如何去解释其他案例,因为我发现的在线案例只有一个案例。。。在里面。