2017-10-12 60 views
0

在下面的代码中,如何将MRR_Created和MRR_Destroyed结合在UNION中,以便它只显示下一个数字?现在查询是正确的,但我不需要分别看到增加/减少。选择子查询中的UNION问题

select account.email, account.vip, datediff(now(), account.date_created) AS Age, 
(select sum(account_subscription.next_invoice_price) as ActiveMRR 
from account_subscription 
where account_subscription.status = 'active' 
and account_subscription.acctid = account.acctid 
) as MRR, 
(select count(account_subscription.subid) as Churn 
from account_subscription 
where account_subscription.date_created between DATE_ADD(NOW(), INTERVAL -2880 MINUTE) and NOW() 
and account_subscription.date_closed between DATE_ADD(NOW(), INTERVAL -2880 MINUTE) and NOW() 
and account_subscription.acctid = account.acctid 
) as Churn, 

(select sum(account_subscription.next_invoice_price) as MRR 
from account_subscription 
where date(account_subscription.date_created) = date(curdate()) 
and account_subscription.acctid = account.acctid 
) as MRR_Created, 
(select sum(account_subscription.next_invoice_price) as MRR 
from account_subscription 
where date(account_subscription.date_closed) = date(curdate()) 
and account_subscription.acctid = account.acctid 
) as MRR_Destroyed, 

concat("https://sitetest.com?ACCTID=",account.acctid) as URL 

from account 
where account.status = 'active' 
and (
account.type in ('affiliate', 'customer', 'customer_freetrial', 'customer_duplicate', 'customer_match') 
or account.type is null 
) 
group by account.acctid 
order by MRR desc 

回答

0

不知道你是否真的需要一个UNION在这里。试着用

(select sum(account_subscription.next_invoice_price) as MRR 
from account_subscription 
where (date(account_subscription.date_created) = date(curdate()) 
or date(account_subscription.date_closed) = date(curdate())) 
and account_subscription.acctid = account.acctid 
) as MRR_Created, 

希望这有助于更换

(select sum(account_subscription.next_invoice_price) as MRR 
from account_subscription 
where date(account_subscription.date_created) = date(curdate()) 
and account_subscription.acctid = account.acctid 
) as MRR_Created, 
(select sum(account_subscription.next_invoice_price) as MRR 
from account_subscription 
where date(account_subscription.date_closed) = date(curdate()) 
and account_subscription.acctid = account.acctid 
) as MRR_Destroyed, 

+0

几乎 - 唯一的怪癖是,我试图将总数创建为一个正数,将总数关闭为负数,然后求和以获得净变化。这是有两个查询背后的想法,并试图使用UNION – skyrunner

+0

@skyrunner如果值存储为你所描述的(打开为正面和封闭为负面),这个查询会给你网。或者你的意思是所有的值都存储为正数,你需要在查询中做出这种分离? –

+0

一切都存储为积极的,不幸的是 – skyrunner