MSSQL求中位數 常見解決方案 整理帖

2021-09-30 08:15:52 字數 3094 閱讀 1693

--> title   : mssql求中位數-常見解決方案-整理帖

--環境描述:

按id和datevalue排序,希望能取到每乙個的中間值,

若id總數為奇數,取第(n+1)/2個的值;如果是偶數,取第n/2個的值

if object_id('[tb]') is not null drop table [tb]

gocreate table [tb] (id int,datevalue numeric(7,4))

insert into [tb]

select 1,1.0000 union all

select 1,5.0000 union all

select 1,6.0000 union all

select 2,9.0000 union all

select 2,20.0000 union all

select 2,35.0000 union all

select 2,60.0000 union all

select 3,72.0000 union all

select 3,99.0000 union all

select 3,100.0000 union all

select 3,20.0000 union all

select 3,6.0000 union all

select 4,6.0000 union all

select 4,12.0000 union all

select 5,10.0000

--方法1

if object_id('tempdb..#')is not null drop table #

select *,px=(select count(*) from tb where id=t.id and datevalue<=t.datevalue) into # 

from tb t

select distinct

id,datevalue=(select datevalue from # where id=t.id and px=((select max(px) from # where id=t.id)+1)/2)

from # t

id          datevalue

1           5.0000

2           20.0000

3           72.0000

4           6.0000

5           10.0000

(5 個資料列受到影響)

--方法2

select * 

from tb t 

where (select count(*) from tb where id=t.id and datevalue<=t.datevalue)=

((select count(*) from tb where id=t.id)+1)/2

id          datevalue

1           5.0000

2           20.0000

3           72.0000

4           6.0000

5           10.0000

(5 個資料列受到影響)

--方法3

;with cte as 

select n=row_number() over(partition by id order by datevalue),

id,datevalue

from tb

select cte.id,

cte.datevalue

from cte,

select n=case when max(n)%2=1 then max(n)/2+1 else max(n)/2 end,

idfrom cte group by id)n

where cte.id=n.id and cte.n=n.n

id          datevalue

1           5.0000

2           20.0000

3           72.0000

4           6.0000

5           10.0000

(5 個資料列受到影響)

--方法

select id,

datevalue

from

select 

(select count(*) from tb where id=t.id and datevalue<=t.datevalue)px,

(select ceiling(count(*)*1.0/2)from tb where id=t.id)cnt,

id,datevalue

from tb t

)tt where cnt=px 

id          datevalue

1           5.0000

2           20.0000

3           72.0000

4           6.0000

5           10.0000

(5 個資料列受到影響)

--方法5

select id, 

datevalue 

from

(select *, 

rid=row_number() over (partition by id order by datevalue), 

cnt=count(1) over (partition by  id) from 

tb) t

where rid=(case when cnt%2=1 then (cnt+1)/2 else cnt/2 end)

id          datevalue

1           5.0000

2           20.0000

3           72.0000

4           6.0000

5           10.0000

(5 個資料列受到影響)

MSSQL編號重排常見解決方案

title mssql 編號重排常見解決方案 author wufeng4552 date 2009 11 25 if object id tb is notnull drop table tb go create table tb id int id1 nvarchar 6 id2 sql var...

中文亂碼常見解決方案

亂碼問題是困擾很多程式設計師的問題,為什麼別人的機器是正常的,我的是亂碼的,為什麼本地是正常的,測試環境是亂碼,生產環境是亂碼?注入此類的問題,我們該怎麼解決這個亂碼問題?本文章不斷更新,將自己遇到的,丟擲來,有則改之,無則加勉。首先,明確一點 計算機記憶體中,統一使用unicode進行編碼。既然有...

MSSQL求連續ID內數量合計 常見解決方案

title mssql求連續id內數量合計 常見解決方案 author wufeng4552 date 2009 12 04 if object id tb is not null drop table tb gocreate table tb id varchar 10 num decimal 1...