乙個使用pivot的儲存過程

2021-07-23 14:48:43 字數 1687 閱讀 6265

alter procedure [dbo].[p_getgameconsumebytype] 

-- add the parameters for the stored procedure here

@dtbegin datetime,

@dtend datetime,

@ngametype int

asbegin

-- set nocount on added to prevent extra result sets from

-- interfering with select statements.

set nocount on;

with tbl as(

select case

when goldcount <= 40000 then '40000'

when goldcount between 40001 and 50000 then '50000'

when goldcount between 50001 and 60000 then '60000'

when goldcount between 60001 and 70000 then '70000'

when goldcount between 70001 and 80000 then '80000'

when goldcount between 80001 and 90000 then '90000'

when goldcount between 90001 and 100000 then '100000'

when goldcount between 100001 and 110000 then '110000'

case

when moneycount <= 200 then '200'

when moneycount between 201 and 400 then '400'

when moneycount between 401 and 600 then '600'

when moneycount between 601 and 800 then '800'

when moneycount between 801 and 1000 then '1000'

when moneycount between 1001 and 3000 then '3000'

when moneycount between 3001 and 5000 then '5000'

when moneycount between 5001 and 7000 then '7000'

from t_gamemoneyconsume with(nolock) where createtime between @dtbegin and @dtend and gametype = @ngametype

),tblstat1 as()

union all

union all

union all

union all

union all

union all

union all

union all

end

最終的到的是乙個二維表,第一列列標題是 xx數, 後面的列標題依次是 40000~110000+,可以自己針對這個儲存過程建個表填入資料執行看下結果

乙個儲存過程

create or replace package abc zys is procedure daily census end abc zys 建乙個包,包中有儲存過程daily census。不涉及任何引數。create or replace package body abc zys is pro...

乙個儲存過程

首先是建立儲存過程.drop procedure if exists externalcalltocomplete delimiter create procedure externalcalltocomplete begin drop table if exists temp1 create ta...

PIVOT的乙個小應用

之前學習case when來實現行列轉換的方法,但是今天學習到了pivot的方法,記錄分享下,園子里大牛多多,甚是受益 select provinceid,中石油,中石化,中海油,社會單位 這裡要注意的是對漢字不需要加單引號,這裡是選出來所需要的列 from select provinceid,na...