如何使用其他前一个值来创建新值

时间:2016-11-28 17:05:23

标签: sql-server tsql

这是我的第一篇帖子,通过搜索这个网站找到了我以前需要的东西。我无法找到这个答案,所以我们在这里......我有一个表格,其中包含按月列出的约40个组织的客户数量,样本如下:

Month   OrgID   Cust
201509  G85001  12731
201509  G85006  10638
201509  G85106  2501
201510  G85001  12577
201510  G85006  10550
201510  G85106  2533....

我已经编写了一个脚本来显示月份的数据(感谢这个论坛!),现在看起来像这样:

OrgID   201605  201606  201607   201608  201609
G85001  12650   12650   12597    12597   12597
G85006  11182   11182   11074    11074   11074
G85012  21323   21323   0        0       0
G85034  11956   11956   58648    58648   58648
G85094  15857   15857   0        0       0
G85095  8436    8436    0        0       0
G85106  2429    2429    2426     2426    2426

(已编辑以包含生成此代码的代码:

SELECT        
    CSU.GPWP.[Organisation Code] AS ORGID, 
    SUM(CASE WHEN monthkey = '201603' THEN Customers ELSE 0 END) AS 201603, 
    SUM(CASE WHEN monthkey = '201604' THEN Customers ELSE 0 END) AS 201604, 
    SUM(CASE WHEN monthkey = '201605' THEN Customers ELSE 0 END) AS 201605, 
    SUM(CASE WHEN monthkey = '201606' THEN Customers ELSE 0 END) AS 201606, 
.....
FROM            CSU.GPWP 

GROUP BY CSU.GPWP.[Organisation Code]

你可以看到G85012,G85094和G85095都在第三列中变为0并且G85034增加了,这是因为这三个分支在技术上是关闭的'他们的客户名单已由G85034承担。不幸的是,并非所有系统都已关闭,因此我仍需要报告每个站点。我想要做的是有0个客户的月份,使用最后一个月来估算客户规模,基于当前的总客户。

所以,对于六月:

G85012  = 21323 (37%)
G85094  = 15857 (28%)
G85095  = 8436 (15%)
G85034  = 11956 (21%)
Total =  57572

我希望7月如下: 总计58648(总共G85034)

G85012  = 37%*58648 = 21722
G85094  = 28%*58648 = 16153
G85095  = 15%*58648 = 8594
G85034(new) = 21%*58648 = 12179

对于所有其他网站,我希望保留其现有的客户列表大小。是否有可能让SQL持续计算?提前谢谢!

1 个答案:

答案 0 :(得分:0)

当你需要纠正联合记录G85034时,CUST = 0的条件是不够的,其中CUST<> 0

说我们有下表:

CREATE TABLE GPWP 
    ([Month] int, [OrgID] varchar(6), [Cust] int)
;

INSERT INTO GPWP 
    ([Month], [OrgID], [Cust])
VALUES
    (201605, 'G85001', '12650'),
    (201605, 'G85006', '11182'),
    (201605, 'G85012', '21323'),
    (201605, 'G85034', '11956'),
    (201605, 'G85094', '15857'),
    (201605, 'G85095', '8436'),
    (201605, 'G85106', '2429'),                

    (201606, 'G85001', '12650'),
    (201606, 'G85006', '11182'),
    (201606, 'G85012', '21323'),
    (201606, 'G85034', '11956'),
    (201606, 'G85094', '15857'),
    (201606, 'G85095', '8436'),
    (201606, 'G85106', '2429'),     

    (201607, 'G85001', '12597'),
    (201607, 'G85006', '11074'),
    (201607, 'G85012', '0'),
    (201607, 'G85034', '58648'),
    (201607, 'G85094', '0'),
    (201607, 'G85095', '0'),
    (201607, 'G85106', '2429'),   

    (201608, 'G85001', '12597'),
    (201608, 'G85006', '11074'),
    (201608, 'G85012', '0'),
    (201608, 'G85034', '58648'),
    (201608, 'G85094', '0'),
    (201608, 'G85095', '0'),
    (201608, 'G85106', '2429'),   

    (201609, 'G85001', '12597'),
    (201609, 'G85006', '11074'),
    (201609, 'G85012', '0'),
    (201609, 'G85034', '58648'),
    (201609, 'G85094', '0'),
    (201609, 'G85095', '0'),
    (201609, 'G85106', '2429')
    ;  

我们需要知道哪些记录联合起来,以及联合记录中每条记录的份额。所以你应该创建下表:

CREATE TABLE GPWP_UNIONS
    ([StartMonth] int,[EndMonth] int, 
     [OrgID] varchar(6), [ParentOrgID] varchar(6), 
     [Share] money);
;

INSERT INTO GPWP_UNIONS
    ([StartMonth],[EndMonth], [OrgID], [ParentOrgID], [Share])
VALUES
    (201607,201609, 'G85012', 'G85034',0.37),
    (201607,201609, 'G85094', 'G85034',0.28),
    (201607,201609, 'G85095', 'G85034',0.15),
    (201607,201609, 'G85034', 'G85034',0.21);   

这里StartMonth是工会的月份。 EndMonth最后一个月如果您需要在本月取回并取消工会(如果您需要无休止的工会,可以使用999999)。 ParentId是联盟记录的OrgId。 Share是总和中每条记录的百分比/ 100。

使用以下查询,我们得到了所需的结果:

WITH GPWP_WITH_UNIONS AS
(
  SELECT GPWP.OrgID,
         GPWP.month,
         ISNULL(GPWP_PARENT.Cust*GPWP_UNIONS.Share,GPWP.Cust) as Cust

  FROM GPWP
  -- Get a Share If it is an UNION record
  LEFT JOIN GPWP_UNIONS ON (GPWP.OrgID = GPWP_UNIONS.OrgID)
                       AND 
                      (GPWP.month BETWEEN GPWP_UNIONS.StartMonth 
                                      AND GPWP_UNIONS.EndMonth) 
  -- get the Parent CUST value if it is an UNION record
  LEFT JOIN GPWP AS GPWP_PARENT ON (GPWP_UNIONS.ParentOrgID = GPWP_PARENT.OrgID)
                       AND 
                      (GPWP.month = GPWP_PARENT.month)                       

)
SELECT        
    OrgID AS ORGID, 
    SUM(CASE WHEN month = '201603' THEN Cust ELSE 0 END) AS [201603], 
    SUM(CASE WHEN month = '201604' THEN Cust ELSE 0 END) AS [201604], 
    SUM(CASE WHEN month = '201605' THEN Cust ELSE 0 END) AS [201605], 
    SUM(CASE WHEN month = '201606' THEN Cust ELSE 0 END) AS [201606],
    SUM(CASE WHEN month = '201607' THEN Cust ELSE 0 END) AS [201607],
    SUM(CASE WHEN month = '201608' THEN Cust ELSE 0 END) AS [201608],
    SUM(CASE WHEN month = '201609' THEN Cust ELSE 0 END) AS [201609]   
FROM  GPWP_WITH_UNIONS 

GROUP BY GPWP_WITH_UNIONS.ORGID;

结果:

╔════════╦════════╦════════╦══════════╦══════════╦══════════╦══════════╦══════════╗
║ ORGID  ║ 201603 ║ 201604 ║  201605  ║  201606  ║  201607  ║  201608  ║  201609  ║
╠════════╬════════╬════════╬══════════╬══════════╬══════════╬══════════╬══════════╣
║ G85001 ║ 0.00   ║ 0.00   ║ 12650.00 ║ 12650.00 ║ 12597.00 ║ 12597.00 ║ 12597.00 ║
║ G85006 ║ 0.00   ║ 0.00   ║ 11182.00 ║ 11182.00 ║ 11074.00 ║ 11074.00 ║ 11074.00 ║
║ G85012 ║ 0.00   ║ 0.00   ║ 21323.00 ║ 21323.00 ║ 21699.76 ║ 21699.76 ║ 21699.76 ║
║ G85034 ║ 0.00   ║ 0.00   ║ 11956.00 ║ 11956.00 ║ 12316.08 ║ 12316.08 ║ 12316.08 ║
║ G85094 ║ 0.00   ║ 0.00   ║ 15857.00 ║ 15857.00 ║ 16421.44 ║ 16421.44 ║ 16421.44 ║
║ G85095 ║ 0.00   ║ 0.00   ║ 8436.00  ║ 8436.00  ║ 8797.20  ║ 8797.20  ║ 8797.20  ║
║ G85106 ║ 0.00   ║ 0.00   ║ 2429.00  ║ 2429.00  ║ 2429.00  ║ 2429.00  ║ 2429.00  ║
╚════════╩════════╩════════╩══════════╩══════════╩══════════╩══════════╩══════════╝