2015-10-15 75 views
0

我試圖從數據庫開始到今年9月選擇總帳帳戶的餘額。我在WHERE子句中遇到了困難。我需要捕獲前幾年的所有十二個月數據,但僅在2015年9月之前。數據庫中沒有公佈日期屬性。我的查詢現在如何編寫,它只返回每年的前9個月。累計日期小於或等於期間日期 - T-SQL

SELECT 
t_leac AS LedgerAccount, 
SUM(t_amnt) AS Debit, 
t_dbcr AS debit_credit 
FROM  
ttfgld106100 
WHERE 
t_fyer <= 2015 and t_fprd IN ('1','2','3','4','5','6','7','8','9') 
GROUP BY 
t_leac, 
t_dbcr 
ORDER BY 
t_leac; 

由於

UPDATE:

我跑下面的查詢和檢索到的最小值爲t_fyer(數字)列1999和t_fprd(數字)列作爲1的最小值。下面是結果的樣子:

t_fyer t_fprd 
1999  1 
1999  2 
1999  3 
....  .... 
1999  13 
2000  1 
....  .... 


SELECT DISTINCT 
t_fyer, 
t_fprd 
FROM 
ttfgld106100 
ORDER BY 
t_fyer, t_fprd 
+0

我們根本不知道,因爲我們不知道數據是什麼。什麼是最短日期?你檢查過了嗎?什麼是t_fyer列的類型? – Hozikimaru

+0

't_fprd',是數字或文本字段嗎? – Caramiriel

+0

這兩列都是數字。最短日期是t_fyer:1999和t_fprd:1.我正在嘗試將t_amnt的總和提高到t_fyer:2015和t_fprd:9.謝謝 – Andrew

回答

0

我在select語句中添加了一個子查詢,發現了我的問題的答案。我加入了自己的表格,增加了截至2014年(包括2014年)在內的累計總數,並將2015年的數據增加到了9月份。作爲參考,這裏是我的代碼:

SELECT 
a.t_leac AS LedgerAccount, 
(SUM(a.t_amnt) + (SELECT SUM(b.t_amnt) FROM ttfgld106100 b WHERE b.t_fyer <=2014 and a.t_leac = b.t_leac)) AS Amount, 
a.t_dbcr AS debit_credit 
FROM 
ttfgld106100 a 
WHERE 
a.t_fyer = 2015 and a.t_fprd IN ('1','2','3','4','5','6','7','8','9') 
GROUP BY 
a.t_leac, 
a.t_dbcr 
ORDER BY 
a.t_leac;