教师姓名 月 捐献金额
王1 1 234.56
张4 10 23.56
赵5 9 223.56
齐0 8 235.56
王1 10 234.56
张4 10 23.56
赵5 9 223.56
齐0 8 235.56
王1 12 234.56
张4 10 23.56
赵5 9 223.56
齐0 8 235.56
王1 1 234.56
张4 10 23.56
赵5 5 223.56
齐0 8 235.56要求输出月 累计捐献金额
1 xxxxx
2 xxxxx
3 xxxxx
4 xxxxx
5 xxxxx
6 xxxxx
7 xxxxx
8 xxxxx
9 xxxxx
10 xxxxx
12 xxxxx
只有教师1-12月都有捐献金额的时候 才回被统计进入数据
******而且*****
我想知道教师的详细名单! 因为用一维表很难表诉出来
解决方案 »
- 导入文本数据,为什么{CR}$${CR}这样的分隔符不能使用呢
- 问一个关于复制数据库的问题
- 请教关于“主键”和“聚集索引”
- Subquery returned more than 1 value. This is , update operator ,update table ...,thanks a lot!
- 怎么这么多人不明白我的意思呢?怎么返回对应的name,age,workdate等字段的值?
- 查询每一个成员的最后一条记录的集合
- 还得麻烦 zjcxc(邹建) 看一下。
- 笔计本电脑和台式机电脑如何数据迁移?
- 急!!!VFP的坏form如何修复?
- 读取一个MS SQL SERVER6.5中有2万多条记录的一个表中的某行数据需十几秒时间,请问怎样提高读取速度?
- sql 简单中的查询,来解决一下...
- 根据选择的范围统计问题
from tablename
group by 教师姓名,月
having sum(月) = 78
from table
group by 教师姓名,月
having sum(月) = 78
select 月,捐献金额
from tttt
where 教师姓名 in(
select 教师姓名
from tttt
group by 教师姓名
having sum(月) = 78)
order by 月
create table tttt(教师姓名 varchar(10), 月 int , 捐献金额 numeric(9,2))
insert tttt
select '王1', 1 , 234.56 union all
select '王1', 2 , 234.56 union all
select '王1', 3 , 234.56 union all
select '王1', 4 , 234.56 union all
select '王1', 5 , 234.56 union all
select '王1', 6 , 234.56 union all
select '王1', 7 , 234.56 union all
select '王1', 8 , 234.56 union all
select '王1', 9 , 234.56 union all
select '王1', 10 , 234.56 union all
select '王1', 11 , 234.56 union all
select '王1', 12 , 234.56 union all
select '张4', 1 , 234.56 union all
select '张4', 4 , 234.56 union all
select '赵5', 8 , 234.56 union all
select '赵5', 9 , 234.56 union all
select '赵5', 10 , 234.56 union all
select '赵5', 11 , 234.56 union all
select '赵5', 12 , 234.56 union all
select '齐0', 1 , 234.56 union all
select '齐0', 4 , 234.56
select 月,捐献金额
from tttt
where 教师姓名 in(
select 教师姓名
from tttt
group by 教师姓名
having sum(月) = 78)
order by 月
----78是从1加道12的和
having sum(月) = 7878是什么意思?
------------------------------------------------------------------------------------ 1+2+3..+12=78
我使用
select a.月,sum(a.捐献金额)as 捐献金额 from
(
SELECT 月,捐献金额 ,教师姓名
FROM table )a
group by a.月 having sum(a.月)=12
order by a.月
检索不出数据
但我可以肯定 有一个或几个教师 1-12月份都有捐献
为什么统计不出来?
declare @t table(教师姓名 varchar(10), 月 int , 捐献金额 numeric(9,2))
insert @t
select '王1', 1 , 234.56 union all
select '王1', 2 , 234.56 union all
select '王1', 3 , 234.56 union all
select '王1', 4 , 234.56 union all
select '王1', 5 , 234.56 union all
select '王1', 6 , 234.56 union all
select '王1', 7 , 234.56 union all
select '王1', 8 , 234.56 union all
select '王1', 9 , 234.56 union all
select '王1', 10 , 234.56 union all
select '王1', 11 , 234.56 union all
select '王1', 12 , 234.56 union all
select '张4', 1 , 234.56 union all
select '张4', 4 , 234.56 union all
select '赵5', 8 , 234.56 union all
select '赵5', 9 , 234.56 union all
select '赵5', 10 , 234.56 union all
select '赵5', 11 , 234.56 union all
select '赵5', 12 , 234.56 union all
select '齐0', 1 , 234.56 union all
select '齐0', 4 , 234.56 select 教师姓名,月,
[捐献金额]=sum(捐献金额)
from @t t
where (select count(distinct 月) from @t where 教师姓名=t.教师姓名)=12
group by 教师姓名,月教师姓名 月 捐献金额
---------- ----------- ---------------------------------------
王1 1 234.56
王1 2 234.56
王1 3 234.56
王1 4 234.56
王1 5 234.56
王1 6 234.56
王1 7 234.56
王1 8 234.56
王1 9 234.56
王1 10 234.56
王1 11 234.56
王1 12 234.56(12 行受影响)
SELECT 教师姓名,月,
捐献金额
FROM t
and 教师姓名='王1 '
可以清楚显示该教师的12个月值
但是
一旦加以限定
select a.月,sum(a.捐献金额)as 捐献金额 from
(
SELECT 月,捐献金额 ,教师姓名
FROM table )a
group by a.月 having sum(a.月)=78 或--having count(a.月)=12
order by a.月
就检索不出数据
为什么?????????????/
无论 having count(distinct(a.月))=12
having sum(a.月)=78
只要加上此限定
就检索不出数据
你的写法就有问题select a.月,sum(a.捐献金额)as 捐献金额 from
(SELECT 月,捐献金额 ,教师姓名 FROM table )a ---这个地方为什么用个查询啊!
group by a.月 ---这个地方怎么能用“月”来分组呢?
having sum(a.月)=78 或--having count(a.月)=12
order by a.月-----你这样写看看
select 月,捐献金额=sum(捐献金额) from [table]
group by 教师姓名
having sum(月)=78
order by 月
select 教师姓名,sum(a.捐献金额)as 捐献金额 from
(
SELECT 月,捐献金额 ,教师姓名
FROM tttt )a
group by 教师姓名
having sum(distinct a.月)=78 --或--having count(a.月)=12
order by 教师姓名
from
(
SELECT 月,捐献金额
FROM ttt where dyear='2007'
)a
group by a.月 having count(distinct(a.月))=12依旧不行
太奇怪了
insert tttt
select '王1', 1 , 234.56 union all
select '王1', 2 , 234.56 union all
select '王1', 3 , 234.56 union all
select '王1', 4 , 234.56 union all
select '王1', 5 , 234.56 union all
select '王1', 6 , 234.56 union all
select '王1', 7 , 234.56 union all
select '王1', 8 , 234.56 union all
select '王1', 9 , 234.56 union all
select '王1', 10 , 234.56 union all
select '王1', 11 , 234.56 union all
select '王1', 12 , 234.56 union all
select '张4', 1 , 234.56 union all
select '张4', 4 , 234.56 union all
select '赵5', 8 , 234.56 union all
select '赵5', 9 , 234.56 union all
select '赵5', 10 , 234.56 union all
select '赵5', 11 , 234.56 union all
select '赵5', 12 , 234.56 union all
select '齐0', 1 , 234.56 union all
select '齐0', 4 , 234.56
select 月,捐献金额=sum(捐献金额) from tttt where 教师姓名 in
(select 教师姓名 from tttt group by 教师姓名 having sum(月)=78) group by 月
select 教师姓名,月,
[捐献金额]=sum(捐献金额)
from @t t
where (select count(distinct 月) from @t where 教师姓名=t.教师姓名)=12
group by 教师姓名,月
select a.月, sum(a.捐献金额)as 捐献金额
from
(
SELECT 月,捐献金额
FROM ttt where dyear='2007'
)a
group by a.月 having count(distinct(a.月))=12 ---这个地方有问题依旧不行
太奇怪了
----------------------有点不懂lz为什么老是用“月”进行分组呢?说过了要按照“教师姓名”分组的
1 xxxxx 王1
2 xxxxx 王1
3 xxxxx 王1
4 xxxxx 王1
5 xxxxx 王1
6 xxxxx 王1
7 xxxxx 王1
8 xxxxx 王1
9 xxxxx 王1
10 xxxxx 王1
12 xxxxx 王1
drop table [dbo].[tttt]
GO
create table tttt(教师姓名 varchar(10), 月 int , 捐献金额 numeric(9,2))
insert tttt
select '王1', 1 , 234.56 union all
select '王1', 2 , 234.56 union all
select '王1', 3 , 234.56 union all
select '王1', 4 , 234.56 union all
select '王1', 5 , 234.56 union all
select '王1', 6 , 234.56 union all
select '王1', 7 , 234.56 union all
select '王1', 8 , 234.56 union all
select '王1', 9 , 234.56 union all
select '王1', 10 , 234.56 union all
select '王1', 11 , 234.56 union all
select '王1', 12 , 234.56 union all
select '张4', 1 , 234.56 union all
select '张4', 4 , 234.56 union all
select '赵5', 8 , 234.56 union all
select '赵5', 9 , 234.56 union all
select '赵5', 10 , 234.56 union all
select '赵5', 11 , 234.56 union all
select '赵5', 12 , 234.56 union all
select '齐0', 1 , 234.56 union all
select '齐0', 4 , 234.56 union all
select '王2', 1 , 234.56 union all
select '王2', 2 , 234.56 union all
select '王2', 3 , 234.56 union all
select '王2', 4 , 234.56 union all
select '王2', 5 , 234.56 union all
select '王2', 6 , 234.56 union all
select '王2', 7 , 234.56 union all
select '王2', 8 , 234.56 union all
select '王2', 9 , 234.56 union all
select '王2', 10 , 234.56 union all
select '王2', 11 , 234.56 union all
select '王2', 12 , 21.1
declare @user nvarchar(1000)
select @user='' select @user=case when @user='' then 教师姓名 else @user+','+教师姓名 end
from (select 教师姓名 from tttt group by 教师姓名 having sum(月)=78) a
print @user select 月,捐献金额=sum(捐献金额),捐献名单=@user from tttt
where 教师姓名 in(select 教师姓名 from tttt group by 教师姓名 having sum(月)=78) group by 月