Sql交叉表簡單實現

2021-06-16 02:53:05 字數 3662 閱讀 2002

最近碰到一需求,是醬紫的: 乙個銷售商品表, 每年每月每天都賣出n種不同種類的商品, 最後要統計每月每種商品的銷售額. 或每年的, 在此我模擬建立了乙個簡單的表, 主要說明經驗和大家一塊分享, 若發現有問題, 歡迎隨時指教.

如下:(此表過於簡單, 主要用於說明交叉表)

按某一年的多月查詢結果如下:

多年對比查詢結果如下:

具體實現**如下:

----------建立乙個交叉表--------------

create table salesrecord (

id int primary key identity(1,1) not null,

[date] datetime not null,

[goods] varchar(10) not null,

[money] decimal(10,2) default 0

)----------向交

叉表中插入記錄--------------

insert into salesrecord values('2009-3-2','顯示器',2300)

insert into salesrecord values('2009-1-3','鍵盤',30)

insert into salesrecord values('2009-3-2','機箱',200)

insert into salesrecord values('2009-7-2','滑鼠',55)

insert into salesrecord values('2009-12-2','硬碟',455)

insert into salesrecord values('2009-9-2','屏保膜',10)

insert into salesrecord values('2009-4-28','硬碟',455)

insert into salesrecord values('2009-2-9','電源線',15)

insert into salesrecord values('2009-3-5','主機板',605)

insert into salesrecord values('2009-8-15','攝像頭',120)

insert into salesrecord values('2009-5-18','u盤',105)

insert into salesrecord values('2008-5-28','u盤',105)

insert into salesrecord values('2009-10-11','u盤',80)

-------建立訪問交叉表的查詢語句生成函式---(商品種類赿多,查出出的表字段就赿多)--------------

/* * @year 某一年

* @ym 有可能是年 有可能是月, 看後邊的@flag決定

* @month 某一月

* @flag 對前邊引數的說明標記.

*   如果 yy 則某年到某年查詢,前兩個引數為開始年,結束年,第三個引數無效(但仍然需要傳入任意整型值)

*   如果ymm 某一年從幾月到幾月查詢 ,第乙個引數為某年, 後兩個引數為開始月和結束月.

* @returns 返回組裝後的字串.

*/create function getquerystr (@y int,@ym int,@m int,@flag varchar(3))

returns varchar(7000)

asbegin

declare @tempstr varchar(20)

declare @rtnstr varchar(7000)

declare @condstr varchar(500)

--判斷如果是yy則是查某一年到某一年,組裝查詢串及條件

if(@flag='yy')

begin

set @rtnstr=' select year(date)  as ''年份'', '

set @condstr=' year(date)>= '+cast(@y as varchar) +' and year(date)<= '+cast(@ym as varchar)+' group by year(date)'

endelse if(@flag='yym')

--否則查某一年的某一月到某一月,組裝查詢串及條件

begin

set @rtnstr=' select cast(month(date) as varchar)+''月'' as ''月份'', '

set @condstr=' year(date)= '+cast(@y as varchar)+' and month(date)>= '+cast(@ym as varchar)+' and  month(date)<= '+cast(@m as varchar)+' group by month(date)'

end

declare getone cursor

for

select distinct goods from salesrecord

open getone

fetch next from getone into @tempstr

while @@fetch_status=0

begin

set @rtnstr = @rtnstr+' sum(case goods when

'''+@tempstr+'''

then money else 0 end) as

'''+@tempstr+''','

fetch next from getone into @tempstr

endclose getone

deallocate getone

set @rtnstr=left(@rtnstr,len(@rtnstr)-1)

set @rtnstr=@rtnstr+' from salesrecord where

'+@condstr

return @rtnstr

end

----------按多年查詢------------

declare @str varchar(7000)

set @str=dbo.getquerystr(2008,2009,0,'yy');

exec(@str)

----------按一年多月查詢------------

declare @str varchar(7000)

set @str=dbo.getquerystr(2009,0,12,'yym');

exec(@str)

----------查詢下源表中的資料對比下------------

select * from salesrecord order by date

---------如果不用了可以刪除函式和表--------------

drop function getquerystr

drop table salesrecord

SQL實現動態交叉表 用游標實現

set quoted identifier on goset ansi nulls on goalter procedure crosstable strtablename as varchar 50 查詢表 strcol as varchar 50 strgroup as varchar 50 分...

SQL動態交叉表

動態交叉表就是列表可以根據表中資料的情況動態建立列。動態查詢不能使用 select 語句實現,它可以利用儲存過程實現。思路是 首先檢索列頭資訊,形成乙個游標,然後遍歷游標,將上面靜態交叉表實現過程中使用 case 語句判斷的內容用游標裡的值替代,形成一條新的 sql查詢語句,然後執行並返回結果。下面...

SQL表交叉連線

mssql文件中也叫交叉聯接.好比 a表裡面的a,b,c 查詢符合a x 條件的.b表裡面 d,e,f 查詢符合 d 1 語句 select a.a,a.b,a.c,b.d,b.e b.f from a,b where a.a x b.d 1 這個是錯的.錯在 怎麼改?把 where裡面的,改為an...