新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
mysql的位函数,就是将数字转换成2进制,各位求与。举个例子2915 结果是13。29的二进制是11101,15的二进制是1111,位运算 11101+01111------------ 0110101101的十进制是13。在Oracle里面是BITAND(nExpression1, nExpression2) 参数 nExpression1, nExpression2 指定按位进行 AND 运算的两个数值。这个函数进行位运算,MySQL我没怎么用,希望有帮助
目前创新互联已为成百上千的企业提供了网站建设、域名、网站空间、网站运营、企业网站设计、聊城网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
score[i]/10是整除,结果保留整数部分,比如9x/10=9,8x/10=8,因此case 9表示score[i]的值是90~99,case 8表示score[i]的值是80~89,以此类推。
中位数:也就是选取中间的数。一种衡量集中趋势的方法。
要找中位数,首先需要从小到大排序,例如这组数据:23、29、20、32、23、21、33、25;
我们将数据排序20、21、23、23、25、29、32、33;排序后发现有8个数怎么办?
若有n个数,n为奇数,则选择第(n+1)/2个为中位数,若n为偶数,则中位数是(n/2以及n+1/2)的平均数
此例中选择24为中位数
算总数,排序,区四分之一处的数,可以在查询语句中使用产量+1来标志排序号,再嵌套查询
有点复杂,在你基础上加了条有奇数的数据
创建表,插入数据:
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
这条是主语句,主要是按照大小给出一个排名,然后根据中位数的公式,偶数的话,取最中间两个的平均数,奇数取最中间的数。自己研究一下吧。
第五十九回 许褚裸衣斗马超 曹操抹书间韩遂第六十回 张永年反难杨修 庞士元议取西蜀