jockey 发表于 2007-11-30 09:50

求 帐龄分析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

purana 发表于 2007-11-30 10:05

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

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 行)
/*

XieLi 发表于 2007-11-30 10:17

又比我快![em10]

purana 发表于 2007-11-30 10:19

我要加速度..

jockey 发表于 2007-11-30 10:45

高手就是高手!感谢啊![em13]

老大,再帮忙看看:
因为记录有正数有负数,而合计可能为0
我想让合并后的 合计为0的就不显示了。如何弄?

jockey 发表于 2007-11-30 10:46

[合计]=sum(金额) 可能为0,想让最后的结果中 [合计]=0的不显示

XieLi 发表于 2007-11-30 10:51

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]]

jockey 发表于 2007-11-30 10:54

真的很感谢版主和XieLi[em13] [em13]

purana 发表于 2007-11-30 10:57

刚转了一下..晚了..

XieLi 发表于 2007-11-30 10:58

下次留给你!

jockey 发表于 2007-11-30 11:20

下次的机会来了[em12]

问题:
另外一个表为
TB_2
名称   描述
张三   大学
王二   中学
马六   小学

现在要在干才那个表的基础上,加上 描述 字段:

名称  描述       三个月以内       三个月到一年      一年以上        合计         
---------- ----------- ----------- ----------- -----------
马六  小学       150         0           0           150
王五  中学       0           0           900         900
张三  大学       200         100         400         700

jockey 发表于 2007-11-30 11:22

我在后面加上
   ...Tb_2.描述.... LEFT JOIN Tb_2  ON Tb.名称=Tb_2.名称

提示语法错误[em10]

purana 发表于 2007-11-30 11:29

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 行)
*/

XieLi 发表于 2007-11-30 11:36

[em14] [em14] 给你啦!

jockey 发表于 2007-11-30 13:12

感谢两位!
我发现个问题:

select 名称,编号, [三个月以内]=sum(case when datediff(month,时间,getdate())<=3 then 金额 else 0 end) from Tb group by 名称

就要出错!
而去掉 编号
可以正常,也就是group by 只允许 一个字段

XieLi 发表于 2007-11-30 13:33

select 名称,编号, [三个月以内]=sum(case when datediff(month,时间,getdate())<=3 then 金额 else 0 end) from Tb group by 名称,编号

去帮助里查一下GROUP BY 的用法

purana 发表于 2007-11-30 14:19

这是Group by 句法的问题...
同意楼上说在帮助查一下..

但是你这种句法.
select 名称,编号, [三个月以内]=sum(case when datediff(month,时间,getdate())<=3 then 金额 else 0 end) from Tb group by 名称

...在SQL2003标准中是可以的.因为它会自动查找函数依赖..
但是现在还没有数据库产品支持SQL2003标准.

XieLi 发表于 2007-11-30 14:25

SQL2003没用过呢?
现在不是有SQL2005吗好用吗?

缘吇弹 发表于 2007-11-30 14:34

哈哈。。。SQL区因你们而活跃啊。

缘吇弹 发表于 2007-11-30 14:35

[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

编程论坛