简体   繁体   English

显示每个分支MySQL按天显示的每周销售额

[英]Displaying weekly sales by day per branch mysql

I have 2 tables. 我有2张桌子。

Table 1 = branch containing 表1 =分支包含

branch_id branch_name branch_id branch_name

Table 2 = sales_data containing 表2 = sales_data包含

sales_id sales_date sales_branch sales_profit sales_id sales_date sales_branch sales_profit

I need away of showing the total daily sales and total daily profit for each branch. 我需要显示每个分支机构的每日总销售额和每日总利润。 I know how to bring back the result for a given week etc I am just really struggling on how to pull back the data. 我知道如何在给定的一周内取回结果,等等,我真的很想如何取回数据。

I also need all the branches to be shown at all time and if they haven't sold anything to display a 0. 我还需要始终显示所有分支,如果它们还没有卖出任何东西来显示0。

I have put together a quick image ( http://i37.photobucket.com/albums/e71/dannyflap/screen_shot.jpg ) of how I would like the finished. 我整理了一下我希望如何完成的快速图片( http://i37.photobucket.com/albums/e71/dannyflap/screen_shot.jpg )。 This is really driving me nuts :( 这真让我发疯:(

UPDATE 更新

select sales_branch, WEEKDAY(sales_date), COUNT(sales_profit), SUM(sales_profit)
FROM sales_date
GROUP BY sales_branch, WEEKDAY(sales_date)

This then brings back the following example. 然后返回下面的示例。 Figures are made up. 数字构成。

sales_branch, day, units, profit:

| branch1 | 0 (as day) | 16 | 439 |

| branch1 | 1 (as day) | 12 | 651 |

| branch1 | 2 (as day) | 22 | 312 |

| branch1 | 3 (as day) | 61 | 614 |

| branch1 | 4 (as day) | 12 | 541 |

| branch1 | 5 (as day) | 24 | 102 |

| branch1 | 6 (as day) | 21 | 145 |

You're going to need a foreign key in sales_data that maps to branch. 您将在sales_data中需要一个映射到分支的外键。

assuming sales_branch is something different than branch_name or branch_id, sales_data should contain: 假设sales_branch与branch_name或branch_id不同,则sales_data应包含:

sales_id sales_date sales_branch branch_id sales_profit

Also - you should post your code for getting back the result for a given week and we can help you build on that rather than give you the entire answer. 另外-您应该发布代码以获取给定一周的结果,我们可以帮助您在此基础上而不是为您提供完整的答案。

EDIT Ok with modifying your sales_data table to have a foreign key branch_id to the branch table, this query should get you closer to what you're looking for. 编辑好,修改sales_data表以使分支表具有外键branch_id,此查询应使您更接近所需的内容。

SELECT b.branch_name, WEEKDAY(s.sales_date), COUNT(s.sales_profit), SUM(s.sales_profit)
FROM branch b
LEFT OUTER JOIN sales_data on s.branch_id = b.branch_id
GROUP BY b.branch_id, WEEKDAY(s.sales_date)

I guess you would have to do an outer join on table 1's branch_id and table 2's sales_branch and loop through the result to produce the table. 我猜您将不得不对表1的branch_id和表2的sales_branch并循环遍历结果以生成表。 With an outer join those branches who haven't sold anything will get a NULL , I think. 我认为,通过外部联接,那些没有出售任何产品的分支机构将获得NULL

Is it the table generating you're having problem with? 它是产生您问题的表格吗? I don't think it's wise to do some spectacular SQL for this problem. 我认为针对此问题执行一些出色的SQL并不明智。 My two cents. 我的两分钱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM