【问题标题】:Count totals per month per salesman每个推销员每月的总数
【发布时间】:2023-03-17 03:53:01
【问题描述】:

我有一个带有订单的 mySQL 表。每个表格行包括订单日期、金额和销售人员

 order_id   | 
 amount     |  
 saleman_id | 
 order_date

我想查询它,以便生成一个 HTML 表格,其中包含类似这样的报告。

 Year | Month | Total | Total for salesman 1 | Total for salesman 2 | etc...
 2014 | 10    | 5000  | 2000                 | 3000                 | etc...
 2014 | 11    | 6000  | 5000                 | 1000                 | etc...

这已经部分工作了,这意味着我有一个查询,允许我创建一个报告,显示所有销售人员每个月的总数,但我想知道是否有一种方法可以实现我想要的单个 SQL 查询,如果值得,性能方面。否则,我可以在 PHP while 循环中查询每个推销员的结果,该循环会生成我已经拥有的表。

Mu 当前查询是:

SELECT 
YEAR(ord_date) AS year, 
MONTH(ord_date) AS month,  
ROUND(SUM(...colum calculations...),2) AS sales, 
COUNT(o.ord_id) AS count_orders 
FROM 
orders_ord AS o 
GROUP BY 
year, month
ORDER BY year DESC, month DESC;

我正在使用带有经典 mysql 模块和 mySQL5.5 的 PHP 5.4

【问题讨论】:

  • 你有什么问题?
  • 您使用的是哪个 MySQL API?
  • MySQL 服务器 5.5.28,数据库客户端版本:libmysql - 5.1.73 @Mike “我想获得一份报告,在每一行显示每月总销售额和每月总销售额每个销售人员”如果可能的话,使用什么 SQL。
  • 含义,mysql_ - mysqli_ 还是 PDO?我想你想用 PHP 做这个吗?或者,只是一个直接的 SQL 调用?
  • 对不起 :) 我想在 PHP 中执行此操作,正确。我使用的 API 是 mysql,但我也可以使用 mysqli。

标签: php mysql sql


【解决方案1】:

你可以使用with rollup做你想做的事:

SELECT YEAR(ord_date) AS year, MONTH(ord_date) AS month, repid, MONTHNAME(ord_date) AS monthname, 
       ROUND(SUM(...colum calculations...),2) AS profit,
       ROUND(SUM(...colum calculations...),2) AS sales, 
       COUNT(o.ord_id) AS count 
FROM orders_ord  o 
GROUP BY year, month
ORDER BY year DESC, month desc, repid with rollup;

但是,由于汇总的性质,我建议在 group by 中结合年份和月份:

SELECT YEAR(ord_date) AS year, MONTH(ord_date) AS month, repid, MONTHNAME(ord_date) AS monthname, 
       ROUND(SUM(...colum calculations...),2) AS profit,
       ROUND(SUM(...colum calculations...),2) AS sales, 
       COUNT(o.ord_id) AS count 
FROM orders_ord  o 
GROUP BY YEAR(ord_date) * 100 + MONTH(ord_date) desc, repid with rollup;

这将只放置单个月份的摘要行,而不是年份和月份的摘要。

【讨论】:

  • rollup 和 order by 不是互斥的吗?为什么年份要乘以 100?
  • @LavaRo 。 . .第二个是你只能得到一个汇总行(而不是一年一个,一个月一个)。第一个是因为我认为随着group by 弃用默认排序而取消了 5.7 的限制。那是个误会;它们仍然是相互排斥的。
【解决方案2】:

没有看到你的节目创建表很难给你准确的查询。 Genarelly speeking 我喜欢在 SQL 中而不是在 PHP 中完成所有工作。但是,如果您有一个数据网格并且想要显示小计或总计,则使用 PHP 来添加行。

总之,这里有一些对你有帮助的查询

此查询将为您提供四舍五入的利润和四舍五入的销售额

SELECT 
rep_id,
YEAR(ord_date) AS year, 
MONTH(ord_date) AS month,  
MONTHNAME(ord_date) AS monthname, 
ROUND(SUM(...colum calculations...),2) AS profit,
ROUND(SUM(...colum calculations...),2) AS sales, 
COUNT(o.ord_id) AS count 
FROM 
orders_ord AS o 
GROUP BY rep_id, year, month
ORDER BY year DESC, month DESC;

【讨论】:

    【解决方案3】:

    让数据库做一切,它擅长。 (在填补 PHP 的空白时,将所有额外的行程保存到数据库中。)

    SELECT 
    YEAR(ord_date) AS year, 
    MONTH(ord_date) AS month,  
    MONTHNAME(ord_date) AS monthname, 
    saleman_id,
    ROUND(SUM(...colum calculations...),2) AS profit,
    ROUND(SUM(...colum calculations...),2) AS sales, 
    COUNT(o.ord_id) AS count 
    FROM 
    orders_ord 
    GROUP BY  year, month, saleman_id
    
    UNION ALL
    
    SELECT 
    YEAR(ord_date) AS year, 
    MONTH(ord_date) AS month,  
    MONTHNAME(ord_date) AS monthname,
    'zzz Total zzz', 
    ROUND(SUM(...colum calculations...),2) AS profit,
    ROUND(SUM(...colum calculations...),2) AS sales, 
    COUNT(o.ord_id) AS count 
    FROM 
    orders_ord 
    GROUP BY  year, month
    ORDER BY year DESC, month DESC;
    

    如果确实不提供年度总数,将进行调整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 2021-05-25
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2023-01-08
      相关资源
      最近更新 更多