这是我最初的查询语句..。
SELECT airline company, country, count(country) "The no.of times the airline visits the country"
FROM [table]
GROUP BY airline company, country我认为这是可行的,fine...until,我想在有错误的地方添加一个案例语句.
修改后,
Airline Company : country : No.Of times
Singapore Airline : SG : 5
Singapore Airline : MAL : 7
Japan Airline : SG : 3
Japan Airline : MAL : 2在第一个查询语句之后,结果如上面所示。我想将No.of times列的值更改为0,而不考虑国家“MAL”的航空公司。因此,我尝试将case语句添加到select中,但它给了我like group函数。
SELECT airline company, country, CASE WHEN country = 'MAL' THEN 0
ELSE count(country) "The no.of times the airline visits the country" END
FROM [table]
GROUP BY airline company, country, CASE WHEN country = 'MAL' THEN 0
ELSE count(country) "The no.of times the airline visits the country" END我该怎么解决呢?
发布于 2019-12-18 04:57:54
我会做Conditional Aggregation:
SELECT airline_company, country,
COUNT ( CASE WHEN country = 'MAL' THEN NULL
ELSE country
END ) as "# of times the airline visits"
FROM [table]
GROUP BY airline_company, country发布于 2019-12-18 05:14:44
我认为您不需要"group“中的"CASE”条件来实现您想要的结果,只需将其保存在select stmt中即可。
我希望这能帮到你。
发布于 2019-12-18 05:44:05
我认为您的查询中存在多个问题:
airline company是列名,那么它必须以区分大小写的方式包装在双引号中:"airline company"或"AIRLINE COMPANY"。因为oracle不允许直接使用包含空格"The no.of times the airline visits the country"。在ENDGROUP BY子句中的CASE表达式之后,必须为GROUP BY表达式提供别名,因为case表达式只直接使用了一个列(COUNTRY),该列已经存在于GROUP BY子句中。因此,您的查询必须如下所示:
选择“航空公司”,-添加双引号国家,当国家= 'MAL‘然后0其他计数(国家)结束为“航空公司访问国家的no.of时间”-别名是在YOUR_TABLE集团的END关键字之后由“航空公司”,COUNTRY -案例--评论这句话--当COUNTRY = 'MAL‘那么0 -- AS COUNT(COUNTRY) --结尾为“航空公司访问该国的no.of时间”。
干杯!!
https://stackoverflow.com/questions/59385638
复制相似问题