我有一张这样的表:
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
NB。年份和月份列是花车,也想将它们更改为日期格式。
我是SQL和水晶报道的新手。我想要一个查询来帮助我完成开发具有期初和期末余额列的报表的任务,期初余额是之前的期末余额。
答案 0 :(得分: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;
这会给你:
| 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 |
根据您的问题,此查询将评估CLOSINGBALANCE
,OPENINGBALANCE, and
CURRENTMONTHBALANCE`,如下所示:
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;
这将为您提供相同的输出。