我建议 3 张桌子。
一个实体表,其中包含帐户所有者的详细信息(并使用对拥有实体的引用来满足子公司的需求),并具有用于类型(私人或公司)的列。
具有帐户名称和所有者的帐户表。
用于记录交易的Account Transaction表,基本上是两列,一列引用账户(因此实体)和交易金额。
以上内容非常简单,您可能希望添加更多列。
示例
以下是一个简短的演示,最终以帐户名称、直接所有者名称和显示的帐户类型显示帐户余额。
DROP TABLE IF EXISTS entity;
DROP TABLE IF EXISTS account;
DROP TABLE IF EXISTS account_transaction;
CREATE TABLE IF NOT EXISTS entity (id INTEGER PRIMARY KEY, entity_name TEXT, entity_type INTEGER, entity_owning_entity);
CREATE TABLE IF NOT EXISTS account (id INTEGER PRIMARY KEY, account_name TEXT, entity_reference INTEGER);
CREATE TABLE IF NOT EXISTS account_transaction (id INTEGER PRIMARY KEY, account_reference INTEGER, amount REAL);
INSERT INTO entity (entity_name, entity_type, entity_owning_entity)
VALUES
("Company 1",1,null), -- will be id 1
("Company 2",1, null), -- will be id 2
("Sub Company A",1,1), -- Owned by Company 1 will be id 3
("Sub Company B",1,1), -- Owned by Company 1 will be id 4
("Sub Company X",1,2), -- Owned by Company 2 will be id 5
("Sub Company Y",1,2), -- Owned by Company 2 will be id 6
("Fred",0,null), -- Not owned personal type (0) will be id 7
("Mary",0,null) -- Not owned, personal type, will be id 8
;
INSERT INTO account (account_name, entity_reference)
VALUES
("Fred's Cheque",7), -- id 1
("Sub A Cheque",3), -- id 2
("Fred's Savings",7), -- id 3
("Mary's Cheque",8), -- id 4
("Sub X Chequ",5) -- id 5
;
INSERT INTO account_transaction (account_reference, amount)
VALUES
(3,123.45),(3,57.55),(3,200), -- 381 for Fred's Cheque (i.e. references id 3 in the account table)
(2,100),(2,100),(2,100),(2,100), -- 400 for Sub A Cheque (i.e. references id 2)
(1,10),
(4,15),(4,15),(4,30),
(5,13),(5,14),(5,15),(5,16),(5,17)
;
WITH cte1(id,name) AS
-- Use Common Table Expression (temp table) to resolve owner of an entity
(SELECT id, (SELECT entity_name FROM entity WHERE me.entity_owning_entity = id) FROM entity AS me)
-- Main SELECT Query
SELECT
-- Handle case when no entity owner or when entity owner
CASE
WHEN (SELECT name FROM cte1 where cte1.id = entity.id) IS NULL
THEN entity_name
ELSE (SELECT name FROM cte1 where cte1.id = entity.id)||'-'||entity_name
END AS AccountOwner,
account_name AS Account, -- Show the axccount_name
sum(amount) AS Balance, -- Show the balance
-- Use and appropriate value for the type (private or company account)
CASE
WHEN entity_type = 1 THEN 'Company'
WHEN entity_type = 0 THEN 'Private'
END AS Type
FROM account_transaction -- main table
JOIN account ON account.id = account_reference -- relationship to the account table
JOIN entity ON entity.id = entity_reference -- relationship to the entity table
GROUP BY account.id -- group according to accounts (i.e. 1 row is made from all rows in a group for aggregates such a sum)
ORDER BY AccountOwner -- Order result according to the Account Owner
;
结果
SELECT 查询将返回:-
- 请注意,您通常不会假设 id 是特定值,为了简洁起见,使用了它们的假设。