首先,让我解释一下表结构。它如下。
示例数据
CatGroupID | GroupName | GroupType
1 |实用工具|费用
2 |自动|费用
3 |杂项。 |费用
4 |收入|收入
5 | HouseHold |费用
子表 - TransactionCategory
示例数据
CategoryID | 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(AutoInc)|用户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
我尝试了很多查询 - 其中一个如下,
从transactionTbl A,TransactionCategory B,CategoryGroup C中选择A.TraDate,A.TraAmt,C.GroupType,B.CatName 其中A.TraDate ='2010-01-11'和A.CAtegoryID = B.CategoryID和B.CatGroupID = C.CatGroupID
我是一名iPhone开发人员&使用SQLite。关于数据库管理和我的想法很少。查询&连接。因为这个我不得不在这里问。
我需要的输出如下。
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
它提供如下输出。
TraID CatName GroupType TraDate 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 Auto Expense 2010-01-03 450
8燃气费2010-01-05 750
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 :(得分: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;
答案 1 :(得分: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