另外,如果要求出每个客户从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。 谢谢啦。
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
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)
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