duan010167787 2013-02-19 21:56
浏览 191
已采纳

mysql查询 - 一个查询来创建按月显示销售额的表

I have a mysql table with sales information: Seller, product, sales date.

How do I write a query to generate a table that shows total products shown by each seller, per month, and in total?

This is the query I use to create a table with SellerName and total sales in July:

select SellerName, count(*) as july 
from onlineDATA
where ReportMonth = 201207
GROUP BY SellerName
ORDER BY july DESC
LIMIT 0,30

How do I modify this to get additional sales totals for each month?

I am looking for output like this:

    SellerName   | Jun  | Jul  | Aug  | YTD
    John Doe     | 30   |  25  |  30  | 85

Bonus question - how would I code this in PHP when the number of months would be a user input - anywhere from 1 to 12?

Thanks

  • 写回答

1条回答 默认 最新

  • dtueufe82643 2013-02-19 22:04
    关注

    I think you will have to define the columns separately, like this:

    SELECT 
    
    SellerName,     
    SUM(IF(ReportMonth = 201206, 1.0)) AS Jun,
    SUM(IF(ReportMonth = 201207, 1.0)) AS Jul,
    SUM(IF(ReportMonth = 201208, 1.0)) AS Aug,
    COUNT(*) AS YTD
    
    FROM onlineDATA
    
    GROUP BY SellerName 
    ORDER BY SellerName DESC LIMIT 30;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答