2017-10-12 84 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