按日期分组

 
现在要从一张表里面取出记录按照时期来分组。
必然说我要取得表里面客户的从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 回复次数:4 显示所有回复 显示星级回复 显示楼主回复
  • marco08
  • 天道酬勤(学习.NET中)
  • 等 级:
发表于: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
 
  • shiliangdong
  • stou
  • 等 级:
发表于:2007-02-09 08:18:422楼 得分:5
樓上.
 
  • dawugui
  • 潇洒老乌龟
  • 等 级:
发表于: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)

 
  • kyoo_1985
  • lovefishsmile
  • 等 级:
发表于: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月份都会有,还是谢谢了,呵呵。

你可能感兴趣的:(按日期分组)