【问题标题】:Recursive Sql Query 2008递归 SQL 查询 2008
【发布时间】:2015-07-13 11:30:12
【问题描述】:

我需要计算indebtedness 列的值,以便当openingBalance !=0 then indebtedness = openingBalnce+SalesTotal-SalesReturn 时。但是,当之前的monthSalesopeningBalnce = 0 then indebtedness = indebtedness 与相同的SalesID。如果previous value = 0 得到previous value 并继续得到previous value 直到在此列中有值:

SalesMonth SalesID  openingBalance  SalesTotal SalesReturn  Indebtednes
1          1        352200          0          5600         NULL
1          2        50000           1100       0            NULL
1          3        9500            6000       0            NULL
2          1        0               0          1200         NULL
2          2        0               300        0            NULL
2          3        0               500        1000         NULL
3          1        0               600        0            NULL
3          2        0               200        0            NULL
3          3        0               0          10           NULL
.
.
.
12         1        0               0          0            NULL
12         2        0               0          0            NULL
12         3        0               0          0            NULL

输出如下:

 when openingBalance !=0 then Indebtednes=openingBalnce+SalesTotal-SalesReturn

    when openingBalnce =0 then Indebtednes=Indebtednes (of the previous
    month of the same SalesID)+SalesTotal-SalesReturn.

And this is the output i want.

 SalesMonth SalesID  openingBalance  SalesTotal SalesReturn  Indebtednes
    ---------- -------  --------------  ---------- -----------  ------------  
    1           1             352200     0          5600            346600
    ------------------------------------------------------------------------
    1           2             50000     1100         0              51100
    ------------------------------------------------------------------------
    1           3             9500      6000         0              15500
    ------------------------------------------------------------------------
    2           1              0         0          1200            345400
    ------------------------------------------------------------------------
    2           2              0         300          0             51400
    ------------------------------------------------------------------------
    2           3              0         500        1000            15000
    ------------------------------------------------------------------------
    3           1              0         600         0              346000
    ------------------------------------------------------------------------
    3           2              0         200         0              51600
    -----------------------------------------------------------------------
    3           3              0         0           10             14990
    -----------------------------------------------------------------------
    .
    .
    .
    12           1             0          0           0              NULL
    ----------------------------------------------------------------------
    12           2             0          0           0              NULL
    ----------------------------------------------------------------------
    12           3             0          0           0              NULL

【问题讨论】:

  • 你也可以添加预期的输出
  • 对于 salesMonth=1 和 SalesID=1 Indebtednes =346600
  • 我的意思是预期的样本输出,其数据基于问题中的上述样本输入。您可以编辑问题并添加详细信息
  • for salesMonth=1 and SalesID=1 Indebtednes =346600 salesMonth=2 and salesID =1 indebtedness = 346600 and the same case with SalesID =2,3 when openingBalnce !=0 then indebtedness=openingBalnce+SalesTotal -SalesReturn 但是,当openingBalance = 0 获取相同SalesID 的上个月的Indebtednes 时,问题是如果上一个indebtedness 为0 获取上个月的值并继续获取上个月的上一个值直到得到值,怎么办这???

标签: sql-server-2008 recursive-query


【解决方案1】:

您可以执行CROSS APPLY 或共同相关的子查询。像这样的东西。 SQL Fiddle

样本数据

DECLARE @Sales TABLE (SalesMonth INT,SalesID  INT,openingBalance  MONEY,SalesTotal MONEY,SalesReturn  MONEY,Indebtednes MONEY)
insert into @Sales 
VALUES(1,           1,             352200,     0,          5600,            Null),
(1,           2,             50000,     1100,         0,              Null),
(1,           3,             9500,      6000,         0,              Null),
(2,           1,              0,         0,          1200,            Null),
(2,           2,              0,         300,          0,             Null),
(2,           3,              0,         500,        1000,            Null),
(3,           1,              0,         600,         0,              NULL),
(3,           2,              0,         200,         0,              NULL),
(3,           3,              0,         0,           10,             NULL)

选择查询

SELECT C1.SalesMonth,C1.SalesID,C1.openingBalance,C1.SalesTotal,C1.SalesReturn,C2.Indebt
FROM @Sales C1
CROSS APPLY (
SELECT TOP 1 CASE WHEN openingBalance = 0 THEN NULL ELSE openingBalance + SalesTotal - SalesReturn END Indebt
FROM @Sales C2 WHERE C2.SalesID = C1.SalesID AND openingBalance <> 0  AND C2.SalesMonth <=C1.SalesMonth ORDER BY SalesMonth Desc
) C2

更新查询

UPDATE C1 
SET Indebtednes = 
(
SELECT TOP 1 CASE WHEN openingBalance = 0 THEN NULL ELSE openingBalance + SalesTotal - SalesReturn END Indebt
FROM @Sales C2 WHERE C2.SalesID = C1.SalesID AND openingBalance <> 0  AND C2.SalesMonth <=C1.SalesMonth ORDER BY SalesMonth Desc
)
FROM @Sales C1

SELECT * FROM @Sales

【讨论】:

  • 请再次查看我的帖子并给我答案,我用我想要的输出编辑了帖子,谢谢你的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-17
  • 2023-03-22
  • 1970-01-01
  • 2020-11-27
  • 2014-12-19
  • 2012-12-05
  • 2016-01-25
相关资源
最近更新 更多