gpt4 book ai didi

MySQL UNION - 复制子查询

转载 作者:行者123 更新时间:2023-11-29 20:58:09 24 4
gpt4 key购买 nike

这是我当前的查询:

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”,并更改 WHERE 子句中的 Close_Date 字段而不是 Log_Date 字段。基本上仅在“ReqCount1”字段中产生两个子查询的总和。

如有任何帮助,我们将不胜感激。

最佳答案

好的,问题解决了

这是解决方案(在 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

关于MySQL UNION - 复制子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37454617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com