MySql将两个表中的记录相加并与第三个表连接

时间:2017-09-27 13:24:05

标签: mysql

我有3张桌子。表A存储库存和类别关系。添加日期表示库存何时添加到类别中,删除日期表示库存被删除的日期。如果删除日期为空,则表示该股票仍在该类别中。

      +---+----------+  ------- +------------+------------+
      |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 |
      +---+----------+--------  +------------+------------+

表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  |
  +---+------------+----------+-------------+

表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  |
  +---+------------+----------+-------------+

我想要做的是在给定的时间段内,例如从2017-09-04到2017-09-08,1)总结期间的类别金额,2)然后按ID计算所有股票的金额属于该时期的类别。 3)将2除以1来计算比率。对于STOCK ID1,因为它在09-07被删除,sql应该只计算3天记录的金额(09-03 / 09.06)。对于STOCK ID2,它是在09-06添加的,sql应该只计算从09.06到09.08的3天的数量。表C中类别的总金额很简单,只需5天。 结果我期待的是

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

我该怎么办?谢谢大家!

1 个答案:

答案 0 :(得分:0)

我发现代码示例有点棘手,但这是一次尝试。请注意,虽然这可能无法直接解决您的问题,但希望您能够通常应用此方法来解决您的问题。

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;

如果有任何不清楚的地方,请告诉我。

此致

詹姆斯