MySQL UNION - 复制子查询

时间:2016-05-26 07:46:32

标签: mysql union

这是我目前的查询:

Select Month as ReqMonth1,
    sum(TotalUsage) As ReqCount1,
    sum(Memberbase) as Club_updates_Records1,
    round(sum(TotalUsage)/sum(Memberbase)*100,0) AS UsagePerc
from(
Select * from (
SELECT
     CAST(DATE_FORMAT(Log_Date, '%b-%y') AS CHAR(100)) AS 'Month',
     CAST(DATE_FORMAT(Log_Date, '%y-%m') AS CHAR(100)) AS 'Monthsort',
     count(Requests.`fk_Members_ID`) AS TotalUsage,
     0 as Memberbase
FROM
     `Requests` Requests INNER JOIN `Members` Members ON Requests.`fk_Members_ID` = Members.`ID`
WHERE
     cast(Requests.`Log_date` as date) BETWEEN date_sub(if($P{StartDate}>'2016-06-01',$P{StartDate},'2016-05-01'), Interval 4 month) and $P{EndDate} AND $P{EndDate}
 AND Members.`Club` = $P{Club}
GROUP BY
     CAST(DATE_FORMAT(Log_Date, '%M-%y') AS CHAR(100)) ) as A

UNION ALL

Select * from (
SELECT
     CAST(DATE_FORMAT(Club_updates.`Update_Date`, '%b-%y') AS CHAR(100)) AS 'Month',
     CAST(DATE_FORMAT(Club_updates.`Update_Date`, '%y-%m') AS CHAR(100)) AS 'Monthsort',
     0 AS TotalUsage,
     ifnull(max(Club_updates.`Records`),1) AS MemberBase
FROM
     `Club updates` Club_updates
WHERE
     cast(Club_updates.`Update_Date` as date) BETWEEN date_sub(if($P{StartDate}>'2016-06-01',$P{StartDate},'2016-05-01'), Interval 4 month) and $P{EndDate} AND $P{EndDate}
 AND Club_updates.`fk_Club` = $P{Club}
GROUP BY  CAST(DATE_FORMAT(Club_updates.`Update_Date`, '%M-%y') AS CHAR(100)) ) As B) as D
group by Month
Order by MonthSort

我试图复制/添加另一个子查询" A"使用Close_Date字段的更改而不是WHERE子句中的Log_Date字段。 基本上只在" ReqCount1"中产生两个子查询的总和。字段。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

好的,解决了它

这里是解决方案(在WHERE子句中有附加条件)

Select Month as ReqMonth1,
    sum(TotalUsage) As ReqCount1,
    sum(Memberbase) as Club_updates_Records1,
    round(sum(TotalUsage)/sum(Memberbase)*100,0) AS UsagePerc
from(
Select * from (
SELECT
     CAST(DATE_FORMAT(Log_Date, '%b-%y') AS CHAR(100)) AS 'Month',
     CAST(DATE_FORMAT(Log_Date, '%y-%m') AS CHAR(100)) AS 'Monthsort',
     count(Requests.`fk_Members_ID`) AS TotalUsage,
     0 as Memberbase
FROM
     `Requests` Requests INNER JOIN `Members` Members ON Requests.`fk_Members_ID` = Members.`ID`
WHERE
     cast(Requests.`Log_date` as date) BETWEEN date_sub(if($P{StartDate}>'2016-06-01',$P{StartDate},'2016-05-01'), Interval 4 month) and $P{EndDate} AND $P{EndDate}
 AND Members.`Club` = $P{Club} AND Requests.`Request_Type` <> 7
GROUP BY
     CAST(DATE_FORMAT(Log_Date, '%M-%y') AS CHAR(100)) ) as A


UNION ALL
Select * from (
SELECT
     CAST(DATE_FORMAT(Close_Date, '%b-%y') AS CHAR(100)) AS 'Month',
     CAST(DATE_FORMAT(Close_Date, '%y-%m') AS CHAR(100)) AS 'Monthsort',
     count(Requests.`fk_Members_ID`) AS TotalUsage,
     0 as Memberbase
FROM
     `Requests` Requests INNER JOIN `Members` Members ON Requests.`fk_Members_ID` = Members.`ID`
WHERE
     cast(Requests.`Close_Date` as date) BETWEEN date_sub(if($P{StartDate}>'2016-06-01',$P{StartDate},'2016-05-01'), Interval 4 month) and $P{EndDate} AND $P{EndDate}
 AND Members.`Club` = $P{Club} AND Requests.`Request_Type` = 7
GROUP BY
     CAST(DATE_FORMAT(Close_Date, '%M-%y') AS CHAR(100)) ) as G

UNION ALL


Select * from (
SELECT
     CAST(DATE_FORMAT(Club_updates.`Update_Date`, '%b-%y') AS CHAR(100)) AS 'Month',
     CAST(DATE_FORMAT(Club_updates.`Update_Date`, '%y-%m') AS CHAR(100)) AS 'Monthsort',
     0 AS TotalUsage,
     ifnull(max(Club_updates.`Records`),1) AS MemberBase
FROM
     `Club updates` Club_updates
WHERE
     cast(Club_updates.`Update_Date` as date) BETWEEN date_sub(if($P{StartDate}>'2016-06-01',$P{StartDate},'2016-05-01'), Interval 4 month) and $P{EndDate} AND $P{EndDate}
 AND Club_updates.`fk_Club` = $P{Club}
GROUP BY  CAST(DATE_FORMAT(Club_updates.`Update_Date`, '%M-%y') AS CHAR(100)) ) As B) as D
group by Month
Order by MonthSort