简体   繁体   English

MySql对两个表中的记录求和,并与第三个表联接

[英]MySql Sum the records in two tables and join with the third table

I have 3 tables. 我有3张桌子。 Table A stores stock and category relationship. 表A存储库存和类别关系。 Add date indicates when the stock was added to the category and remove date means the day which the stock was removed. 添加日期表示将库存添加到类别的时间,删除日期表示删除库存的日期。 if remove date is null , it means the stock is still in the category. 如果删除日期为null,则表示库存仍在类别中。

      +---+----------+  ------- +------------+------------+
      |id | Stock_id |Category  | Add Date   | Remove Date|
      +---+----------+--------  +------------+------------+
      | 1 |        1 | CategoryA| 2017-09-03 | 2017-09-07 |
      | 2 |        1 | CategoryA|2017-09-11  | null       |
      | 3 |        2 | CategoryA| 2017-09-06 | null |
      +---+----------+--------  +------------+------------+

Table B stores stock transaction amount by days. 表B按天存储库存交易量。

  +---+------------+----------+-------------+
  |id | Stock_id   | amount   |  Date       |
  +---+------------+----------+-------------+
  | 1 |          1 |      100 | 2017-09-04  |
  | 2 |          1 |      100 | 2017-09-05  |
  | 3 |          1 |      100 | 2017-09-06  |
  | 4 |          1 |      100 | 2017-09-07  |
  | 5 |          1 |      100 | 2017-09-08  |
  | 6 |          1 |      100 | 2017-09-09  |
  | 7 |          2 |      100 | 2017-09-05  |
  | 8 |          2 |      200 | 2017-09-06  |
   ....
  | 2 |          2 |      200 | 2017-09-10  |
  +---+------------+----------+-------------+

Table C stores category transaction amount by days. 表C按天存储类别交易金额。

  +---+------------+----------+-------------+
  |id | Category   | amount   |  Date       |
  +---+------------+----------+-------------+
  | 1 |          A |      300 | 2017-09-04  |
  | 2 |          A |      300 | 2017-09-05  |
  | 3 |          A |      300 | 2017-09-06  |
  | 4 |          A |      300 | 2017-09-07  |
  | 5 |          A |      300 | 2017-09-08  |
  +---+------------+----------+-------------+

What I want to do is in a given period, such as from 2017-09-04 to 2017-09-08, 1)sum the category amount in the period, 2)and then sum all the stock's amount by id which is in the category in the period. 我想做的是在给定的时间段内,例如从2017-09-04到2017-09-08,1)将期间内的类别金额相加,2)然后通过ID将所有股票金额相加期间中的类别。 3) the divide the 2 by 1 to calculate the ratio. 3)用2除以1来计算比率。 for STOCK ID1, for it was removed on 09-07, sql should only calculate it's amount for 3 days records(09-03/09.06). 对于库存ID1,因为它已于09-07删除,因此sql只能计算3天记录的数量(09-03 / 09.06)。 for STOCK ID2, it was added on 09-06, sql should only calculate it's amount for 3 days from 09.06 to 09.08. 对于STOCK ID2,它是在09-06上添加的,sql应该只计算从09.06到09.08的3天的金额。 The sum amount for category in Table C is simple, just sum 5 days. 表C中类别的总金额很简单,总和为5天。 The result What I expect is 我期望的结果是

  +---+----------+  ------- +
  |id | Stockid  |Result    |
  +---+----------+--------  +
  | 1 |        1 | 0.2      |     # (100+100+100)/300*5            
  | 2 |        2 | 0.4      |     # (200+200+200)/300*5

How can I do it? 我该怎么做? Thank you all! 谢谢你们!

I found the code example a bit tricky to follow but here's an attempt. 我发现遵循此代码示例有些棘手,但这是一个尝试。 Note that although this may not solve your problem directly, hopefully you will be able to apply the approach in general to solve your issue. 请注意,尽管这可能无法直接解决您的问题,但希望您能够总体上采用该方法来解决您的问题。

CREATE TABLE tableA(id INT, stockId INT, category CHAR(1), addDate DATE, removeDate DATE);

INSERT INTO tableA VALUES
  (1, 1, 'A', '2017-09-03', '2017-09-07'),
  (2, 1, 'A', '2017-09-11', null),
  (3, 2, 'A', '2017-09-06', null);

CREATE TABLE tableB(id INT, stockId INT, amount INT, `date` DATE);

INSERT INTO tableB VALUES
  (1, 1, 100, '2017-09-04'),
  (2, 1, 100, '2017-09-05'),
  (3, 1, 100, '2017-09-06'),
  (4, 1, 100, '2017-09-07'),
  (5, 1, 100, '2017-09-08'),
  (6, 1, 100, '2017-09-09'),
  (7, 2, 100, '2017-09-05'),
  (8, 2, 200, '2017-09-06'),
  (9, 2, 200, '2017-09-06'),
  (10, 2, 200, '2017-09-10');

CREATE TABLE tableC (id INT, category CHAR(1), amount INT, `date` DATE);

INSERT INTO tableC VALUES
  (1, 'A', 300, '2017-09-04'),
  (2, 'A', 300, '2017-09-05'),
  (3, 'A', 300, '2017-09-06'),
  (4, 'A', 300, '2017-09-07'),
  (5, 'A', 300, '2017-09-08');

SELECT table_AB.tableAB_SUM / table_B.tableB_SUM * TIMESTAMPDIFF(DAY, '2017-09-04', '2017-09-08') FROM  
    (
    SELECT tableB.stockId, SUM(tableB.amount) tableAB_SUM FROM 
        tableB
    LEFT OUTER JOIN
        tableA
    ON tableB.stockId = tableA.stockId
    WHERE tableB.`date` BETWEEN '2017-09-04' AND '2017-09-08' AND (NOT tableA.addDate > '2017-09-08' OR NOT IFNULL(tableA.removeDate, CURDATE()) < '2017-09-08')
    GROUP BY tableA.stockId
    ) table_AB
LEFT OUTER JOIN
    (
    SELECT stockId, SUM(amount) tableB_SUM FROM tableB WHERE `date` BETWEEN '2017-09-04' AND '2017-09-08' GROUP BY stockId
    ) table_B
ON table_AB.stockID = table_B.stockID;

Let me know if anythings unclear. 让我知道是否有任何不清楚的地方。

Regards, 问候,

James 詹姆士

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

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