狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

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

使用動態(tài)SQL語句實現(xiàn)簡單的行列轉(zhuǎn)置(動態(tài)產(chǎn)生列)

Ccoffee
2015年12月24日 10:4 本文熱度 7145
 原始數(shù)據(jù)如下圖所示:(商品的銷售明細(xì))

date=業(yè)務(wù)日期;Item=商品名稱;saleqty=銷售數(shù)量;

-- 建立測試數(shù)據(jù)(表)
create table test (Date varchar(10), item char(10),saleqty int)
insert test values(''2010-01-01'',''AAA'',8)
insert test values(''2010-01-02'',''AAA'',4)
insert test values(''2010-01-03'',''AAA'',5)
insert test values(''2010-01-01'',''BBB'',1)
insert test values(''2010-01-02'',''CCC'',2)
insert test values(''2010-01-03'',''DDD'',6)

需要實現(xiàn)的報表樣式:每一行既每一天,顯示所有商品(列)該天的銷售數(shù)量;

 

 

實現(xiàn)的方法和思路如下:

-- 實現(xiàn)結(jié)果的靜態(tài)SQL語句寫法
-- 整理報表需要的格式
select date,
case item when ''AAA'' then saleqty when null then 0 end as AAA,
case item when ''BBB'' then saleqty when null then 0 end as BBB,
case item when ''CCC'' then saleqty when null then 0 end as CCC,
case item when ''DDD'' then saleqty when null then 0 end as DDD
from test

 

 

  

-- 按日期匯總行
select date,
sum(case item when ''AAA'' then saleqty when null then 0 end) as AAA,
sum(case item when ''BBB'' then saleqty when null then 0 end) as BBB,
sum(case item when ''CCC'' then saleqty when null then 0 end) as CCC,
sum(case item when ''DDD'' then saleqty when null then 0 end) as DDD
from test 
group by date

 

 

 

 

-- 處理數(shù)據(jù):將空值的欄位填入數(shù)字0;
select date,
isnull (sum(case item when ''AAA'' then saleqty end),0) as AAA,
isnull (sum(case item when ''BBB'' then saleqty end),0) as BBB,
isnull (sum(case item when ''CCC'' then saleqty end),0) as CCC,
isnull (sum(case item when ''DDD'' then saleqty end),0) as DDD
from test 
group by date

 

靜態(tài)SQL語句編寫完成!

-- 需要動態(tài)實現(xiàn)的SQL部分
isnull (sum(case item when ''AAA'' then saleqty end),0) as AAA,
isnull (sum(case item when ''BBB'' then saleqty end),0) as BBB,
isnull (sum(case item when ''CCC'' then saleqty end),0) as CCC,
isnull (sum(case item when ''DDD'' then saleqty end),0) as DDD

-- 動態(tài)語句的實現(xiàn)
select ''isnull (sum(case item when ''''''+item+'''''' then saleqty end),0) as [''+item+'']'' 
from (select distinct item from test) as a
-- 這一步很關(guān)鍵:利用結(jié)果集給變量賦值;

-- 完成!
declare @sql varchar(8000)
set @sql = ''select Date''
select @sql = @sql + '',isnull (sum(case item when ''''''+item+'''''' then saleqty end),0) as [''+item+'']'' 
from (select distinct item from test) as a
select @sql = @sql+'' from test group by date''
exec(@sql)

-- 刪除測試數(shù)據(jù)(表)
drop table test


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