mysql怎么同时计算的简单介绍
在MySQL中,如何同时计算多组数据的中位数
有点复杂,在你基础上加了条有奇数的数据
成都创新互联是专业的麦积网站建设公司,麦积接单;提供网站设计制作、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行麦积网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
创建表,插入数据:
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
这条是主语句,主要是按照大小给出一个排名,然后根据中位数的公式,偶数的话,取最中间两个的平均数,奇数取最中间的数。自己研究一下吧。
mysql如何使TIMEDIFF计算结果与条件同时显示
sekect *,TIMEDIFF(b,c) AS '结果' FROM a
或
sekect b,c,TIMEDIFF(b,c) AS '结果' FROM a
mysql 如何查询计算指定字段到当前日期之间的天数 同时查询其他字段
datediff
DATEDIFF(expr,expr2)
DATEDIFF()
返回起始时间
expr和结束时间expr2之间的天数。Expr和expr2
为日期或
date-and-time
表达式。计算中只用到这些值的日期部分。
mysql
SELECT
DATEDIFF('1997-12-31
23:59:59','1997-12-30');
-
1
mysql
SELECT
DATEDIFF('1997-11-30
23:59:59','1997-12-31');
-
-31
select
Datediff(列名,列名)
as
datenum
from
表名
文章名称:mysql怎么同时计算的简单介绍
URL地址:http://hbruida.cn/article/doosjis.html