我遇到此错误“ SELECT列表表达式引用的列FY既不在[4:20]处也不进行分组或聚集”,这是SQL语句:
SELECT '00-ActualTotalRevenues' as type,code, previous_code,
name as master1,'' as master2,'' as master3,'' as master4,'' as master5,
case when FY < 2018 then sum(January+February+March+April+May+June+July+August+September+October+November+December) else 0 end as detail,
case when FY = 2018 then sum(January) else 0 end as detail1,
case when FY = 2018 then sum(February) else 0 end as detail2,
case when FY = 2018 then sum(March) else 0 end as detail3,
case when FY = 2018 then sum(April) else 0 end as detail4,
case when FY = 2018 then sum(May) else 0 end as detail5,
case when FY = 2018 then sum(June) else 0 end as detail6,
case when FY = 2018 then sum(July) else 0 end as detail7,
case when FY = 2018 then sum(August) else 0 end as detail8,
case when FY = 2018 then sum(September) else 0 end as detail9,
case when FY = 2018 then sum(October) else 0 end as detail10,
case when FY = 2018 then sum(November) else 0 end as detail11,
case when FY = 2018 then sum(December) else 0 end as detail12
FROM `basetis-etl-bigquery.services.results_actual_revenues_complete` where FY <= 2018
group by 1,2,3,4,5,6,7,8
以下查询运行没有问题:
SELECT '00-ActualTotalRevenues' as type,code, previous_code,
name as master1,'' as master2,'' as master3,'' as master4,'' as master5,
sum(January) as detail1,
sum(February) as detail2,
sum(March) as detail3,
sum(April) as detail4,
sum(May) as detail5,
sum(June) as detail6,
sum(July) as detail7,
sum(August) as detail8,
sum(September) as detail9,
sum(October) as detail10,
sum(November) as detail11,
sum(December) as detail12
FROM `basetis-etl-bigquery.services.results_actual_revenues_complete` where FY <= 2018
group by 1,2,3,4,5,6,7,8
预先感谢您的支持。
亲切的问候
答案 0 :(得分:1)
在下面改用
#standardSQL
SELECT '00-ActualTotalRevenues' AS type, code, previous_code,
name AS master1,'' AS master2,'' AS master3,'' AS master4,'' AS master5,
SUM(IF(FY < 2018, January+February+March+April+May+June+July+August+September+October+November+December, 0))AS detail,
SUM(IF(FY = 2018, January, 0)) AS detail1,
SUM(IF(FY = 2018, February, 0)) AS detail2,
SUM(IF(FY = 2018, March, 0)) AS detail3,
SUM(IF(FY = 2018, April, 0)) AS detail4,
SUM(IF(FY = 2018, May, 0)) AS detail5,
SUM(IF(FY = 2018, June, 0)) AS detail6,
SUM(IF(FY = 2018, July, 0)) AS detail7,
SUM(IF(FY = 2018, August, 0)) AS detail8,
SUM(IF(FY = 2018, September, 0)) AS detail9,
SUM(IF(FY = 2018, October, 0)) AS detail10,
SUM(IF(FY = 2018, November, 0)) AS detail11,
SUM(IF(FY = 2018, December, 0)) AS detail12
FROM `basetis-etl-bigquery.services.results_actual_revenues_complete` WHERE FY <= 2018
GROUP BY 1,2,3,4,5,6,7,8