在新列中引入计算值

时间:2014-07-09 07:30:45

标签: mysql sql sql-server-2008

我想介绍一个新列,其中包含下三个周期的计算总和。

这是样本数据

Company Value   Period
------- -----   ------
MGT     9       1401
MGT     2       1402
MGT     3       1403
MGT     4       1404
MGT     5       1405
MGT     6       1406
MGT     7       1407
MGT     8       1408
MGT     9       1409
MGT     10      1410
MGT     11      1411
MGT     12      1412
MGT     3       1501

这是预期数据

Company Value   Period  New_Column
------- -----   ------  ----------
MGT     9       1401    9
MGT     2       1402    12
MGT     3       1403    15
MGT     4       1404    18
MGT     5       1405    21
MGT     6       1406    24
MGT     7       1407    27
MGT     8       1408    30
MGT     9       1409    33
MGT     10      1410    26
MGT     11      1411    15
MGT     12      1412    3
MGT     3       1501    0

New_Column包含未来三个月的值。 例如,考虑列Period 1403(March'14),其中值 3 ,New_Column现在应该添加接下来三个月的值 即,[1404(四月'14)+ 1404(五月'14)+ 1405(六月'))的值 [4 + 5 + 6] = 15 < / p>

3 个答案:

答案 0 :(得分:2)

 DROP TABLE IF EXISTS my_table;

 CREATE TABLE my_table
 (company CHAR(3) NOT NULL
 ,dt DATE NOT NULL
 ,value INT NOT NULL
 ,PRIMARY KEY(company,dt)
 );

 INSERT INTO my_table VALUES
 ('MGT',20140101,9),
 ('MGT',20140201,2),
 ('MGT',20140301,3),
 ('MGT',20140401,4),
 ('MGT',20140501,5),
 ('MGT',20140601,6),
 ('MGT',20140701,7),
 ('MGT',20140801,8),
 ('MGT',20140901,9),
 ('MGT',20141001,10),
 ('MGT',20141101,11),
 ('MGT',20141201,12),
 ('MGT',20150101,3);

 mysql> SELECT * FROM my_table;
 +---------+------------+-------+
 | company | dt         | value |
 +---------+------------+-------+
 | MGT     | 2014-01-01 |     9 |
 | MGT     | 2014-02-01 |     2 |
 | MGT     | 2014-03-01 |     3 |
 | MGT     | 2014-04-01 |     4 |
 | MGT     | 2014-05-01 |     5 |
 | MGT     | 2014-06-01 |     6 |
 | MGT     | 2014-07-01 |     7 |
 | MGT     | 2014-08-01 |     8 |
 | MGT     | 2014-09-01 |     9 |
 | MGT     | 2014-10-01 |    10 |
 | MGT     | 2014-11-01 |    11 |
 | MGT     | 2014-12-01 |    12 |
 | MGT     | 2015-01-01 |     3 |
 +---------+------------+-------+

 SELECT x.*
      , COALESCE(SUM(y.value),0) new_val 
   FROM my_table x 
   LEFT
   JOIN my_table y 
     ON y.company = x.company 
    AND y.dt BETWEEN x.dt + INTERVAL 1 MONTH AND x.dt + INTERVAL 3 MONTH 
  GROUP 
     BY x.company
      , x.dt;
 +---------+------------+-------+---------+
 | company | dt         | value | new_val |
 +---------+------------+-------+---------+
 | MGT     | 2014-01-01 |     9 |       9 |
 | MGT     | 2014-02-01 |     2 |      12 |
 | MGT     | 2014-03-01 |     3 |      15 |
 | MGT     | 2014-04-01 |     4 |      18 |
 | MGT     | 2014-05-01 |     5 |      21 |
 | MGT     | 2014-06-01 |     6 |      24 |
 | MGT     | 2014-07-01 |     7 |      27 |
 | MGT     | 2014-08-01 |     8 |      30 |
 | MGT     | 2014-09-01 |     9 |      33 |
 | MGT     | 2014-10-01 |    10 |      26 |
 | MGT     | 2014-11-01 |    11 |      15 |
 | MGT     | 2014-12-01 |    12 |       3 |
 | MGT     | 2015-01-01 |     3 |       0 |
 +---------+------------+-------+---------+

答案 1 :(得分:1)

您可以执行类似

的操作

您的表格定义

CREATE TABLE TestTable(
Company nvarchar(50) NOT NULL,
Value int NOT NULL,
Period char(4) NOT NULL  
);

INSERT INTO TestTable
(Company, Value, Period)
VALUES
('MGT', 9, 1401),
('MGT', 11,1402),
('MGT', 5, 1403),
('MGT', 6, 1404);

然后执行以下查询

创建临时表

CREATE TEMPORARY TABLE IF NOT EXISTS TempTable AS 
(SELECT 
CAST(SUBSTRING(Period, 1, 2) AS UNSIGNED) AS Year,
CAST(SUBSTRING(Period, 3, 2) AS UNSIGNED) AS Month,
Value,
Company
FROM TestTable)

以后直接

SELECT Company, Month, Year, 

Value

+

IFNULL((SELECT Value FROM TempTable SUBQUERY_02
WHERE SUBQUERY_02.Month = 
CASE 
    WHEN SUBQUERY_01.Month = 12 THEN 1 
    ELSE SUBQUERY_01.Month + 1 
END
AND SUBQUERY_02.Year =
CASE 
    WHEN SUBQUERY_01.Month = 12 THEN SUBQUERY_01.Year + 1
    ELSE SUBQUERY_01.Year
END), 0)

+

IFNULL((SELECT Value FROM TempTable SUBQUERY_03
WHERE SUBQUERY_03.Month = 
CASE 
    WHEN SUBQUERY_01.Month = 11 THEN 1 
    WHEN SUBQUERY_01.Month = 12 THEN 2 
    ELSE SUBQUERY_01.Month + 2
END
AND SUBQUERY_03.Year =
CASE 
    WHEN SUBQUERY_01.Month >= 11 THEN SUBQUERY_01.Year + 1
    ELSE SUBQUERY_01.Year
END), 0)

FROM TempTable SUBQUERY_01

答案 2 :(得分:0)

我在MS SQL Server 2008中使用了以下代码

select t1.Company, t1.value value, t1.period period, 
isnull(t2.value,0) 
+ isnull(t3.value,0)
+ isnull(t4.value,0) New_Column
  from @NOA t1
     left join @NOA t2 on (t1.period % 100 < 12 and t1.period = t2.period - 1)
                       or (t1.period % 100 >= 12 and t1.period = t2.period - 89)
     left join @NOA t3 on (t1.period % 100 < 11 and t1.period = t3.period - 2)
                       or (t1.period % 100 >= 11 and t1.period = t3.period - 90)
     left join @NOA t4 on (t1.period % 100 < 10 and t1.period = t4.period - 3)
                       or (t1.period % 100 >= 10 and t1.period = t4.period - 91)

提供预期的结果

Company Value   Period  New_Column
------- -----   ------  ----------
MGT     9       1401    9
MGT     2       1402    12
MGT     3       1403    15
MGT     4       1404    18
MGT     5       1405    21
MGT     6       1406    24
MGT     7       1407    27
MGT     8       1408    30
MGT     9       1409    33
MGT     10      1410    26
MGT     11      1411    15
MGT     12      1412    3
MGT     3       1501    0

谢谢@all:)