Sum,Case,Union,Group by:Php Mysql

时间:2017-01-24 10:49:53

标签: php mysql group-by sum union

SUM CASE未显示正确的详细信息

示例日期:

  下面的

是按照prdid分组的数据

C:\Python27-64\python.exe
  

下面是我按照prdid

删除群组时的数据
{
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "variants": [ { 
          "name": "Run", 
          "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", 
          "cmd": ["C:\\Python27-64\\python.exe", "-u", "$file"] 
    } ] 
}
有一件奇怪的事:

以上示例数据

wghvalue    prdid  addby  cutequl
0.4         57546    me     1
0.6         71256    me     1
0.2         68754    me     1

所以如果我正在写查询

  

prdb.order - cutid.order as newval

我得到的价值就像... 2-2 = 0,3-2 = 1,4-4 = 0

相同
  

总和(prdb.order - cutid.order = 0然后1其他0结束时的情况)作为newval

获得价值...... 3,2,1

我认为bove值3,2,1没有得到prdid小组

下面是查询

wghvalue    prdid  addby  cutequl
0.1         57546    me     1
0.1         57546    me     1
0.2         57546    me     1
0.1         71256    me     1
0.5         71256    me     1
0.2         68754    me     1

来自上述查询的输出为:

prdb.order  cutid.order
    2           2
    3           2
    4           4

我应该得到以下内容

SELECT 
    sum(t1.count) as count,
    sum(t1.wghvalue) as wghvalue,
    t1.addby,
    SUM(t1.cutequl) as cutequl
FROM 
    (SELECT  
        COUNT(pl.id) as count,
        SUM(pl.wghvalue) as wghvalue
        addby,
        SUM(CASE 
            WHEN prdb.cutaname = cutid.cutname
                 AND prdb.order = cutid.order
                 AND prdb.order - cutid.order = 0  
            THEN 1 
            else 0 
        end) as cutequl
    FROM 
        product AS prod
        LEFT JOIN producta AS prda ON prod.id = prda.id
        LEFT JOIN prdoctb AS prdb ON prod.pid = prda.pid 
        LEFT JOIN ( SELECT id, wghvalue, addby, prdid
                    FROM prdentrya
                    WHERE 1=1 
                    GROUP BY prdid
                    UNION
                    SELECT id, wghvalue, addby, prdid
                    FROM prdentryb
                    WHERE 1=1
                    GROUP BY prdid
                   ) AS pl ON( pl.prdid = prda.prdid  )
        LEFT JOIN cut AS cutid ON prdb.cutaname = cutid.cutname  
    WHERE 
        pl.aadby = 103 
    GROUP BY 
        pl.prdid
    ORDER BY
        pl.prdid ASC,
        pl.aadby ASC 
    ) t1
group by t1.addby

我认为 GROUP BY prdid 在总和情况下不起作用, 我怎么能把它包含在总和的情况下。

感谢

1 个答案:

答案 0 :(得分:1)

首先尝试将结果存储到临时表中。 然后在它上使用SUM运算。

                CREATE TEMPORARY TABLE IF NOT EXISTS TempTemp AS
                (
                    SELECT  
                        COUNT(pl.id) as count,
                        SUM(pl.wghvalue) as wghvalue
                        addby,
                        SUM(CASE 
                            WHEN prdb.cutaname = cutid.cutname
                                 AND prdb.order = cutid.order
                                 AND prdb.order - cutid.order = 0  
                            THEN 1 
                            WHEN prdb.order is null or prdb.cutaname  is null THEN 1
                            else 0 
                        end) as cutequl
                    FROM 
                        product AS prod
                        LEFT JOIN producta AS prda ON prod.id = prda.id
                        LEFT JOIN prdoctb AS prdb ON prod.pid = prda.pid 
                        LEFT JOIN ( SELECT id, wghvalue, addby, prdid
                                    FROM prdentrya
                                    WHERE 1=1 
                                    GROUP BY prdid
                                    UNION
                                    SELECT id, wghvalue, addby, prdid
                                    FROM prdentryb
                                    WHERE 1=1
                                    GROUP BY prdid
                                   ) AS pl ON( pl.prdid = prda.prdid  )
                        LEFT JOIN cut AS cutid ON prdb.cutaname = cutid.cutname  
                    WHERE 
                        pl.aadby = 103 
                    GROUP BY 
                        pl.prdid
                    ORDER BY
                        pl.prdid ASC,
                        pl.aadby ASC 
                )


                SELECT 
                    sum(t1.count)  , sum(t1.wghvalue) ,t1.addby,SUM(t1.cutequl) , From 
                    TempTemp t1 
                    group by addby