91网首页-91网页版-91网在线观看-91网站免费观看-91网站永久视频-91网站在线播放

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

按月統(tǒng)計查詢之SQL語句

admin
2013年8月26日 22:17 本文熱度 5123

數(shù)據(jù)表Order.dbo結(jié)構(gòu):
OrderNo varchar(10), ---單號
GetDate Varchar(10), ---日期
Sales Varchar(10), ---員
ItemNo Varchar(20), ---產(chǎn)品號
Amount Smallint ---金額

數(shù)據(jù)記錄如下:
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,得到按月份統(tǒng)計的如下結(jié)果:
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 '業(yè)務(wù)員',
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 '業(yè)務(wù)員',
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

業(yè)務(wù)員 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

(所影響的行數(shù)為 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 出售數(shù)量 else 0 end) as '9月',
sum(case when substring(convert(varchar(7),日期,120),6,2)='10' then 出售數(shù)量 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 出售數(shù)量 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 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 国产精品沙发 | 国产天堂一区 | 国产肥熟| 成人欧美在线观看 | 国产午夜在 | 国产精品极品美 | 97色精品视频在 | 国产精品人在线观看 | 国产v亚| 成人午夜在线视频网 | 91免费观看视频 | 日韩精品动漫一区 | 绿帽在线 | 午夜成人看片 | 91极品美 | 国产一区二区小电影 | 日韩精品欧美一区色 | 精品国产2021| 国内视频日韩顶级片 | 国产重口| 国产精品免费小视频 | 欧美中文综合在线 | 国产在线观看不卡 | 国产精品夜夜嗨 | 国产网站大全 | 国产12页 | 国产精品电影久 | 国产精品自拍16页 | 精品不卡一区二区 | 精品欧美 | 日韩午夜视频欧美 | 日本最猛xxxxx | 午夜91福利 | 精品国产不卡女 | 成人精品一区二区三 | 日本免费一级视频 | 国产92成人 | 国产区日韩区欧美区 | 日本+国产+高清 | 午夜亚洲一区二区福 | 91视频h|