2010-01-07 30 views
0

首先,讓我解釋一下表結構。SQL查詢解決方案 - 逐日餘額

它如下。

  • 主表 - CategoryGroup
  • 領域 - CatGroupID,組名,GroupType
  • 樣本數據

    CatGroupID | GroupName | GroupType
    1 |實用程序|費用
    2 |自動|費用
    3 |雜項。 |費用
    4 |收入|收入
    5 | HouseHold |費用

  • 子表 - TransactionCategory

  • 領域 - 類別ID,CatGroupID,CatName,CatImgName
  • 樣本數據

    類別ID | CatGroupID | CatName | CatImgName
    1 | 1 |水| Water.png
    2 | 1 |電話| Phone.png
    3 | 1 |電力| Ele.png
    4 | 2 |自動| Auto.png
    5 | 2 |燃氣| Gas.png

  • 兒童兒童表 - TransactionTbl

  • 領域 - ROWID,用戶名,簡檔,類別ID,三聯體,特拉達泰,TraAmt
  • 樣本數據

    ROWID(自動遞增)|用戶ID | ProfileID | CategoryID | TraID | TraDate | TraAmt
    1 | 1 | 1 | 1 | 1 | 2010-1-1 | 1000.00
    2 | 1 | 1 | 1 | 2 | 2010-1-2 | 250.00

我已經嘗試了很多疑問 - 其中之一是如下,

選擇A.TraDate,A.TraAmt,C.GroupType,B.CatName從transactionTbl A,TransactionCategory乙,CategoryGroupç 其中A.TraDate = '2010-01-11' 和A.CAtegoryID = B.CategoryID和B.CatGroupID = C.CatGroupID

我使用SQLite iPhone開發&。我對數據庫管理&查詢&連接的想法很少。正因爲如此,我不得不在這裏問。

Oke。我需要的輸出如下。

TraID | TraDate | CatName |金額|平衡|
1。| 2010-1-1 |收入| 5000.00 | 5000.00 |
2. | 2010-1-2 |自動| 250.00 | 4750.00 |
3. | 2010-1-3 |燃氣| 200.00 | 4550.00 |
4. | 2010-1-4 |收入| 500 | 5050.00 |

請注意上面的每一行。

  • 如果我有收入的事務,它應該增加平衡&
  • 如果我有代價的交易,應該從平衡中減去。

我想從過去兩天有這種輸出。但我無法得到它。所以,最後我問到了。

非常感謝您的幫助。

薩加爾。

編輯:

我已經找到了一些解決方案 - 一點點。但它不是我想要的。

select A.traID,B.CatName,C.GroupType,A.TraDate,A.TraAmt from transactionTbl A,TransactionCategory B,CategoryGroup C 
where A.CategoryID=B.CategoryID and B.CatGroupID=C.CatGroupID 
order by A.TraDate,A.TraID 

它給出如下輸出。

三聯體CatName GroupType特拉達泰TraAmt
----- ------- --------- ------- ------
1收入收入2010-01-01 5000
2收入收入2010-01-02 4500
3水費用2010-01-03 450
4電話費用2010-01-03 450
5電費2010-01-03 650
6汽車消費2010-01-03 750
7汽車消費2010-01-03 450
8汽油消費2010-01-05 7 50
9雜項費用2010-01-06 800
10健康費用2010-01-13 250
11所得稅費用2010-01-21 450
12投資費用2010-01-22 450
13張信用卡費用2010-01-27 550
14吃掉費用2010-01-29 450

剩下的最後一件事情就是平衡管理。 喜歡這裏 - 餘額應爲如下

平衡 1. 5000 2. 9500 3. 9050等。

+0

您是否希望從指定的第一個日期開始,或僅在事務發生日期的每一天輸出? – lins314159 2010-01-07 21:36:59

+0

每筆交易後的餘額。不是一天一天。 Sir @ lins313159 - 我編輯了我的問題。一探究竟。如果需要更多信息,請通知我。 – 2010-01-07 21:44:31

回答

2

雖然這給出了正確的結果,但我不確定在性能方面它是否是在MySQL中實現它的最佳方式。

SELECT 
    A.traID, 
    B.CatName, 
    C.GroupType, 
    A.TraDate, 
    A.TraAmt, 
    SUM(D.TraAmt) as Balance 
FROM transactionTbl A 
INNER JOIN TransactionCategory B 
    ON A.CategoryID=B.CategoryID 
INNER JOIN CategoryGroup C 
    ON B.CatGroupID=C.CatGroupID 
INNER JOIN (
     SELECT 
      A.TraDate, 
      CASE WHEN C.GroupType = 'Income' THEN A.TraAmt ELSE -A.TraAmt END AS TraAmt 
     FROM transactionTbl A 
     INNER JOIN TransactionCategory B 
      ON A.CategoryID=B.CategoryID 
     INNER JOIN CategoryGroup C 
      ON B.CatGroupID=C.CatGroupID) D 
    ON A.TraDate >= D.TraDate 
GROUP BY 
    A.traID, 
    B.CatName, 
    C.GroupType, 
    A.TraDate, 
    A.TraAmt 
ORDER BY 
    A.TraDate, 
    A.TraID; 
0

是的,我找到了解決方案。 (實際上,當我問他的時候,我的項目經理告訴我要執行的查詢。)

select A1.traID,B1.CatName,C1.GroupType,A1.TraDate,A1.TraAmt,    
    (select total(A.TraAmt)    
     from transactionTbl A,TransactionCategory B,CategoryGroup C 
     where A.CategoryID=B.CategoryID and B.CatGroupID=C.CatGroupID 
     AND A.traID <= A1.traID AND C.GroupType = 'Income' and A.userID=1 and A.profileID=1 
     order by A.TraDate,A.TraID) 
     - 
     (select total(A.TraAmt) 
     from transactionTbl A,TransactionCategory B,CategoryGroup C 
     where A.CategoryID=B.CategoryID and B.CatGroupID=C.CatGroupID 
     AND A.traID <= A1.traID AND C.GroupType = 'Expense' and A.userID=1 and A.profileID=1 
     order by A.TraDate,A.TraID) 
    as BalanceAmt 
    from transactionTbl A1,TransactionCategory B1,CategoryGroup C1 
where A1.CategoryID=B1.CategoryID and B1.CatGroupID=C1.CatGroupID 
and A1.userID=1 and A1.profileID=1 
order by A1.TraDate,A1.TraID