基本SQL问题

new100bits 2006-01-15 04:00:53
Table1结构如下:
表名:rpt_newadd
Date NewAdd(日新增量)
20050701 50
20050702 70
... ...
20060101 100
20060102 200
20060103 150
... ...

Table2结构如下:
表名:rpt_yearplan
Year PlanCnt
2005 1000
2006 2000
... ...

现在要得出这样一张表:

Day PlanCnt(年计划值) NewMonthSum(当月合计) YearSum(当年累计)
200507 1000 300 300
... ... ... ...
200601 2000 800 800

如果今天是20060112,
当月合计就是从20060101---20060112的值,当年累计就是20060101---20060112的值
恳求各位大虾的帮助!


...全文
170 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr_Bean 2006-01-27
  • 打赏
  • 举报
回复
实际上你这种功能完全可以通过table1统计得到
没有必要去关联另外的那个表
ibanez550 2006-01-17
  • 打赏
  • 举报
回复
怎么没有键值呢?你依据什么统计呢?
子陌红尘 2006-01-16
  • 打赏
  • 举报
回复
select
Char(Year(b.Date))||Char(Month(b.Date)) as Day,
a.PlanCnt,
sum(NewAdd) as NewMonthSum,
(select sum(NewAdd) from rpt_newadd where Year(Date)=Year(b.Date) and Month(Date)<=Month(b.Date)) as YearSum
from
rpt_yearplan a,
rpt_newadd b
where
a.Year=Year(b.Date)
group by
Year(b.Date),Month(b.Date),a.PlanCnt
order by
Char(Year(b.Date))||Char(Month(b.Date))
子陌红尘 2006-01-16
  • 打赏
  • 举报
回复
try:

select
Year(b.Date) ,
Month(b.Date),
a.PlanCnt ,
sum(NewAdd) as NewMonthSum,
(select sum(NewAdd) from rpt_newadd where Year(Date)=Year(b.Date) and Month(Date)<=Month(b.Date)) as YearSum
from
rpt_yearplan a,
rpt_newadd b
where
a.Year=Year(b.Date)
group by
Year(b.Date),Month(b.Date),a.PlanCnt

5,888

社区成员

发帖
与我相关
我的任务
社区描述
IBM DB2 是美国IBM公司开发的一套关系型数据库管理系统,它主要的运行环境为UNIX(包括IBM自家的AIX)、Linux、IBM i(旧称OS/400)、z/OS,以及Windows服务器版本
社区管理员
  • DB2
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧