按日期分组

 
现在要从一张表里面取出记录按照时期来分组。
必然说我要取得表里面客户的从1月12月的所有记录,该怎么做?

另外,如果要求出每个客户从1月到12的记录,该怎么做?
select   a.CustomerName,isnull(b.TotalPiece,0)   as   TotalPiece,isnull(b.TotalMoney,0)   AS   TotalMoney
from
(select   distinct     CustomerName   from   TQof
where     CompanyID= '100001 '   )   a
left   join  
(
select   CustomerName,Count(*)   AS   TotalPiece,SUM(TotalMoney)   AS   TotalMoney
from   TQof
Group   by   CustomerName
)   b
on   a.CustomerName   =b.CustomerName
上面这个是用来取出所有客户的总金额,总数目的SQL,如果客户总金额或者总数目为0那也可以显示出来,就是0。
谢谢啦。
 
 
30 
id="Topic_Zone" style="HEIGHT: 4px" marginwidth="0" marginheight="0" src="/u/AD/Topic_Zone.aspx" frameborder="0" width="100%" scrolling="no" height="0">
  • marco08
  • 等 级:
发表于:2007-02-09 00:34:571楼 得分:20
--try


select   CustomerName,
[1月金额]=sum(case   when   month(日期字段)=1   then   TotalMoney   else   0   end),
[2月金额]=sum(case   when   month(日期字段)=2   then   TotalMoney   else   0   end),
[3月金额]=sum(case   when   month(日期字段)=3   then   TotalMoney   else   0   end),
[4月金额]=sum(case   when   month(日期字段)=4   then   TotalMoney   else   0   end),
[5月金额]=sum(case   when   month(日期字段)=5   then   TotalMoney   else   0   end),
[6月金额]=sum(case   when   month(日期字段)=6   then   TotalMoney   else   0   end),

[7月金额]=sum(case   when   month(日期字段)=7   then   TotalMoney   else   0   end),
[8月金额]=sum(case   when   month(日期字段)=8   then   TotalMoney   else   0   end),
[9月金额]=sum(case   when   month(日期字段)=9   then   TotalMoney   else   0   end),
[10月金额]=sum(case   when   month(日期字段)=10   then   TotalMoney   else   0   end),
[11月金额]=sum(case   when   month(日期字段)=11   then   TotalMoney   else   0   end),
[12月金额]=sum(case   when   month(日期字段)=12   then   TotalMoney   else   0   end),

Count(*)   AS   TotalPiece,
isnull(SUM(TotalMoney),   0)   AS   TotalMoney
from   TQof
where   year(日期字段)=2006   and   CompanyID= '100001 '
Group   by   CustomerName
 
发表于:2007-02-09 08:18:422楼 得分:5
樓上.
 
发表于:2007-02-09 09:01:583楼 得分:5
假设表结构为ID,DATE,NUMBER
以下为按月份分组

select   substring(convert(varchar(10),date,120),6,2)   as   date,   sum(number)   as   number  
from   tb
group   by   substring(convert(varchar(10),date,120),6,2)

以下为按每人每月份分组
select   id   ,   substring(convert(varchar(10),date,120),6,2)   as   date,   sum(number)   as   number  
from   tb
group   by   id   ,   substring(convert(varchar(10),date,120),6,2)

 
发表于:2007-02-09 09:50:594楼 得分:0
select   a.CustomerName,isnull(b.TotalPiece,0)   as   TotalPiece,isnull(b.TotalMoney,0)   AS   TotalMoney,
isnull(b.oneTotalMoney,0)   AS   oneTotalMoney,
isnull(b.twoTotalMoney,0)   AS   twoTotalMoney,
isnull(b.threeTotalMoney,0)   AS   threeTotalMoney,
isnull(b.fourTotalMoney,0)   AS   fourTotalMoney,
isnull(b.fiveTotalMoney,0)   AS   fiveTotalMoney,
isnull(b.sixTotalMoney,0)   AS   sixTotalMoney,
isnull(b.sevenTotalMoney,0)   AS   sevenTotalMoney,
isnull(b.eightTotalMoney,0)   AS   eightTotalMoney,
isnull(b.nineTotalMoney,0)   AS   nineTotalMoney,
isnull(b.tenTotalMoney,0)   AS   tenTotalMoney,
isnull(b.elevenTotalMoney,0)   AS   elevenTotalMoney,
isnull(b.twelveTotalMoney,0)   AS   twelveTotalMoney
from
(select   distinct     CustomerName   from   TQof
where     CompanyID= '100001 '   )   a
left   join  
(
select   CustomerName,Count(*)   AS   TotalPiece,SUM(TotalMoney)   AS   TotalMoney,
[oneTotalMoney]=sum(case   when   month(QOFDate)=1   then   TotalMoney   else   0   end),
[twoTotalMoney]=sum(case   when   month(QOFDate)=2   then   TotalMoney   else   0   end),
[threeTotalMoney]=sum(case   when   month(QOFDate)=3   then   TotalMoney   else   0   end),
[fourTotalMoney]=sum(case   when   month(QOFDate)=4   then   TotalMoney   else   0   end),
[fiveTotalMoney]=sum(case   when   month(QOFDate)=5   then   TotalMoney   else   0   end),
[sixTotalMoney]=sum(case   when   month(QOFDate)=6   then   TotalMoney   else   0   end),
[sevenTotalMoney]=sum(case   when   month(QOFDate)=7   then   TotalMoney   else   0   end),
[eightTotalMoney]=sum(case   when   month(QOFDate)=8   then   TotalMoney   else   0   end),
[nineTotalMoney]=sum(case   when   month(QOFDate)=9   then   TotalMoney   else   0   end),
[tenTotalMoney]=sum(case   when   month(QOFDate)=10   then   TotalMoney   else   0   end),
[elevenTotalMoney]=sum(case   when   month(QOFDate)=11   then   TotalMoney   else   0   end),
[twelveTotalMoney]=sum(case   when   month(QOFDate)=12   then   TotalMoney   else   0   end)
from   TQof   WHERE   YEAR(QOFDate)=2005
Group   by   CustomerName
)   b
on   a.CustomerName   =b.CustomerName

显示每个客户每个月份的总金额和总数目。
谢谢marco08(天道酬勤)   ,另外两位也多谢了,按照楼上那位的做法,取出来的是按照数据库中已经存在的日期然后取出月份来分组,但这样不一定是1到12月份都会有,还是谢谢了,呵呵。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值