首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CASE语句和group by

问CASE语句和group by
EN

Stack Overflow用户
提问于 2021-03-14 20:53:11
回答 1查看 36关注 0票数 0
代码语言:javascript
复制
select  [Major Genre],count(*) as 'Movie Count', 
        sum([US Gross]) as 'Total US Gross',
        sum([Worldwide Gross]-[Production Budget]) as 'Profit', 
        (sum([Worldwide Gross]-[Production Budget])/SUM([Production Budget]))*100 as 'Gross Profit',
    case when sum([US Gross])>=200000000 AND sum([US Gross])<=999000000 
         then 'US Gross more then 220M'
         when sum([US Gross])>=999000000 
         then 'US Gross more then 999M'
     end AS 'US Gross Status'
from Sheet1$
group by [Major Genre],[US Gross]
having [US Gross] > 220000000
order by [Major Genre]

我希望它只显示关于每个主要流派的汇总信息,现在我在每个主要流派上得到了几行,我如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2021-03-14 21:21:24

如果您可以提供示例数据和所需的输出,这将有所帮助,但是查看您的查询,我可以看到它需要稍微调整的地方。您需要从Group by中删除[US Gross],并在Having子句中对其执行sum操作。通过按它分组,您将获得每个不同值的行,尽管使用了sum。

代码语言:javascript
复制
select [Major Genre], count(*) as 'Movie Count', 
sum([US Gross]) as 'Total US Gross',
sum([Worldwide Gross]-[Production Budget]) as 'Profit', 
(sum([Worldwide Gross]-[Production Budget])/SUM([Production Budget]))*100 as 'Gross Profit',
    case when sum([US Gross])>=200000000 AND sum([US Gross])<=999000000 
    then 'US Gross more then 220M'
    when sum([US Gross])>=999000000 
    then 'US Gross more then 999M'
    end AS 'US Gross Status'
from Sheet1$
group by [Major Genre]
having Sum([US Gross]) > 220000000
order by [Major Genre]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66624745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档