1

我有一张这样的桌子:

StockNumber|InventoryName|Year|Month|Adj|iss|piss|Tsfr|return|rdj|rpo|xefr
alb001      clinic1       2010  1     4   5    5   5    6      5   4   10
alb001      Clinic1       2010  2    10  2    2    3    3      4   4   4
alb001      Clinic1       2010  4    11  3   5    77    90     78   9   6
alb001      Clinic1       2010  5    10  2    2    3    3      4   4   4

我想添加一个期末余额列,它将是

sum(return+rdj+rpo+xefr) - sum(adj+iss+piss+tsfr)

我还想添加期初余额列,这将是上个月的期末余额。

然后,我将计算当前月份的余额:

OpeningBalance + sum(return+rdj+rpo+xefr) - sum(adj+iss+piss+tsfr) = ClosingBalance

注意。Year 和 Month 列是浮点数,也希望将它们更改为日期格式。

我是 SQL 和水晶报表的新手。我想要一个查询来帮助我完成开发具有期初余额和期末余额列的报表的任务,期初余额是上一个期末余额。

4

1 回答 1

2

你可以这样做:

;WITH WithClosingBalances
AS
(
    SELECT
      [StockNumber], 
      [InventoryName], 
      [Year], 
      [Month], 
      [Adj], 
      [iss],
      [piss], 
      [Tsfr], 
      [return], 
      [rdj], 
      [rpo], 
      [xefr],
      ([return] + rdj + rpo + xefr) -
      (adj+iss+piss+tsfr) ClosingBalance,
      ROW_NUMBER() OVER(PARTITION BY INVENTORYNAME
                        ORDER BY month) rownum
    FROM table1
), WithOpeningBalances
AS
(
    SELECT
      [StockNumber], 
      [InventoryName], 
      [Year], 
      [Month], 
      [Adj], 
      [iss],
      [piss], 
      [Tsfr], 
      [return], 
      [rdj], 
      [rpo], 
      [xefr],
      ClosingBalance,
      ISNULL((SELECT ClosingBalance 
             FROM WithClosingBalances c2
             WHERE c1.rownum - c2.rownum =1
            ), 0) OpeningBalance,
      rownum
    FROM WithClosingBalances c1
) 
SELECT
  [StockNumber], 
  [InventoryName], 
  [Year], 
  [Month], 
  [Adj], 
  [iss],
  [piss], 
  [Tsfr], 
  [return], 
  [rdj], 
  [rpo], 
  [xefr],
  ClosingBalance,
  OpeningBalance,
  OpeningBalance + ClosingBalance AS CurrentMonthBalance
FROM WithOpeningBalances;

SQL 小提琴演示

这会给你:

| STOCKNUMBER | INVENTORYNAME | YEAR | MONTH | ADJ | ISS | PISS | TSFR | RETURN | RDJ | RPO | XEFR | CLOSINGBALANCE | OPENINGBALANCE | CURRENTMONTHBALANCE |
------------------------------------------------------------------------------------------------------------------------------------------------------------
|      alb001 |       clinic1 | 2010 |     1 |   4 |   5 |    5 |    5 |      6 |   5 |   4 |   10 |              6 |              0 |                   6 |
|      alb001 |       Clinic1 | 2010 |     2 |  10 |   2 |    2 |    3 |      3 |   4 |   4 |    4 |             -2 |              6 |                   4 |
|      alb001 |       Clinic1 | 2010 |     4 |  11 |   3 |    5 |   77 |     90 |  78 |   9 |    6 |             87 |             -2 |                  85 |
|      alb001 |       Clinic1 | 2010 |     5 |  10 |   2 |    2 |    3 |      3 |   4 |   4 |    4 |             -2 |             87 |                  85 |

这个查询是如何工作的?

我从您的问题中了解到,此查询将评估CURRENTMONTHBALANCE CLOSINGBALANCEOPENINGBALANCE, and如下所示:

  • CLOSINGBALANCE=

      [return] + rdj + rpo + xefr) -
      (adj+iss+piss+tsfr)
    

    每个月。我假设每个月的表中的每条记录。

  • OPENINGBALANCE: 是CLOSINGBALANCE按库存名称分组的上个月。这是使用ROW_NUMBER() OVER(PARTITION BY InventoryName).

  • CURRENTMONTHBALANCE=OpeningBalance + ClosingBalance每条记录。


更新:JOIN如果有多个月份的条目,您可以使用而不是相关子查询,如下所示:

;WITH WithClosingBalances
AS
(
    SELECT
      [StockNumber], 
      [InventoryName], 
      [Year], 
      [Month], 
      [Adj], 
      [iss],
      [piss], 
      [Tsfr], 
      [return], 
      [rdj], 
      [rpo], 
      [xefr],
      ([return] + rdj + rpo + xefr) -
      (adj+iss+piss+tsfr) ClosingBalance,
      ROW_NUMBER() OVER(PARTITION BY INVENTORYNAME
                        ORDER BY month) rownum
    FROM table1
), WithOpeningBalances
AS
(
    SELECT
      [StockNumber], 
      [InventoryName], 
      [Year], 
      [Month], 
      [Adj], 
      [iss],
      [piss], 
      [Tsfr], 
      [return], 
      [rdj], 
      [rpo], 
      [xefr],
      c1.ClosingBalance,
      ISNULL(c2.ClosingBalance, 0) OpeningBalance
    FROM WithClosingBalances c1
    LEFT JOIN
    (
      SELECT rownum, SUM(ClosingBalance) ClosingBalance
      FROM WithClosingBalances
      GROUP BY rownum
    ) c2 ON c1.rownum - c2.rownum = 1
) 
SELECT
  [StockNumber], 
  [InventoryName], 
  [Year], 
  [Month], 
  [Adj], 
  [iss],
  [piss], 
  [Tsfr], 
  [return], 
  [rdj], 
  [rpo], 
  [xefr],
  ClosingBalance,
  OpeningBalance,
  OpeningBalance + ClosingBalance AS CurrentMonthBalance
FROM WithOpeningBalances;

这将为您提供相同的输出。

SQL 小提琴演示

于 2012-12-17T10:56:29.690 回答