2012-02-22 78 views
1

我如何获得所有列的总和?假设我有一张如下所示的桌子。我的查询获得总成本和净收益。我怎样才能在不写第二个查询的情况下计算“总数”总和。列的总和

Date  rate  supplyfee  demandfee  chargedfee othersTotalcosts netReturn 
2010-01-10 1,339.00 2,150.00  10,000.00  120,000.00  1,085.78  1,064.22 
2011-01-01 3,339.00 1,150.00  1,100.00  150,000.00  1,144.13  5.87 
*Total*    3,300.00  2,100.00 

的总成本是由sum(chargedfee + others)/rate + demandfee计算和净收益由supplyfee - totalcosts计算。

下面的查询计算总成本:

SELECT date 
     , rate 
     , supply_fee 
     , demand_fee 
     , charged_fee 
     , (charged + others/rate) + demandfee AS totalcosts 
     , supplyfee-((charged + others/rate) + demandfee AS net returns 

FROM financies 
WHERE date BETWEEN '2010-01-10' AND '2011-01-01' 
+0

试图重新格式化您的帖子,但'作为净收益'和''2010-01-10 AND''2011-01-01''看起来不正确,而你打开两个括号,而只关闭一个...... – 2012-02-22 12:40:46

+0

Redited the post did see the one – smilezjim 2012-02-22 12:43:54

+0

你的配方需要澄清,它们并没有真正意义上的逐字 – 2012-02-22 13:07:52

回答

2

这看起来很复杂,但忍受着我。它需要对others/rate的含义进行一些说明,但是其原理是正确的。如果您可以使用财务主键,那么可以使用更优雅的(GROUP BY ... ROLLUP)解决方案,但是我没有足够的经验来提供可靠的建议。我将如何解决这个问题。

啰嗦选项

(
    SELECT 
     financesTallied.date, 
     financesTallied.rate, 
     financesTallied.supply_fee, 
     financesTallied.demand_fee, 
     financesTallied.charged_fee, 
     financesTallied.total_costs, 
     financesTallied.net_return 

    FROM (

     SELECT 
      financeWithNetReturn.*, 
      @supplyFee := @supplyFee + financeWithNetReturn.supply_fee, 
      @demandFee := @demandFee + financeWithNetReturn.demand_fee, 
      @charedFee := @charedFee + financeWithNetReturn.charged_fee 
     FROM 
     (// Calculate net return based off total costs 
      SELECT 
       financeData.*, 
       financeData.supply_fee - financeData.total_costs AS net_return 
      FROM 
      (// Select the data 
       SELECT 
        date, 
        rate, 
        supply_fee, 
        demand_fee, 
        charged_fee, 
        (supply_fee+demand_fee+charged_fee)/rate AS total_costs // need clarification on others/rate 
       FROM financies 
       WHERE date BETWEEN '2010-01-10' AND '2011-01-01' 
       ORDER BY date ASC 
      ) AS financeData 
     ) AS financeWithNetReturn, 
     (
      SELECT 
       @supplyFee := 0 
       @demandFee := 0 
       @charedFee := 0 
     ) AS variableInit 
    ) AS financesTallied 
) UNION (
    SELECT 
     '*Total*', 
     NULL, 
     @supplyFee, 
     @demandFee, 
     @chargedFee, 
     NULL, 
     NULL 
) 

从最内层查询到最外面的工作。该查询选择基本费用并计算该行的total_costs。这total_costs公式将需要调整,因为我不是100%清楚你在那里寻找什么。将把这个作为[SQ1]

  SELECT 
       date, 
       rate, 
       supply_fee, 
       demand_fee, 
       charged_fee, 
       (supply_fee+demand_fee+charged_fee)/rate AS total_costs // need clarification on others/rate 
      FROM financies 
      WHERE date BETWEEN '2010-01-10' AND '2011-01-01' 
      ORDER BY date ASC 

下一级别我只是重用计算total_costs柱与supply_fee列在net_return列添加。到此结束,你每行所需要的基础数据,将其称为[SQL2]

 SELECT 
      financeData.*, 
      financeData.supply_fee - financeData.total_costs AS net_return 
     FROM 
     ([SQ1]) AS financeData 

在这个层面上它的时候开始清点了值,因此需要初始化为0值所需的变量([SQL3]

 SELECT 
      @supplyFee := 0 
      @demandFee := 0 
      @charedFee := 0 

下一级别,我使用的计算出的行来计算总计([SQL4]

SELECT 
     financeWithNetReturn.*, 
     @supplyFee := @supplyFee + financeWithNetReturn.supply_fee, 
     @demandFee := @demandFee + financeWithNetReturn.demand_fee, 
     @charedFee := @charedFee + financeWithNetReturn.charged_fee 
    FROM 
    ([SQL2]) AS financeWithNetReturn, 
    ([SQL3]) AS variableInit 

现在终于在顶层,只需要输出,而不计算列所需的列([SQL5]

SELECT 
    financesTallied.date, 
    financesTallied.rate, 
    financesTallied.supply_fee, 
    financesTallied.demand_fee, 
    financesTallied.charged_fee, 
    financesTallied.total_costs, 
    financesTallied.net_return 

FROM ([SQL4]) AS financesTallied 

然后输出它联合在一起具有总计行

([SQL5]) UNION (
    SELECT 
     '*Total*', 
     NULL, 
     @supplyFee, 
     @demandFee, 
     @chargedFee, 
     NULL, 
     NULL 
) 
+0

没问题,我很享受有趣的。清理了我的答案 - 可能是一个想法,清理这些评论(稍后将删除这一点) – 2012-02-23 10:31:31

0

包裹它并添加类似于此的另一列:

select date, rate, supply_fee, demand_fee, charged_fee, totalcosts, net_returns, 
(totalcosts+net_returns+supply_fee) as grandtotal 
from(
    select date, rate, supply_fee, demand_fee, charged_fee, 
      (charged + others/rate) + demandfee as totalcosts, 
      supplyfee-((charged + others/rate) + demandfee as net_returns 
    from finances 
    where date between '2010-01-10' AND '2011-01-01') 
0

可以使用WITH ROLLUPGROUP BY子句summurize你的栏目,像:

select IFNULL(date, "Total") date, IFNULL(rate, "") rate, 
     supply_fee, demand_fee, charged_fee, 
     (charged + others/rate) + demandfee as totalcosts, 
     supplyfee-((charged + others/rate) + demandfee as net returns 
from financies 
Group by date, rate WITH ROLLUP 
Having date between '2010-01-10' AND '2011-01-01' 
+0

上述查询成功运行,但是返回零结果集可能问题与查询有关吗? – smilezjim 2012-02-23 07:32:10

+0

@smilezjim,检查你的数据,是否有任何数据符合'Having'中的条件? – 2012-02-23 07:35:45