2016-05-06 19 views
-1

Dies ist die Tabelle Schema:berechnen Wachstumsrate zwischen zwei Daten in MySQL

CREATE TABLE `profit_log` (
    `market_id` smallint(6) NOT NULL, 
    `total_purchase` bigint(20) NOT NULL, 
    `total_profit` bigint(20) NOT NULL, 
    `company_profit` bigint(20) NOT NULL, 
    `date` date NOT NULL, 
    `tid` bigint(20) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; 
-- 
-- Indexes for table `profit_log` 
-- 
ALTER TABLE `profit_log` 
    ADD PRIMARY KEY (`tid`); 

ich die akkumulierbare Kauf zwischen Daten Berechnung Abfrage folgende Verwendung:

SELECT `market_id`, 
    SUM(`total_purchase`) as acc_purchase, 
    SUM(`total_profit`) as acc_profit, 
    SUM(`company_profit`) as acc_company_profit 
FROM `profit_log` 
WHERE `date` >= '2016-01-01' 
AND `date` < ('2016-01-20' + INTERVAL 1 DAY) 
AND `market_id` IN (1,2,5,8,22) 
GROUP BY `market_id` 

Ich frage mich, ob es möglich, Wachstumsrate für jede market_id in dieser Abfrage zu berechnen?.

growth rate = ((total_purchase as top where date = '2016-01-20')-(total_purchase as bottom where date = '2016-01-01')/bottom) * 100 

ist es möglich, das alles in einer Abfrage zu tun, oder es muss in separaten Abfrage berechnet werden?

Danke.

Antwort

0

Versuchen Sie folgendes:

SELECT o.`market_id`, 
    SUM(o.`total_purchase`) as acc_purchase, 
    SUM(o.`total_profit`) as acc_profit, 
    SUM(o.`company_profit`) as acc_company_profit, 

    (SELECT total_purchase FROM profit_log as i WHERE o.market_id = i.market_id AND i.`date` = '2016-01-01') 
     /
    (SELECT total_purchase FROM profit_log as i WHERE o.market_id = i.market_id AND i.`date` = '2016-01-20') 
     as growth_rate, 

FROM `profit_log` as o 
WHERE `date` >= '2016-01-01' 
AND `date` < ('2016-01-20' + INTERVAL 1 DAY) 
AND `market_id` IN (1,2,5,8,22) 
GROUP BY `market_id` 
Verwandte Themen