如何在单个MySQL查询中添加两个MySQL查询中的值/列?

时间:2014-08-22 15:06:34

标签: mysql

我有以下两个单独运行的查询,但我需要将它们组合成一个查询来获取“Jan Totals”,“Feb Totals”,“Mar Totals”等等。

Jan的计算将是:(Jan + Jan_webl)AS Jan_Total,但不确定如何引用这两个值/列以使它们相似?

第一个查询输出是:

"Average"   "Jan"   "Feb"   "Mar"   "Apr"   "May"   "Jun"   "Jul"   "Aug"   "Sep"   "Oct"   "Nov"   "Dec"   "Total"
"97086.8276"    "42550" "33200" "42950" "30192" "48216" "58650" "41451" "16320" ""  ""  ""  ""  "2815518"

第二个查询输出是:

"weekly_Avg"    "Jan_webl"  "Feb_webl"  "Mar_webl"  "Apr_webl"  "May_webl"  "Jun_webl"  "Jul_webl"  "Aug_webl"  "Sep_webl"  "Oct_webl"  "Nov_webl"  "Dec_webl"  "Total_webl"
"53270.0000"    "0" "0" "0" "50"    "19925" "35025" "48975" "27000" "0" "0" "0" "0" "130950"

查询#1

SELECT 
AVG(febl_remaining) as 'Average',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%01') THEN febl_remaining END) 'Jan',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%02') THEN febl_remaining END) 'Feb',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%03') THEN febl_remaining END) 'Mar',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%04') THEN febl_remaining END) 'Apr',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%05') THEN febl_remaining END) 'May',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%06') THEN febl_remaining END) 'Jun',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%07') THEN febl_remaining END) 'Jul',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%08') THEN febl_remaining END) 'Aug',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%09') THEN febl_remaining END) 'Sep',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%10') THEN febl_remaining END) 'Oct',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%11') THEN febl_remaining END) 'Nov',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%12') THEN febl_remaining END) 'Dec',
SUM(febl_remaining) as 'Total'

FROM
(   SELECT 
    1 as id, 
    DATE_FORMAT(tblcom.omActCompDate, '%Y-%m') AS febl_Month,   
    IF 
    (   SUM(DISTINCT weEstBuildLgth) IS NULL, 
          SUM(DISTINCT tblcom.omEstBuildLgth), 
          SUM(DISTINCT tblcom.omEstBuildLgth) - SUM(DISTINCT weEstBuildLgth)
    ) AS febl_remaining
FROM      tblweeklyebl 
RIGHT OUTER JOIN tblcom ON tblweeklyebl.comID = tblcom.omID 
WHERE tblcom.omstatusID Like 'Closed'
GROUP BY DATE_FORMAT(tblcom.omActCompDate, '%Y-%m')
) t 
GROUP BY t.id

查询#2

SELECT 
SUM(DISTINCT tblcom.omEstBuildLgth - tblweeklyebl.weEstBuildLgth) / COUNT(DISTINCT   

IF(tblcom.omEstBuildLgth - tblweeklyebl.weEstBuildLgth = 0, NULL,  
MONTH(tblweeklyebl.weDate))) AS weekly_Average,

   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') =   
DATE_FORMAT(NOW(), '%Y-%01') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Jan_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%02') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Feb_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%03') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Mar_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%04') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Apr_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%05') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'May_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%06') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Jun_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%07') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Jul_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%08') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Aug_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%09') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Sep_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%10') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Oct_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%11') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Nov_webl',
   SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%12') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Dec_webl',
   SUM(DISTINCT tblweeklyebl.weEstBuildLgth) AS Total_webl    

FROM      tblweeklyebl RIGHT OUTER JOIN tblcom ON tblweeklyebl.comID = tblcom.omID

可选:如何垂直列出数据,如:

tebl month
44550 Jan
33200 Feb
48450 Mar
34942 Apr
53516 May
62700 Jun
46651 Jul
78690 Aug

类似:GROUP BY月按月份订购(tblcom.omActCompDate)?

1 个答案:

答案 0 :(得分:0)

我相信这会有效..但我需要数据来测试它

SELECT 
    t1.Average, t2,weekly_average, 
    t1.Jan + t2.Jan_webl AS Jan_Total, 
    t1.Feb + t2.Feb_webl AS Feb_Total,
    t1.Mar + t2.Mar_webl AS Mar_Total,
    t1.Apr + t2.Apr_webl AS Apr_Total,
    t1.May + t2.May_webl AS May_Total,
    t1.Jun + t2.Jun_webl AS Jun_Total,
    t1.Jul + t2.Jul_webl AS Jul_Total,
    t1.Aug + t2.Aug_webl AS Aug_Total,
    t1.Sep + t2.Sep_webl AS Sep_Total,
    t1.Oct + t2.Oct_webl AS Oct_Total,
    t1.Nov + t2.Nov_webl AS Nov_Total,
    t1.Dec + t2.Dec_webl AS Dec_Total,
    t1.Total, t2.total_webl
FROM
(   SELECT 
        AVG(febl_remaining) as 'Average',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%01') THEN febl_remaining END) 'Jan',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%02') THEN febl_remaining END) 'Feb',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%03') THEN febl_remaining END) 'Mar',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%04') THEN febl_remaining END) 'Apr',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%05') THEN febl_remaining END) 'May',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%06') THEN febl_remaining END) 'Jun',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%07') THEN febl_remaining END) 'Jul',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%08') THEN febl_remaining END) 'Aug',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%09') THEN febl_remaining END) 'Sep',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%10') THEN febl_remaining END) 'Oct',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%11') THEN febl_remaining END) 'Nov',
        MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%12') THEN febl_remaining END) 'Dec',
        SUM(febl_remaining) as 'Total'
    FROM
    (   SELECT 
            1 as id, 
            DATE_FORMAT(tblcom.omActCompDate, '%Y-%m') AS febl_Month,   
            IF 
            (   SUM(DISTINCT weEstBuildLgth) IS NULL, 
                SUM(DISTINCT tblcom.omEstBuildLgth), 
                SUM(DISTINCT tblcom.omEstBuildLgth) - SUM(DISTINCT weEstBuildLgth)
            ) AS febl_remaining
        FROM tblweeklyebl 
        RIGHT OUTER JOIN tblcom ON tblweeklyebl.comID = tblcom.omID 
        WHERE tblcom.omstatusID Like 'Closed'
        GROUP BY DATE_FORMAT(tblcom.omActCompDate, '%Y-%m')
    ) t 
    GROUP BY t.id
) t1
JOIN
(   SELECT 
        SUM(DISTINCT tblcom.omEstBuildLgth - tblweeklyebl.weEstBuildLgth) / 
        COUNT(DISTINCT   
            IF(tblcom.omEstBuildLgth - tblweeklyebl.weEstBuildLgth = 0, NULL, MONTH(tblweeklyebl.weDate))
        ) AS weekly_Average,
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%01') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Jan_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%02') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Feb_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%03') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Mar_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%04') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Apr_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%05') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'May_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%06') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Jun_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%07') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Jul_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%08') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Aug_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%09') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Sep_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%10') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Oct_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%11') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Nov_webl',
        SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%12') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Dec_webl',
        SUM(DISTINCT tblweeklyebl.weEstBuildLgth) AS Total_webl    
    FROM tblweeklyebl 
    RIGHT OUTER JOIN tblcom ON tblweeklyebl.comID = tblcom.omID
) t2

顺便说一句,我很高兴看到你接受了我的第一个查询并进行了扩展!你正在学习!!