又乙個SQL的piovt的經典小例子

2021-06-16 09:27:49 字數 3198 閱讀 6101

參考:

create table test2(id int,name varchar(20),quarter char,profile1 int)

insert into test2 values(1,'a',1,1000)

insert into test2 values(1,'a',2,2000)

insert into test2 values(1,'a',3,4000)

insert into test2 values(1,'a',4,5000)

insert into test2 values(2,'b',1,3000)

insert into test2 values(2,'b',2,3500)

insert into test2 values(2,'b',3,4200)

insert into test2 values(2,'b',4,5500)

select * from test2

select id,name

,[1] as 一季度

,[2] as 二季度

,[3] as 三季度

,[4] as 四季度

from test2

pivot(count([profile1])for [quarter] in ([1],[2],[3],[4])) pvt

自己寫的:

--海爾專案當天抓取資料統計指令碼

declare @cureent varchar(30)

--抓取總數量

select @cureent=convert(varchar(30),getdate(),101)

select getdate() as 時間

,count(1)as 抓取總數量

from [sec_item]

where brand_id=20002 and [item_cdate]>= @cureent

--正常與極速抓取總數量

select [0] as [正常抓取(總數)],[1]as [極速抓取(總數)] from

(select count(1) as 數量 ,isfsearchengine

from [sec_item]

where brand_id=20002 and [item_cdate]>=@cureent group by isfsearchengine

) as a

pivot(sum(數量)for isfsearchengine in ([0],[1])) pvt

--詳細列表

select [0] as [正常抓取(數量)]

,[1] as [極速抓取(數量)]

,case item_state when 0 then '未分揀' when 1 then '已分揀' when 2 then '正在分揀' when 4 then '已放棄' end as [分揀情況]

from

(select count(1) as 數量 ,isfsearchengine,item_state

from [sec_item]

where brand_id=20002 and [item_cdate]>=@cureent group by isfsearchengine,item_state

) as a

pivot(sum(數量)for isfsearchengine in ([0],[1])) pvt

go

日期的行列轉換

go

--本週的極速的分揀情況

declare @cureent varchar(30)

select @cureent='2012-11-12' --周一的日期

declare @datetable table ([date] datetime )

declare @isfsearchengine nvarchar(max)

declare @datas nvarchar(max)

declare @rows nvarchar(max)

declare @sql nvarchar(max)

declare @num int

set @isfsearchengine='1' --0:非極速、1:極速

set @num=0

set @sql=''

while @num<5

begin

insert into @datetable values( convert(varchar(11), dateadd(day,@num,@cureent),120))

set @num=@num+1

endselect @datas=isnull(@datas+',','')+'['+convert(varchar(11),[date],120)+']' from @datetable

select @rows=isnull(@rows+',','')+'isnull(['+convert(varchar(11),[date],120)+'],0) as ['+convert(varchar(11),[date],120)+']' from @datetable

--select @datas

--select @rows

set @sql=@sql+'select '

+@rows

+',case item_state when 0 then ''未分揀'' when 1 then ''已分揀'' when 2 then ''正在分揀'' when 4 then ''已放棄'' end as [分揀情況]

from

( select count(1)as 數量,convert(varchar(11),[item_cdate],120) as [date],[item_state]

from [sec_item]

where brand_id=20002 and [item_cdate]>='+@cureent+' and isfsearchengine='+@isfsearchengine

+'group by convert(varchar(11),[item_cdate],120),[item_state]

) as a pivot(sum(數量)for [date] in ('+@datas+')) pvt'

print @sql

exec(@sql)

又乙個迷宮

有乙個迷宮,迷宮裡有乙個人,迷宮是規格為 n n 的方格,房內特定位置上有障礙物,人處於某一位置,可以選擇向上下左右方向前進,但是要保證面對的不是牆壁或是障礙物,否則無法向前行走。輸入迷宮的規格,乙個正整數 n 2 n 100 佔一行,代表矩陣大小 輸入方形矩陣 n 行 n 列,由 0 和 1 組成...

又乙個通宵

敲了一晚上的 閒下來摸魚寫了下字是孟浩然的 宿業師山房期丁大不至 宿業師山房待丁大不至 夕陽度西嶺,群壑倏已暝。松月生夜涼,風泉滿清聽。樵人歸欲盡,煙鳥棲初定。之子期宿來,孤琴候蘿徑。描寫詩人在山中等候遲遲未到的友人的場景 可以看看古詩詞網的賞析 傳送門 想起來這首詩也是我與師弟最後一起寫的呢 雖說...

又乙個amfext的bug

今天又發現乙個amfext的bug,請看如下資料結構 array va player login array 0 1 array 1319618220,這個資料結構用amfext是可以encode的,但是它 自己encode出來的結果,自己無法decode,原因出在了 使用了乙個空字串作為key,根...