【问题标题】:Calculate percentages with MySQL/Javascript使用 MySQL/Javascript 计算百分比
【发布时间】:2012-09-20 14:54:33
【问题描述】:

我正在尝试使用一个麻烦的 SQL 查询 Entrinsik 的告密者。我希望 Informer 对我的 结果使用 javascript 计算百分比。然而,Informer 缺乏 能够访问列下的数据(即我的总数 百分比)所以我需要我的 SQL 查询来为我生成这个。结果 目前看起来像这样:

refund_code    refund_amount    month_group
-----------    -------------    -----------
ref1               10           january
ref2               20           january
ref3               30           january
ref1               40           february
ref2               50           february
ref3               60           february

我想要的是这样的:

refund_code    refund_amount    month_group    month_total  
-----------    -------------    -----------    -----------  
ref1               10           january                 60  
ref2               20           january                 60  
ref3               30           january                 60  
ref1               40           february               150  
ref2               50           february               150  
ref3               60           february               150  

我的查询如下:

SELECT mr.month_group,
    bd.transaction_code AS refund_code,
    SUM(bd.extended) AS refund_amount
FROM   billing_details AS bd
    LEFT JOIN monthly_ranges AS mr
        ON ( bd.entry_date BETWEEN mr.start_date AND mr.end_date )
WHERE  bd.transaction_code IN ( 'REFPRI', 'REFSEC', 'REFPT', 'REFREQPRI' )
    AND bd.entry_date >= '2012-01-05'
GROUP BY mr.month_group, bd.transaction_code
ORDER BY mr.month_group, bd.transaction_code

生成每月总计表的第二个查询如下所示:

SELECT mr.month_group,
    SUM(bd.extended) AS refund_amount
FROM   billing_details AS bd
    LEFT JOIN monthly_ranges AS mr
        ON ( bd.entry_date BETWEEN mr.start_date AND mr.end_date )
WHERE  bd.transaction_code IN ( 'REFPRI', 'REFSEC', 'REFPT', 'REFREQPRI' )
    AND bd.entry_date >= '2012-01-05'
GROUP BY mr.month_group
ORDER BY mr.month_group

那么有没有办法将两者结合起来呢?

【问题讨论】:

    标签: javascript mysql percentage


    【解决方案1】:

    从技术上讲,您实际上可以将两者作为子查询加入。

    SELECT m.refund_code, m.refund_amount, m.month_group, t.month_total
     FROM (your refund query above) m
     JOIN (your total query above) t
       ON m.month_group = t.month_group
    

    只需确保将“总查询”中的“refund_amount”重命名为“month_total”或类似名称即可。

    【讨论】:

      猜你喜欢
      • 2013-03-22
      • 2013-11-15
      • 2017-02-08
      • 2015-09-04
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多