首页 新闻 会员 周边 捐助

我这个mysql语句如何简化呀???

0
悬赏园豆:30 [待解决问题]
SELECT
    bb.yhpcbh,
    bb.yhxm,
    bb.jmsl,
    bb.jmcb,
    bb.jmje,
    bb.xlsl,
    ROUND(bb.xlcbdj, 2) AS xlcbdj,
    bb.xlsje,
    bb.zlsl,
    ROUND(bb.zlcbdj, 2) AS zlcbdj,
    bb.zlsje,
    bb.dlsl,
    ROUND(bb.dlcbdj, 2) AS dlcbdj,
    bb.dlsje,
    ROUND(
        (bb.xlsje + bb.zlsje + bb.dlsje),
        2
    ) AS lcbze,
    ROUND(bb.zyf, 2) AS zyf,
    ROUND(bb.dyf, 2) AS dyf,
    ROUND(bb.jmje / bb.sszl, 2) AS jjmcb,
    ROUND(
        (bb.xlsje + bb.zlsje + bb.dlsje) / bb.sszl,
        2
    ) AS jjlcb,
    ROUND(bb.zyf / bb.sszl, 2) AS jjycb,
    ROUND(bb.dyf / bb.sszl, 2) AS jjdyfcb,
    ROUND(
        (
            bb.jmje / bb.sszl + (bb.xlsje + bb.zlsje + bb.dlsje) / bb.sszl + bb.zyf / bb.sszl + bb.dyf / bb.sszl
        ),
        2
    ) AS jjzjcb,
    ROUND(bb.jmje / bb.sszs, 2) AS zjmcb,
    ROUND(
        (bb.xlsje + bb.zlsje + bb.dlsje) / bb.sszs,
        2
    ) AS zjlcb,
    ROUND(bb.zyf / bb.sszs, 2) AS zjycb,
    ROUND(bb.dyf / bb.sszs, 2) AS zjdyfcb,
    ROUND(
        (
            bb.jmje / bb.sszs + (bb.xlsje + bb.zlsje + bb.dlsje) / bb.sszs + bb.zyf / bb.sszs + bb.dyf / bb.sszs
        ),
        2
    ) AS zjzjcb,
    ROUND(
        (
            bb.jmje / bb.sszl + (bb.xlsje + bb.zlsje + bb.dlsje) / bb.sszl + bb.zyf / bb.sszl + bb.dyf / bb.sszl
        ) * bb.sszl,
        2
    ) AS zcb,
    ROUND(
        bb.zsr - (
            (
                bb.jmje / bb.sszl + (bb.xlsje + bb.zlsje + bb.dlsje) / bb.sszl + bb.zyf / bb.sszl + bb.dyf / bb.sszl
            ) * bb.sszl
        ),
        2
    ) AS ml,
    ROUND(
        (
            bb.zsr - (
                (
                    bb.jmje / bb.sszl + (bb.xlsje + bb.zlsje + bb.dlsje) / bb.sszl + bb.zyf / bb.sszl + bb.dyf / bb.sszl
                ) * bb.sszl
            )
        ) / bb.sszs,
        2
    ) AS zml,
    bb.sszs,
    ROUND(bb.sszl, 2) AS sszl,
    ROUND(bb.xsjj, 2) AS xsjj,
    ROUND(bb.zsr, 2) AS zsr,
    ROUND(bb.ssjz, 2) AS ssjz
FROM
    (
        SELECT
            tmps.yhpcbh AS yhpcbh,
            tmps.yhxm AS yhxm,
            (
                SELECT
                    SUM(`tmpl`.`receivenumber`)
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`leadregistertype` = ''
            ) AS jmsl,
            ROUND(
                (
                    SELECT
                        SUM(
                            (
                                SELECT
                                    AVG(
                                        `tmpr`.purchasemoney / (
                                            COALESCE (`tmpr`.discount) + `tmpr`.purchasequantity
                                        )
                                    )
                                FROM
                                    `receiving` `tmpr`
                                WHERE
                                    `tmpr`.`mattercode` = `tmpl`.`materielcode`
                                AND `tmpr`.`receivingtime` = `tmpl`.`leadregistertime`
                            )
                        )
                    FROM
                        `leadregister` `tmpl`
                    WHERE
                        `tmpl`.`code` = `tmps`.`yhpcbh`
                    AND `tmpl`.leadregistertype = ''
                ),
                2
            ) AS jmcb,
            ROUND(
                (
                    SELECT
                        SUM(
                            (
                                `tmpl`.`receivenumber` * (
                                    SELECT
                                        AVG(
                                            `tmpr`.purchasemoney / (
                                                COALESCE (`tmpr`.discount, 0) + `tmpr`.purchasequantity
                                            )
                                        )
                                    FROM
                                        `receiving` `tmpr`
                                    WHERE
                                        `tmpr`.type = ''
                                    AND `tmpr`.`mattercode` = `tmpl`.`materielcode`
                                    AND `tmpr`.`receivingtime` = `tmpl`.`leadregistertime`
                                )
                            )
                        )
                    FROM
                        `leadregister` `tmpl`
                    WHERE
                        `tmpl`.`code` = `tmps`.`yhpcbh`
                    AND `tmpl`.leadregistertype = ''
                ),
                2
            ) AS jmje,
            (
                SELECT
                    SUM(`tmpl`.`receivenumber`)
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`materielgoods` = '小鸡料'
            ) AS xlsl,
            (
                SELECT
                    AVG(
                        (
                            SELECT
                                AVG(
                                    `tmpr`.purchasemoney / (
                                        COALESCE (`tmpr`.discount, 0) + `tmpr`.purchasequantity
                                    )
                                )
                            FROM
                                `receiving` `tmpr`
                            WHERE
                                `tmpr`.`mattercode` = `tmpl`.`materielcode`
                            AND `tmpr`.`receivingtime` <= tmpll.maxtime
                            AND `tmpr`.`receivingtime` >= tmpll.mintime
                        )
                    )
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`materielgoods` = '小鸡料'
            ) AS xlcbdj,
            (
                SELECT
                    SUM(
                        (
                            `tmpl`.`receivenumber` * (
                                SELECT
                                    AVG(
                                        `tmpr`.purchasemoney / (
                                            COALESCE (`tmpr`.discount, 0) + `tmpr`.purchasequantity
                                        )
                                    )
                                FROM
                                    `receiving` `tmpr`
                                WHERE
                                    `tmpr`.`mattercode` = `tmpl`.`materielcode`
                                AND `tmpr`.`receivingtime` <= tmpll.maxtime
                                AND `tmpr`.`receivingtime` >= tmpll.mintime
                            )
                        )
                    )
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`materielgoods` = '小鸡料'
            ) AS xlsje,
            (
                SELECT
                    SUM(`tmpl`.`receivenumber`)
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`materielgoods` = '中鸡料'
            ) AS zlsl,
            (
                SELECT
                    AVG(
                        (
                            SELECT
                                AVG(
                                    `tmpr`.purchasemoney / (
                                        COALESCE (`tmpr`.discount, 0) + `tmpr`.purchasequantity
                                    )
                                )
                            FROM
                                `receiving` `tmpr`
                            WHERE
                                `tmpr`.`mattercode` = `tmpl`.`materielcode`
                            AND `tmpr`.`receivingtime` <= tmpll.maxtime
                            AND `tmpr`.`receivingtime` >= tmpll.mintime
                        )
                    )
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`materielgoods` = '中鸡料'
            ) AS zlcbdj,
            (
                SELECT
                    SUM(
                        (
                            `tmpl`.`receivenumber` * (
                                SELECT
                                    AVG(
                                        `tmpr`.purchasemoney / (
                                            COALESCE (`tmpr`.discount, 0) + `tmpr`.purchasequantity
                                        )
                                    )
                                FROM
                                    `receiving` `tmpr`
                                WHERE
                                    `tmpr`.`mattercode` = `tmpl`.`materielcode`
                                AND `tmpr`.`receivingtime` <= tmpll.maxtime
                                AND `tmpr`.`receivingtime` >= tmpll.mintime
                            )
                        )
                    )
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`materielgoods` = '中鸡料'
            ) AS zlsje,
            (
                SELECT
                    SUM(`tmpl`.`receivenumber`)
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`materielgoods` = '大鸡料'
            ) AS dlsl,
            (
                SELECT
                    AVG(
                        (
                            SELECT
                                AVG(
                                    `tmpr`.purchasemoney / (
                                        COALESCE (`tmpr`.discount, 0) + `tmpr`.purchasequantity
                                    )
                                )
                            FROM
                                `receiving` `tmpr`
                            WHERE
                                `tmpr`.`mattercode` = `tmpl`.`materielcode`
                            AND `tmpr`.`receivingtime` <= tmpll.maxtime
                            AND `tmpr`.`receivingtime` >= tmpll.mintime
                        )
                    )
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`materielgoods` = '大鸡料'
            ) AS dlcbdj,
            (
                SELECT
                    SUM(
                        (
                            `tmpl`.`receivenumber` * (
                                SELECT
                                    AVG(
                                        `tmpr`.purchasemoney / (
                                            COALESCE (`tmpr`.discount, 0) + `tmpr`.purchasequantity
                                        )
                                    )
                                FROM
                                    `receiving` `tmpr`
                                WHERE
                                    `tmpr`.`mattercode` = `tmpl`.`materielcode`
                                AND `tmpr`.`receivingtime` <= tmpll.maxtime
                                AND `tmpr`.`receivingtime` >= tmpll.mintime
                            )
                        )
                    )
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`materielgoods` = '大鸡料'
            ) AS dlsje,
            (
                SELECT
                    SUM(
                        (
                            `tmpl`.`receivenumber` * (
                                SELECT
                                    AVG(
                                        `tmpr`.purchasemoney / (
                                            COALESCE (`tmpr`.discount, 0) + `tmpr`.purchasequantity
                                        )
                                    )
                                FROM
                                    receiving tmpr
                                WHERE
                                    tmpr.mattercode = `tmpl`.materielcode
                            )
                        )
                    )
                FROM
                    `leadregister` `tmpl`
                WHERE
                    `tmpl`.`code` = `tmps`.`yhpcbh`
                AND `tmpl`.`leadregistertype` = ''
            ) AS zyf,
            COALESCE (
                ROUND(
                    (
                        SELECT
                            SUM(`tmpl`.`receivenumber`)
                        FROM
                            `leadregister` `tmpl`
                        WHERE
                            `tmpl`.`code` = `tmps`.`yhpcbh`
                        AND `tmpl`.`leadregistertype` = ''
                    ) * 2.6,
                    0
                ),
                0
            ) AS dyf,
            tempwl.zs AS sszs,
            tempwl.zl AS sszl,
            tempwl.je / tempwl.zl AS xsjj,
            tempwl.je AS zsr,
            tempwl.jz AS ssjz
        FROM
            settlementparam tmps
        LEFT JOIN (
            SELECT
                stmpl.`code`,
                MIN(stmpl.leadregistertime) mintime,
                MAX(stmpl.leadregistertime) maxtime
            FROM
                leadregister stmpl
            GROUP BY
                stmpl.`code`
        ) tmpll ON tmpll.`code` = tmps.yhpcbh
        LEFT JOIN (
            SELECT
                wl.yhbh,
                SUM(wl.je) AS je,
                SUM(wl.zs) AS zs,
                SUM(wl.zl) AS zl,
                SUM(wl.zl) / SUM(wl.zs) AS jz
            FROM
                weightlist wl
            GROUP BY
                wl.yhbh
        ) tempwl ON tempwl.yhbh = tmps.yhpcbh
    ) bb
wongdavid的主页 wongdavid | 菜鸟二级 | 园豆:394
提问于:2016-08-07 23:01
< >
分享
所有回答(5)
0

这么多,头都大了。以后谁会来维护啊。能否搞些中间数据表来存一下啊。

| 园豆:780 (小虾三级) | 2016-08-07 23:07
数据多呀,可以远程协助么?
支持(0) 反对(0) wongdavid | 园豆:394 (菜鸟二级) | 2016-08-07 23:23
1

在数据库写业务代码的都是没前途的码农.因为他们没有能力好好设计一个结构来实现业务逻辑.

只能通过过程式的编程来偷懒.

扯这么多是说:别写长sql啦.读出来在程序里处理吧.

吴瑞祥 | 园豆:29449 (高人七级) | 2016-08-08 00:33

不要拿个观点一帮子打死,业务放在哪里都有它的优缺点.

支持(0) 反对(0) wongdavid | 园豆:394 (菜鸟二级) | 2016-08-08 23:39
0

得顶一下,业务丢进数据库确实很挫,

Постой! | 园豆:1084 (小虾三级) | 2016-08-08 09:34
0

太长了,看不下去。。。

顾晓北 | 园豆:10898 (专家六级) | 2016-08-08 10:42
0

我看了下,好像不需要怎么优化吧?嵌套很少,大部分都是子查询,有啥可优化的?

刘宏玺 | 园豆:14020 (专家六级) | 2016-08-08 11:37

返回row太多,非常慢

支持(0) 反对(0) wongdavid | 园豆:394 (菜鸟二级) | 2016-08-08 23:27

@wongdavid: 那你把下面的两个join的group by 想法去掉,可能效率就上来了

支持(0) 反对(0) 刘宏玺 | 园豆:14020 (专家六级) | 2016-08-09 10:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册