LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

按月统计查询之SQL语句

admin
2013年8月26日 22:17 本文热度 4231

数据表Order.dbo结构:
OrderNo varchar(10), ---单号
GetDate Varchar(10), ---日期
Sales Varchar(10), ---员
ItemNo Varchar(20), ---产品号
Amount Smallint ---金额

数据记录如下:
001 2004-01-01 ABC A001 1000
001 2004-01-15 QQQ A001 5000
002 2004-02-01 ABC A001 2000
003 2004-03-01 ABC A001 3000
004 2004-06-01 ABC A001 6000
......

要求输入欲查询的年度条件如:2004,得到按月份统计的如下结果:
Slaes 200401 200402 200403 200404 200405 200406 200407 200408 200409 200410 200411
----- ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
ABC 1000 2000 3000 6000
QQQ 5000

第一种答案:

declare @sql varchar(8000)
set @sql = 'select Sales'
select @sql = @sql + ',sum(case left(GetDate,7) when '''+left(GetDate,7)+''' then Amount end) ['+ replace(left(GetDate,7),'-','')+']'

from (select distinct left(GetDate,7) as GetDate from test) as a
select @sql = @sql+' from test group by Sales'
print @sql
exec(@sql)

第二种答案:

SQL语句:
select
sales as '业务员',
sum(case when month(getdatetime)=1 then amount end) as '1月份总额',
sum(case when month(getdatetime)=2 then amount end) as '2月份总额',
sum(case when month(getdatetime)=3 then amount end) as '3月份总额',
sum(case when month(getdatetime)=4 then amount end) as '4月份总额',
sum(case when month(getdatetime)=5 then amount end) as '5月份总额',
sum(case when month(getdatetime)=6 then amount end) as '6月份总额',
sum(case when month(getdatetime)=7 then amount end) as '7月份总额',
sum(case when month(getdatetime)=8 then amount end) as '8月份总额',
sum(case when month(getdatetime)=9 then amount end) as '9月份总额',
sum(case when month(getdatetime)=10 then amount end) as '10月份总额',
sum(case when month(getdatetime)=11 then amount end) as '11月份总额',
sum(case when month(getdatetime)=12 then amount end) as '12月份总额'
from you_table group by sales
--********************************************************************************
--存储过程
create procedure sp_ordertable
@Year int
as
select
sales as '业务员',
sum(case when month(getdatetime)=1 then amount end) as '1月份总额',
sum(case when month(getdatetime)=2 then amount end) as '2月份总额',
sum(case when month(getdatetime)=3 then amount end) as '3月份总额',
sum(case when month(getdatetime)=4 then amount end) as '4月份总额',
sum(case when month(getdatetime)=5 then amount end) as '5月份总额',
sum(case when month(getdatetime)=6 then amount end) as '6月份总额',
sum(case when month(getdatetime)=7 then amount end) as '7月份总额',
sum(case when month(getdatetime)=8 then amount end) as '8月份总额',
sum(case when month(getdatetime)=9 then amount end) as '9月份总额',
sum(case when month(getdatetime)=10 then amount end) as '10月份总额',
sum(case when month(getdatetime)=11 then amount end) as '11月份总额',
sum(case when month(getdatetime)=12 then amount end) as '12月份总额'
from lpy_ordertable where year(getdatetime)=@year group by sales

exec sp_ordertable 2004

业务员 1月份总额 2月份总额 3月份总额 4月份总额 5月份总额 6月份总额 7月份总额 8月份总额 9月份总额 10月份总额 11月份总额 12月份总额
---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
abc 1000 2000 3000 NULL NULL 6000 NULL NULL NULL NULL NULL NULL
qqq 5000 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

(所影响的行数为 2 行)


该文章在 2013/8/26 22:17:15 编辑过

全部评论1

admin
2013年8月26日 22:19
方法一:这种方法列名是固定的。
select 名称,
sum(case when substring(convert(varchar(7),日期,120),6,2)='09' then 出售数量 else 0 end) as '9月',
sum(case when substring(convert(varchar(7),日期,120),6,2)='10' then 出售数量 else 0 end) as '10月'
from 表
group by 名称

方法二:这种方法虽然麻烦一些,但是支持列名不确定的情况。
declare @sql varchar(2000)
declare @mon varchar(2)

set @sql = 'select 名称'
select @sql = @sql + ',sum(case mon  when '''+mon+''' then 出售数量 end) ['+mon+'月]'
from (select distinct substring(convert(varchar(7),日期,120),6,2) mon from 表) as a
select @sql = @sql+' from 表 group by 名称'
exec(@sql)

该评论在 2013/8/26 22:19:17 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved