create table test
(
Pid int identity not null primary key,
Years datetime,
IsIsFirstSixMonths int default(0), --0表示上半年1表示下半年--
TotalCome int,
TotalOut int
)
假设表里有5条数据select * from test结果如下:
Pid Years IsIsFirstSixMonths TotalCome
1 2007-1-1 0 50
2 2007-3-1 0 60
3 2007-12-1 1 80
4 2008-1-1 0 100
5 2008-12-1 0 100
IsIsFirstSixMonths表段 0表示上半年1表示下半年
我想要的结果是:2007上半年的TotalCome相加的和(说明:SUM,值应该是110)
2007下半年的TotalCome相加的和(说明:SUM,值应该是80))
2008上半年的TotalCome相加的和(说明:SUM,值应该是100)
2008下半年的TotalCome相加的和(说明:SUM,值应该是100)
(
Pid int identity not null primary key,
Years datetime,
IsIsFirstSixMonths int default(0), --0表示上半年1表示下半年--
TotalCome int,
TotalOut int
)
假设表里有5条数据select * from test结果如下:
Pid Years IsIsFirstSixMonths TotalCome
1 2007-1-1 0 50
2 2007-3-1 0 60
3 2007-12-1 1 80
4 2008-1-1 0 100
5 2008-12-1 0 100
IsIsFirstSixMonths表段 0表示上半年1表示下半年
我想要的结果是:2007上半年的TotalCome相加的和(说明:SUM,值应该是110)
2007下半年的TotalCome相加的和(说明:SUM,值应该是80))
2008上半年的TotalCome相加的和(说明:SUM,值应该是100)
2008下半年的TotalCome相加的和(说明:SUM,值应该是100)
解决方案 »
- 这是什么方法体?
- 能不能用javascript 遍历 浏览器中 所有 的cookie
- 求一个主从表录入的例子
- 在c#中,请问 asp.net内容页怎么 更新母版页控件的值
- VS2008 网站 调试问题
- 100分, 求各种dropdown下拉效果,要求能支持firefox
- 关于文件更改问题。。。 不好意思没分了
- 正则检验控件用在textarea上时,出现的异常.
- 在线的高手门帮帮偶啊~我有语句中有几个:select(count 1,sum a)... ,select(count 2,sum b)...,selcet(count3,sum c)...然后我要返回一
- sos(传值问题)
- 保存图片水印部分出错..请求帮助
- 上传时如何限制图片的宽高?
select year(years),sum(totalcome) from test group by year(Years),IsIsFirstSixMonths order by year(years)楼上的要对years进行一个排序
use dbTemp
create table test
(
Pid int identity(1,1) not null primary key,
Years datetime,
IsFirstSixMonths int default(0), --0表示上半年1表示下半年--
TotalCome int
)
insert test
select '2007-1-1',0,50
union
select '2007-3-1',0,60
union
select '2007-12-1',1,80
union
select '2008-1-1',0,100
union
select '2008-12-1',1,100select * from testselect convert(char(4),Years,120) as 'year',
IsFirstSixMonths=case when IsFirstSixMonths=0 then '上半年' when IsFirstSixMonths=1 then '下半年' END ,
sum(totalcome) as 'sum' from test
group by IsFirstSixMonths,convert(char(4),Years,120)或者
select convert(char(4),Years,120) as 'year',
IsFirstSixMonths=case when IsFirstSixMonths=0 then '上半年' ELSE '下半年' END ,
sum(totalcome) as 'sum' from test
group by IsFirstSixMonths,convert(char(4),Years,120)--DROP DATABASE dbtemp
然后 SUM