求 帐龄分析SQL语句
帐龄分析表结构及记录如下
Tb
名称 时间 金额
张三 2007.1.1 100
张三 2007.10.30 200
张三 2006.5.12 400
马六 2007.11.5 150
王五 2005.10.1 900
要求实现下表:
名称 3个月以内 3个月-1年 1年以上 合计
张三 200 100 400 700
马六 150 150
王五 900 900
insert @t select '张三','2007-1-1',100
union all select '张三','2007-10-30',200
union all select '张三','2006-5-12',400
union all select '马六','2007-11-5',150
union all select '王五','2005-10-1',900
select 名称,
[三个月以内]=sum(case when datediff(month,时间,getdate())<=3 then 金额 else 0 end),
[三个月到一年]=sum(case when datediff(month,时间,getdate())>3 and datediff(month,时间,getdate()) <=12 then 金额 else 0 end),
[一年以上]=sum(case when datediff(month,时间,getdate())>12 then 金额 else 0 end),
[合计]=sum(金额)
from @t
group by 名称
order by 名称
/*
名称 三个月以内 三个月到一年 一年以上 合计
---------- ----------- ----------- ----------- -----------
马六 150 0 0 150
王五 0 0 900 900
张三 200 100 400 700
(所影响的行数为 3 行)
/* 又比我快![em10] 我要加速度.. 高手就是高手!感谢啊![em13]
老大,再帮忙看看:
因为记录有正数有负数,而合计可能为0
我想让合并后的 合计为0的就不显示了。如何弄? [合计]=sum(金额) 可能为0,想让最后的结果中 [合计]=0的不显示 select 名称,
[三个月以内]=sum(case when datediff(month,时间,getdate())<=3 then 金额 else 0 end),
[三个月到一年]=sum(case when datediff(month,时间,getdate())>3 and datediff(month,时间,getdate()) <=12 then 金额 else 0 end),
[一年以上]=sum(case when datediff(month,时间,getdate())>12 then 金额 else 0 end),
[合计]=sum(金额)
from @t
group by 名称 having sum(金额)<>0
order by 名称
[[italic] 本帖最后由 XieLi 于 2007-11-30 10:53 编辑 [/italic]] 真的很感谢版主和XieLi[em13] [em13] 刚转了一下..晚了.. 下次留给你! 下次的机会来了[em12]
问题:
另外一个表为
TB_2
名称 描述
张三 大学
王二 中学
马六 小学
现在要在干才那个表的基础上,加上 描述 字段:
名称 描述 三个月以内 三个月到一年 一年以上 合计
---------- ----------- ----------- ----------- -----------
马六 小学 150 0 0 150
王五 中学 0 0 900 900
张三 大学 200 100 400 700 我在后面加上
...Tb_2.描述.... LEFT JOIN Tb_2 ON Tb.名称=Tb_2.名称
提示语法错误[em10] declare @t table(名称 varchar(10),时间 datetime,金额 int)
insert @t select '张三','2007-1-1',100
union all select '张三','2007-10-30',200
union all select '张三','2006-5-12',400
union all select '马六','2007-11-5',150
union all select '王五','2005-10-1',900
declare @t2 table(名称 varchar(10),描述 varchar(10))
insert @t2 select '张三','大学'
union all select '王五','中学'
union all select '马六','小学'
select a.名称,b.描述,a.[三个月以内],a.[三个月到一年],a.[一年以上],a.[合计]
from
(
select 名称,
[三个月以内]=sum(case when datediff(month,时间,getdate())<=3 then 金额 else 0 end),
[三个月到一年]=sum(case when datediff(month,时间,getdate())>3 and datediff(month,时间,getdate()) <=12 then 金额 else 0 end),
[一年以上]=sum(case when datediff(month,时间,getdate())>12 then 金额 else 0 end),
[合计]=sum(金额)
from @t
group by 名称
) a
left join @t2 b
on a.名称=b.名称
order by a.名称
/*
名称 描述 三个月以内 三个月到一年 一年以上 合计
---------- ---------- ----------- ----------- ----------- -----------
马六 小学 150 0 0 150
王五 中学 0 0 900 900
张三 大学 200 100 400 700
(所影响的行数为 3 行)
*/ [em14] [em14] 给你啦! 感谢两位!
我发现个问题:
select 名称,编号, [三个月以内]=sum(case when datediff(month,时间,getdate())<=3 then 金额 else 0 end) from Tb group by 名称
就要出错!
而去掉 编号
可以正常,也就是group by 只允许 一个字段 select 名称,编号, [三个月以内]=sum(case when datediff(month,时间,getdate())<=3 then 金额 else 0 end) from Tb group by 名称,编号
去帮助里查一下GROUP BY 的用法 这是Group by 句法的问题...
同意楼上说在帮助查一下..
但是你这种句法.
select 名称,编号, [三个月以内]=sum(case when datediff(month,时间,getdate())<=3 then 金额 else 0 end) from Tb group by 名称
...在SQL2003标准中是可以的.因为它会自动查找函数依赖..
但是现在还没有数据库产品支持SQL2003标准. SQL2003没用过呢?
现在不是有SQL2005吗好用吗? 哈哈。。。SQL区因你们而活跃啊。 [quote]原帖由 [bold][underline]XieLi[/underline][/bold] 于 2007-11-30 14:25 发表 [url=http://bbs.bc-cn.net/redirect.php?goto=findpost&pid=1124227&ptid=189454][img]http://bbs.bc-cn.net/images/common/back.gif[/img][/url]
SQL2003没用过呢?
现在不是有SQL2005吗好用吗? [/quote]
我也没用过。[em02]
页:
[1]
2
