SQL Server中的GROUP BY有问题吗? [英] A problem with GROUP BY in SQL Server?

查看:107
本文介绍了SQL Server中的GROUP BY有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含3列的表(名为MyIncome),第一个是Year(2008、2008、2009),第二个是Month(1、2、1),第三个是收入(1000美元,1500美元,2000美元) )
现在,我想知道年份"列中每年的收入是多少.我使用了以下T-SQL语句:
SELECT Year, sum(Income) as [Annual Income] FROM MyIncome GROUP BY Year
结果应该是一个包含两列的表,一个是Year(2008,2009),另一个是Year Income($ 2500,$ 2000).
但是,除非我必须按年份和收入进行分组,否则它不起作用(这将导致结果与仅按年份分组的结果有所不同.).
尤其是上面的陈述在Access中似乎效果很好,能否请您为我的问题提供解决方案,我认为经常要做这样的工作.
非常感谢!
更新
我为这个错误感到抱歉,请查看我的另一个问题,现在我想知道每年的收入(例如,对于上面的MyIncome表,结果应该是一个包含两列的表:Year(2008 (2009年)和总收入(2500,4500)).到2009年为止的收入是2008年收入和2009年收入的总和.
请帮我解决.
非常感谢!

Suppose I have a table (named MyIncome) with 3 columns, the first is Year (2008, 2008, 2009), the second is Month (1, 2, 1), the third is Income ($1000, $1500, $2000)
Now I want to know how much the Income are for each year in the Year column. I used the following T-SQL statement:
SELECT Year, sum(Income) as [Annual Income] FROM MyIncome GROUP BY Year
The result should be a table with two columns, one is Year(2008, 2009), the other is Annual Income ($2500,$2000).
But it didn''t work unless I had to group by both Year and Income (this will cause the result to be different from what I want with grouping by Year only).
Especially the above statement seems to work well in Access, could you please give me a solution for my problem, I think it is very often to do such a job.
Thank you so much!
UPDATE
I''m sorry for the mistake, please see another problem of mine, now I want to know the income up to each year (for example, for the MyIncome table above, the result should be a table with two columns: Year(2008, 2009) and Total Income(2500, 4500)). The income up to 2009 is the sum of 2008''s income and 2009''s income.
Please help me solve that.
Thank you so much!

推荐答案

1000,


1500,


2000)
现在,我想知道年份"列中每年的收入是多少.我使用了以下T-SQL语句:
SELECT Year, sum(Income) as [Annual Income] FROM MyIncome GROUP BY Year
结果应该是一个包含两列的表,一列是Year(2008,2009),另一列是Annual Income(
2000)
Now I want to know how much the Income are for each year in the Year column. I used the following T-SQL statement:
SELECT Year, sum(Income) as [Annual Income] FROM MyIncome GROUP BY Year
The result should be a table with two columns, one is Year(2008, 2009), the other is Annual Income (


这篇关于SQL Server中的GROUP BY有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆