日韩欧美人妻无码精品白浆,夜夜嗨AV免费入口,国产欧美官网在线看,高校回应聋哑女生因长相完美被质疑

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

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

Ccoffee
2015年12月24日 10:4 本文熱度 7020
 原始數(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)

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

 

 

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

-- 實(shí)現(xiàn)結(jié)果的靜態(tài)SQL語句寫法
-- 整理報(bào)表需要的格式
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語句編寫完成!

-- 需要?jiǎng)討B(tài)實(shí)現(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

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