好吧,我会直接谈到在尝试用Extract制作一本书并且必须连续平衡时呈现给我的案例。
我真的挂在我的咨询中,我找不到任何理想的解决方案。
感谢你们中的一些人做了一些小小的贡献。
我正在寻找类似的东西:
ACCDATE ACCOUNT DEBIT CREDIT BALANCE
2013-01-01 00:00:00 11200 0.00 1500.00 -1500.00
2013-01-01 00:00:00 11200 0.00 60.00 -1560.00
2013-01-01 00:00:00 11200 0.00 400.00 -1960.00
2013-01-01 00:00:00 11200 0.00 100.00 -2060.00
2013-01-01 00:00:00 11200 0.00 300.00 -2360.00
2013-01-01 00:00:00 11200 0.00 250.00 -2910.00
OR:
ACCDATE ACCOUNT DEBIT CREDIT BALANCE
2013-01-01 00:00:00 11200 1500.00 0 1500.00
2013-01-01 00:00:00 11200 0.00 60.00 1440.00
2013-01-01 00:00:00 11200 0.00 400.00 1040.00
2013-01-01 00:00:00 11200 0.00 40 1000.00
2013-01-01 00:00:00 11200 300 0 1300.00
2013-01-01 00:00:00 11200 0.00 250.00 1550.00
此时我真的不需要帐户类型过滤它,但具体。
我的想法是,我的查询仍然没有给我这个结果。
他们可以购买创建临时或临时表格,如下所示:
INSERT INTO Accounting (AccDate,DebitCredit,Account,Amount) VALUES ('20110101','D',11200,1500)
INSERT INTO Accounting (AccDate,DebitCredit,Account,Amount) VALUES ('20110101','C',11200,60)
INSERT INTO Accounting (AccDate,DebitCredit,Account,Amount) VALUES ('20110102','D',11200,400)
INSERT INTO Accounting (AccDate,DebitCredit,Account,Amount) VALUES ('20110102','C',11200,100)
INSERT INTO Accounting (AccDate,DebitCredit,Account,Amount) VALUES ('20110102','C',11200,300)
INSERT INTO Accounting (AccDate,DebitCredit,Account,Amount) VALUES ('20110102','C',11200,250)
WITH CTE_FIRST AS
(
SELECT ACCDATE,
ACCOUNT,
CASE WHEN DEBITCREDIT='D' THEN AMOUNT ELSE 0 END AS DEBIT,
CASE WHEN DEBITCREDIT='C' THEN AMOUNT ELSE 0 END AS CREDIT,
ROW_NUMBER()OVER(ORDER BY ACCOUNT,ACCDATE) RN
FROM ACCOUNTING
WHERE ACCDATE >='20130101'
)
,CTE_SECOND AS(
SELECT *,
ISNULL((SELECT TOP 1 DEBIT FROM CTE_FIRST B WHERE B.ACCOUNT=A.ACCOUNT AND B.RN<A.RN ORDER BY RN DESC),0) COL1,
ISNULL((SELECT TOP 1 CREDIT FROM CTE_FIRST B WHERE B.ACCOUNT=A.ACCOUNT AND B.RN<A.RN ORDER BY RN DESC),0) COL2
FROM CTE_FIRST A
)
SELECT ACCDATE,ACCOUNT,DEBIT,CREDIT,
CASE WHEN DEBIT=0 THEN 0-(CREDIT+COL2) ELSE DEBIT+COL1 END BALANCE
FROM CTE_SECOND
在总和的验证中发生了一些事情,或者它正在采取如此线性的咨询......
所有这些询问,我都试图用网上的材料来帮助我。但它给了我全部回应。
答案 0 :(得分:0)
最后我得到了这个解决方案,但这个过程变得非常缓慢。
有没有办法优化它?
对于真实而言非常慢,并且它不适用于高查询。
DECLARE @T TABLE(FECHA DATETIME,COMENTARIO NVARCHAR(300),CUENTA VARCHAR(15),DEBE NUMERIC(15,2),HABER NUMERIC(15,2))
INSERT INTO @T SELECT FECHA,COMENTARIO,CUENTA,DEBE,HABER FROM DIARIOAPUNTES;
/ * INSERT INTO @T VALUES('001-0001','20130102',100,0);插入@T VALUES('001-0001','20130102',0,200);插入@T VALUES ( '001-0001', '20130102',100,0);插入@T VALUES ( '001-0001', '20130103',100,0);插入@T VALUES ( '001-0001', '20130105',0100);插入@T VALUES ( '001-0002', '20130105',100,0);插入@T VALUES ( '001-0002', '20130106',500,0); * /
- 债务 - 信贷+平衡WITH T_ROW AS( SELECT(ROW_NUMBER()OVER(ORDER BY(T1_1.FECHA)ASC))AS CONTADOR,FECHA,T1_1.CUENTA,T1_1.COMENTARIO,T1_1.DEBE,T1_1.HABER 来自@T T1_1)
SELECT T1.CONTADOR,T1.CUENTA,T1.COMENTARIO as DESCRIPCION, 转换(CHAR(10),T1.FECHA,103)AS FECHA,T1.DEBE,T1.HABER, ROUND(T1.DEBE-T1.HABER + COALESCE(T2.SALDO,0),2)来自T_ROW的AS SALDO T1 交叉申请( SELECT ROUND(SUM(DEBE)-SUM(HABER),2)AS SALDO 来自T_ROW T2 在哪里T2.CONTADOR&lt; T1.CONTADOR和T1.CUENTA = T2.CUENTA )AS T2 - 在'20130101'和'20131231'之间的T1.FECHA 订购T1.CUENTA,T1.FECHA --WHERE T1.ACCOUNTNO ='001-0001'