2013-08-05 80 views
0

那麼,我將直接討論在嘗試製作「提取」並且必須持續平衡時提供給我的案例。嘗試在借方和貸方之間創建一個餘額

我真的掛在我的諮詢,我找不到任何需要的解決方案。

感謝會在一些小的貢獻給你們中的一些人。

我正在尋找這樣的事情:

 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

最後我得到了這個解決方案,但是這個過程變得非常緩慢。

有沒有一種方法來優化它?

真的很慢,而且它不適用於高查詢。

DECLARE @T TABLE(日期星DATETIME,COMENTARIO NVARCHAR(300),CUENTA VARCHAR(15),德貝NUMERIC(15,2),HABER NUMERIC(15,2))

INSERT INTO @ T SELECT FECHA,COMENTARIO,CUENTA,DEBE,HABER從 DIARIOAPUNTES;

/* INSERT INTO @T VALUES('001-0001','20130102',100,0); INSERT INTO @T VALUES('001-0001','20130102',0,200);插入@T值 ('001-0001','20130102',100,0);插入@T值 ('001-0001','20130103',100,0);插入@T值 ('001-0001','20130105',0,100);插入@T值 ('001-0002','20130105',100,0);插入@T值 ('001-0002','20130106',500,0); */
--DEBIT - CREDIT + BALANCE

WITH T_ROW AS( SELECT(ROW_NUMBER()OVER(ORDER BY(T1_1.FECHA)ASC))AS康塔,出生日期,T1_1.CUENTA,T1_1.COMENTARIO

,T1_1.DEBE,T1_1.HABER FROM @T T1_1)

SELECT T1.CONTADOR,T1.CUENTA,T1.COMENTARIO AS DESCRIPCION, 轉換(CHAR(10),T1.FECHA,103)AS日期星, T1.DEBE,T1.HABER, ROUND(T1.DEBE-T1.HABER + COALESCE(T2。SALDO,0),2)AS SALDO FROM T_ROW T1 CROSS APPLY( SELECT ROUND(SUM(德貝)-sum(HABER),2)AS SALDO FROM T_ROW T2 WHERE T2.CONTADOR < T1.CONTADOR和T1 .CUENTA = T2.CUENTA )AS T2 --where T1.FECHA '之間20130101' AND '20131231' ORDER BY T1.CUENTA,T1.FECHA --where T1.ACCOUNTNO = '001-0001'

相關問題