第五十九回 许褚裸衣斗马超 曹操抹书间韩遂第六十回 张永年反难杨修 庞士元议取西蜀
益阳网站建设公司创新互联,益阳网站设计制作,有大型网站制作公司丰富经验。已为益阳成百上千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的益阳做网站的公司定做!
中位数求法:(一)把排好序的数据按照“首尾成对”依次去掉,剩下的最后一个数据即为这组数据的中位数。(二)设一组数据共有n个数据。
当n为奇数时,这组数据中第“(n+1)/2”个数据即为这组数据的中位数;当n为偶数时,这组数据中第“n/2”个数据和第“(n+2)/2”个数据的和一半,即为这组数据中位数。
中位数,是把一组数据按照大小排序(为从小到大的顺序)后,居于这组数据的“中间”位置的数。求一组数据的中位数,一定要先排序,再求值。
当一组数据共有奇数个数时,它的中位数一定在这组数据中。当一组数据共有偶数个数时,它的中位数不一定在这组数据中。当一组数据中的每个数据都完全相同时,不管这组数据有奇数个还是偶数个,它的中位数都在这组数据中,这组数据中的任何一个数都可以作为中位数。
举个例子,当原始数据为:30、10、20、70、60时,原始数据的总数是5,为奇数,将原始数据按大小顺序排列得到数据:10、20、30、60、70,那么该数据的中位数的位置为(5+1)÷2=3,中位数既为顺序排列数据的第3位数字30。
有点复杂,在你基础上加了条有奇数的数据
创建表,插入数据:
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);
执行:
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
结果:
其中:
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
这条是主语句,主要是按照大小给出一个排名,然后根据中位数的公式,偶数的话,取最中间两个的平均数,奇数取最中间的数。自己研究一下吧。