第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

求問在MySQL中,我該如何同時(shí)計(jì)算多組數(shù)據(jù)的中位數(shù)?

求問在MySQL中,我該如何同時(shí)計(jì)算多組數(shù)據(jù)的中位數(shù)?

阿波羅的戰(zhàn)車 2022-05-13 15:11:31
以cat_id分組求各相應(yīng)分組price的中位數(shù),即如何寫SQL語句同時(shí)求出cat_id:101組的中位數(shù);求出cat_id:102組的中位數(shù);求出cat_id:103組的中位數(shù);求出cat_id:104組的中位數(shù)?謝謝
查看完整描述

1 回答

?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

有點(diǎn)復(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) priceFROM    (        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    ) t2WHERE    t1.cat_id = t2.cat_idAND t1.rank = t2.rankGROUP 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

這條是主語句,主要是按照大小給出一個(gè)排名,然后根據(jù)中位數(shù)的公式,偶數(shù)的話,取最中間兩個(gè)的平均數(shù),奇數(shù)取最中間的數(shù)。自己研究一下吧。



查看完整回答
反對(duì) 回復(fù) 2022-05-16
  • 1 回答
  • 0 關(guān)注
  • 260 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)