0

我有两个名为memberandexpensebridge表和一个用于连接目的的表,我希望它expense应该被平等地分成那些是合作伙伴的成员expense

UPDATE

//Create Member table
CREATE TABLE member(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);

// Create Expense table
CREATE TABLE expense (_id INTEGER PRIMARY KEY AUTOINCREMENT, amount INTEGER, expenseItemName TEXT);

// Create Bridge Table
CREATE TABLE bridge( bridgeId, _id INTEGER FOREGIN KEY REFERENCES member( _id )  ON DELETE CASCADE, exp_id INTEGER FOREGIN KEY REFERENCES expense(_id)  ON DELETE CASCADE);

,我得到count of present members这样的MEMBER COUNT QUERY

SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id

members这给了我在场的正确计数expense,但是当我想像这样划分expense为现在的成员 时DIVIDE EXPENSE.AMOUNT QUERY

SELECT expense._id, itemName, bridge._id, expense.amount/(SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id), (SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id) AS total_member, bridge.bridge_id FROM member, expense, bridge WHERE  (bridge._id = member._id  AND member._id = 2) AND expense._id = bridge.exp_id

使用这个查询我得到了第一个正确的结果expense.amount,但其他记录不正确,例如如果MEMBER COUNT QUERY我们得到类似的结果3,5,那么在DIVIDE EXPENSE.AMOUNT QUERY这种情况下expense.amount只用first数字除3。在这种情况下,如何修改DIVIDE EXPENSE.AMOUNT QUERY以除expense.amount所有数字。3,5提前致谢。

4

2 回答 2

1

尝试:

SELECT expense._id, itemName, bridge._id, expense.amount/
    (SELECT count(*) 
    FROM expense as E2, bridge as B2
    WHERE E2._id = B2.exp_id
        AND B2._id = M._id), 
    (SELECT count(*) 
    FROM expense as E2, bridge as B2
    WHERE E2._id = B2.exp_id
        AND B2._id = M._id) AS total_member, 
    bridge.bridge_id 
FROM member as M, expense as E, bridge as B
WHERE  (B._id = M._id  AND M._id = 2) 
    AND E._id = B.exp_id 

更新 1:

想想看,我不确定 sqlite 是否支持这种子查询。试试这个:

SELECT E._id, itemName, B._id, E.amount/table_count.count,
    table_count.count AS total_member, 
    B.bridge_id 
FROM member as M, expense as E, bridge as B,
    (SELECT B2._id as b2_id, count(*) as count
        FROM expense as E2, bridge as B2
        WHERE E2._id = B2.exp_id
        GROUP BY B2._id) as table_count
WHERE  (B._id = M._id  AND M._id = 2) 
    AND E._id = B.exp_id 
    AND table_count.b2_id = m._id

更新 2:

再试一次,这次我创建了一个测试模式,所以也许你可以尝试一下我在下面给出的解决方案。希望这次能成功:

SELECT E._id, expenseItemName, B._id, E.amount, 
    table_count.count AS total_member,
    E.amount/table_count.count
FROM member as M, expense as E, bridge as B,
    (SELECT B2.exp_id as b2_exp_id, count(*) as count
        FROM expense as E2, bridge as B2
        WHERE E2._id = B2.exp_id
        GROUP BY B2.exp_id) as table_count
WHERE  (B._id = M._id  AND M._id = 2) 
    AND E._id = B.exp_id 
    AND table_count.b2_exp_id = E._id

您可以在这里尝试:http ://www.sqlfiddle.com/#!7/aeca2/ 13 但您需要 Chrome 或 SQLite。我对您的架构进行了一些修改,但我希望您能了解情况并对您的情况进行所需的小改动。

于 2013-11-23T11:45:42.370 回答
0

乘以expense.amount1.0 可以解决问题。这是sql

`SELECT expense._id, itemName, bridge._id, expense.amount * 1.0 /(SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id), (SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id) AS total_member, bridge.bridge_id FROM member, expense, bridge WHERE  (bridge._id = member._id  AND member._id = 2) AND expense._id = bridge.exp_id
`

希望这有帮助。

于 2013-11-26T04:40:24.823 回答