轉(zhuǎn)載的,能用!
目前成都創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、酉陽土家族苗族網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
------------
CREATE PROCEDURE Pagination
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 10, -- 頁(yè)尺寸
@PageIndex int = 1, -- 頁(yè)碼
@doCount bit = 0, -- 返回記錄總數(shù), 非 0 值則返回
@OrderType bit = 0, -- 設(shè)置排序類型, 非 0 值則降序
@strWhere varchar(1500) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @d datetime
set @d=getdate()
declare @strSQL varchar(5000) -- 主語句
declare @strTmp varchar(110) -- 臨時(shí)變量
declare @strOrder varchar(400) -- 排序類型
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from ['+ @tblName +'] where '+ @strWhere
else
set @strSQL = 'select count(*) as Total from ['+ @tblName +']'
end
--以上代碼的意思是如果@doCount傳遞過來的不是0,就執(zhí)行總數(shù)統(tǒng)計(jì)。以下的所有代碼都
--是@doCount為0的情況
else
begin
if @OrderType != 0
begin
set @strTmp = '(select min'
set @strOrder = ' order by ['+ @fldName +'] desc'
--如果@OrderType不是0,就執(zhí)行降序,這句很重要!
end
else
begin
set @strTmp = '(select max'
set @strOrder = ' order by ['+ @fldName +'] asc'
end
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName +'] where ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName +'] '+ @strOrder
--如果是第一頁(yè)就執(zhí)行以上代碼,這樣會(huì)加快執(zhí)行速度
end
else
begin
--以下代碼賦予了@strSQL以真正執(zhí)行的SQL代碼
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName +'] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + '])
from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] from ['+ @tblName +']' + @strOrder + ') as tblTmp)'+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName +'] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + ']
from ['+ @tblName +'] where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
exec ( @strSQL)
select datediff(ms,@d,getdate()) as 查詢時(shí)間毫秒
go
Pagination 'CopyrightInformation','*','id',120,45667,0,0,''
可以完成,思路如下:
通過pid 商品編碼分組,得到銷售日期的每個(gè)月列,后用sum(case?Fact_m?WHEN?月份 then?數(shù)量end)來操作即可.
--年度售額:
select?pid?商品編碼
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'01'?then?cCost?end)),0)?'countCost_1'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'02'?then?cCost?end)),0)?'countCost_2'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'03'?then?cCost?end)),0)?'countCost_3'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'04'?then?cCost?end)),0)?'countCost_4'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'05'?then?cCost?end)),0)?'countCost_5'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'06'?then?cCost?end)),0)?'countCost_6'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'07'?then?cCost?end)),0)?'countCost_7'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'08'?then?cCost?end)),0)?'countCost_8'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'09'?then?cCost?end)),0)?'countCost_9'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'10'?then?cCost?end)),0)?'countCost_10'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'11'?then?cCost?end)),0)?'countCost_11'
,isnull(convert(dec(18,2),sum(case?Fact_m?WHEN?'12'?then?cCost?end)),0)?'countCost_12'
,isnull(convert(dec(18,2),sum(cCost)))?'countTotal'?
from?Tab?group?by?pid?
--Fact_m?指的就是銷售日期[月],最后還有一列為年度總計(jì)
希望能幫到你!
符合第一范式(原子性),1NF;
存在組合鍵(商店編號(hào),商品編號(hào)) ,?商店編號(hào)-(商店名稱,商店地址)
商品編號(hào)-(銷售數(shù)量,銷售價(jià)格,商品名稱,商品產(chǎn)地)不符合2NF。會(huì)產(chǎn)生數(shù)據(jù)冗余。
修改:商店表(商店編號(hào),商店名稱,商店地址) 商品表(商品編號(hào),銷售數(shù)量,銷售價(jià)格,商品名稱,商品產(chǎn)地) 這兩個(gè)是多對(duì)多的關(guān)系所以 建立中間表 (出貨編號(hào),商品編號(hào),商店編號(hào))