mysql的位函數(shù),就是將數(shù)字轉(zhuǎn)換成2進(jìn)制,各位求與。舉個例子2915 結(jié)果是13。29的二進(jìn)制是11101,15的二進(jìn)制是1111,位運算 11101+01111------------ 0110101101的十進(jìn)制是13。在Oracle里面是BITAND(nExpression1, nExpression2) 參數(shù) nExpression1, nExpression2 指定按位進(jìn)行 AND 運算的兩個數(shù)值。這個函數(shù)進(jìn)行位運算,MySQL我沒怎么用,希望有幫助
“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是創(chuàng)新互聯(lián)建站的服務(wù)宗旨!把網(wǎng)站當(dāng)作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個不僅審美在線,而且實用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對成都網(wǎng)站設(shè)計、網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)頁設(shè)計、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)推廣、探索永無止境。
中位數(shù):也就是選取中間的數(shù)。一種衡量集中趨勢的方法。
要找中位數(shù),首先需要從小到大排序,例如這組數(shù)據(jù):23、29、20、32、23、21、33、25;
我們將數(shù)據(jù)排序20、21、23、23、25、29、32、33;排序后發(fā)現(xiàn)有8個數(shù)怎么辦?
若有n個數(shù),n為奇數(shù),則選擇第(n+1)/2個為中位數(shù),若n為偶數(shù),則中位數(shù)是(n/2以及n+1/2)的平均數(shù)
此例中選擇24為中位數(shù)
把所有的同類數(shù)據(jù)按照大小的順序排列。如果數(shù)據(jù)的個數(shù)是奇數(shù),則中間那個數(shù)據(jù)就是這群數(shù)據(jù)的中位數(shù)。
如果數(shù)據(jù)的個數(shù)是偶數(shù),則中間那2個數(shù)據(jù)的算術(shù)平均值就是這群數(shù)據(jù)的中位數(shù)。示例如下:找出這組數(shù)據(jù):23、29、20、32、23、21、33、25的中位數(shù)。解:首先將該組數(shù)據(jù)進(jìn)行排列(這里按從小到大的順序),得到:20、21、23、23、25、29、32、33因為該組數(shù)據(jù)一共由8個數(shù)據(jù)組成,即n為偶數(shù),故按中位數(shù)的計算方法,得到中位數(shù)24,即第四個數(shù)和第五個數(shù)的平均數(shù)。
有點復(fù)雜,在你基礎(chǔ)上加了條有奇數(shù)的數(shù)據(jù)
創(chuàng)建表,插入數(shù)據(jù):
create?table?test
(cat_id?int,
price?int);
insert?into?test?values?(101,90);
insert?into?test?values?(101,99);
insert?into?test?values?(102,98);
insert?into?test?values?(103,96);
insert?into?test?values?(102,95);
insert?into?test?values?(102,94);
insert?into?test?values?(102,93);
insert?into?test?values?(103,99);
insert?into?test?values?(103,98);
insert?into?test?values?(103,97);
insert?into?test?values?(104,96);
insert?into?test?values?(104,95);
insert?into?test?values?(105,97);
insert?into?test?values?(105,96);
insert?into?test?values?(105,95);
執(zhí)行:
SELECT
t1.cat_id,
round(avg(t1.price),?1)?price
FROM
(
SELECT
*
FROM
(
SELECT
t.cat_id,
t.price,
count(*)?AS?rank
FROM
test?t
LEFT?OUTER?JOIN?test?r?ON?t.cat_id?=?r.cat_id
AND?t.price?=?r.price
GROUP?BY
t.cat_id,
t.price
ORDER?BY
t.cat_id,
t.price?DESC
)?s
)?t1,
(
SELECT?DISTINCT
a.cat_id,
round(a.maxrank?/?2)?rank
FROM
(
SELECT
cat_id,
max(rank)?maxrank,
MOD?(max(rank),?2)?modrank
FROM
(
SELECT
*
FROM
(
SELECT
t.cat_id,
t.price,
count(*)?AS?rank
FROM
test?t
LEFT?OUTER?JOIN?test?r?ON?t.cat_id?=?r.cat_id
AND?t.price?=?r.price
GROUP?BY
t.cat_id,
t.price
ORDER?BY
t.cat_id,
t.price?DESC
)?s
)?t1
GROUP?BY
cat_id
)?a,
(
SELECT
*
FROM
(
SELECT
t.cat_id,
t.price,
count(*)?AS?rank
FROM
test?t
LEFT?OUTER?JOIN?test?r?ON?t.cat_id?=?r.cat_id
AND?t.price?=?r.price
GROUP?BY
t.cat_id,
t.price
ORDER?BY
t.cat_id,
t.price?DESC
)?s
)?b
WHERE
a.cat_id?=?b.cat_id
AND?a.modrank?=?0
UNION?ALL
SELECT?DISTINCT
a.cat_id,
round(a.maxrank?/?2)?+?1?rank
FROM
(
SELECT
cat_id,
max(rank)?maxrank,
MOD?(max(rank),?2)?modrank
FROM
(
SELECT
*
FROM
(
SELECT
t.cat_id,
t.price,
count(*)?AS?rank
FROM
test?t
LEFT?OUTER?JOIN?test?r?ON?t.cat_id?=?r.cat_id
AND?t.price?=?r.price
GROUP?BY
t.cat_id,
t.price
ORDER?BY
t.cat_id,
t.price?DESC
)?s
)?t1
GROUP?BY
cat_id
)?a,
(
SELECT
*
FROM
(
SELECT
t.cat_id,
t.price,
count(*)?AS?rank
FROM
test?t
LEFT?OUTER?JOIN?test?r?ON?t.cat_id?=?r.cat_id
AND?t.price?=?r.price
GROUP?BY
t.cat_id,
t.price
ORDER?BY
t.cat_id,
t.price?DESC
)?s
)?b
WHERE
a.cat_id?=?b.cat_id
AND?a.modrank?=?0
UNION?ALL
SELECT?DISTINCT
a.cat_id,
round(a.maxrank?/?2)?rank
FROM
(
SELECT
cat_id,
max(rank)?maxrank,
MOD?(max(rank),?2)?modrank
FROM
(
SELECT
*
FROM
(
SELECT
t.cat_id,
t.price,
count(*)?AS?rank
FROM
test?t
LEFT?OUTER?JOIN?test?r?ON?t.cat_id?=?r.cat_id
AND?t.price?=?r.price
GROUP?BY
t.cat_id,
t.price
ORDER?BY
t.cat_id,
t.price?DESC
)?s
)?t1
GROUP?BY
cat_id
)?a,
(
SELECT
*
FROM
(
SELECT
t.cat_id,
t.price,
count(*)?AS?rank
FROM
test?t
LEFT?OUTER?JOIN?test?r?ON?t.cat_id?=?r.cat_id
AND?t.price?=?r.price
GROUP?BY
t.cat_id,
t.price
ORDER?BY
t.cat_id,
t.price?DESC
)?s
)?b
WHERE
a.cat_id?=?b.cat_id
AND?a.modrank?=?1
)?t2
WHERE
t1.cat_id?=?t2.cat_id
AND?t1.rank?=?t2.rank
GROUP?BY
t1.cat_id
結(jié)果:
其中:
select?*?from?(??
select?t.cat_id,t.price,count(*)?as?rank?from?test?t??
LEFT?OUTER?JOIN?test?r??
on?t.cat_id?=?r.cat_id??
and?t.price=r.price??
group?by?t.cat_id,t.price??
order?by?t.cat_id,?t.price?desc??
)?s
這條是主語句,主要是按照大小給出一個排名,然后根據(jù)中位數(shù)的公式,偶數(shù)的話,取最中間兩個的平均數(shù),奇數(shù)取最中間的數(shù)。自己研究一下吧。
文章名稱:mysql中位數(shù)怎么算 mysql計算中位數(shù)
網(wǎng)址分享:http://m.newbst.com/article48/doppcep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、搜索引擎優(yōu)化、網(wǎng)站策劃、網(wǎng)站排名、微信公眾號、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)