表定义:
Table "public.transactions"
┌────────────────────────────┬──────────────────────────┬───────────┬──────────┬───────────────────────────────────────────────────────┐
│ Column │ Type │ Collation │ Nullable │ Default │
├────────────────────────────┼──────────────────────────┼───────────┼──────────┼───────────────────────────────────────────────────────┤
│ transactions_id │ integer │ │ not null │ nextval('transactions_transactions_id_seq'::regclass) │
│ transactions_datecreated │ timestamp with time zone │ │ not null │ CURRENT_TIMESTAMP │
│ transactions_datemodified │ timestamp with time zone │ │ │ │
│ transactions_usercreated │ text │ │ not null │ CURRENT_USER │
│ transactions_usermodified │ text │ │ not null │ CURRENT_USER │
│ transactions_date │ timestamp with time zone │ │ not null │ CURRENT_TIMESTAMP │
│ transactions_name │ text │ │ not null │ │
│ transactions_description │ text │ │ │ │
│ transactions_amount │ numeric │ │ not null │ │
│ transactions_currencies │ integer │ │ not null │ │
│ transactions_fromaccount │ integer │ │ not null │ │
│ transactions_toaccount │ integer │ │ not null │ │
│ transactions_uuid │ uuid │ │ not null │ gen_random_uuid() │
│ transactions_hyobjects │ integer │ │ │ │
│ transactions_receiver │ integer │ │ │ │
│ transactions_reporter │ integer │ │ │ │
│ transactions_payer │ integer │ │ │ │
│ transactions_receipt │ integer │ │ │ │
│ transactions_locations │ integer │ │ │ │
│ transactions_exchangerates │ integer │ │ │ │
└────────────────────────────┴──────────────────────────┴───────────┴──────────┴───────────────────────────────────────────────────────┘
Indexes:
"transactions_pkey" PRIMARY KEY, btree (transactions_id)
"transactions_transactions_uuid_key" UNIQUE CONSTRAINT, btree (transactions_uuid)
Foreign-key constraints:
"transactions_transactions_currencies_fkey" FOREIGN KEY (transactions_currencies) REFERENCES currencies(currencies_id)
"transactions_transactions_exchangerates_fkey" FOREIGN KEY (transactions_exchangerates) REFERENCES exchangerates(exchangerates_id)
"transactions_transactions_fromaccount_fkey" FOREIGN KEY (transactions_fromaccount) REFERENCES accounts(accounts_id)
"transactions_transactions_hyobjects_fkey" FOREIGN KEY (transactions_hyobjects) REFERENCES hyobjects(hyobjects_id)
"transactions_transactions_locations_fkey" FOREIGN KEY (transactions_locations) REFERENCES locations(locations_id)
"transactions_transactions_payer_fkey" FOREIGN KEY (transactions_payer) REFERENCES people(people_id)
"transactions_transactions_receipt_fkey" FOREIGN KEY (transactions_receipt) REFERENCES hyobjects(hyobjects_id)
"transactions_transactions_receiver_fkey" FOREIGN KEY (transactions_receiver) REFERENCES people(people_id)
"transactions_transactions_reporter_fkey" FOREIGN KEY (transactions_reporter) REFERENCES people(people_id)
"transactions_transactions_toaccount_fkey" FOREIGN KEY (transactions_toaccount) REFERENCES accounts(accounts_id)
Triggers:
account_update_balance AFTER INSERT OR UPDATE ON transactions FOR EACH STATEMENT EXECUTE FUNCTION account_update_balance()
insert_username_transactions BEFORE INSERT OR UPDATE ON transactions FOR EACH ROW EXECUTE FUNCTION insert_username('transactions_usermodified')
transactions_moddatetime BEFORE UPDATE ON transactions FOR EACH ROW EXECUTE FUNCTION moddatetime('transactions_datemodified')
transactions_id integer NOT NULL,
transactions_datecreated timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
transactions_datemodified timestamp with time zone,
transactions_usercreated text DEFAULT CURRENT_USER NOT NULL,
transactions_usermodified text DEFAULT CURRENT_USER NOT NULL,
transactions_date timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
transactions_name text NOT NULL,
transactions_description text,
transactions_amount numeric NOT NULL,
transactions_currencies integer NOT NULL,
transactions_fromaccount integer NOT NULL,
transactions_toaccount integer NOT NULL,
transactions_uuid uuid DEFAULT gen_random_uuid() NOT NULL,
transactions_hyobjects integer,
transactions_receiver integer,
transactions_reporter integer,
transactions_payer integer,
transactions_receipt integer,
transactions_locations integer,
transactions_exchangerates integer
);
在上面的表格定义中,我只有一个条目,其中的金额表示从一个帐户转移到另一个帐户的金额。
我仍然想在一天结束时使用窗口函数来获取余额。
这是我的尝试,但还不起作用的 SQL:
SELECT transactions_id AS "ID",
transactions_date AS "Date",
CASE
WHEN transactions_fromaccount = accounts_id
THEN - transactions_amount
WHEN transactions_toaccount = accounts_id
THEN transactions_amount
END AS "Amount",
account_name(transactions_fromaccount) AS "From account",
account_name(transactions_toaccount) AS "To account",
CASE WHEN transactions_fromaccount = accounts_id
THEN ''
ELSE transactions_amount::numeric::text END AS "Debit",
CASE WHEN transactions_toaccount = accounts_id
THEN ''
ELSE transactions_amount::numeric::text END AS "Credit",
sum("Amount")
OVER (PARTITITON BY transactions_id
ORDER BY transactions_date)
FROM accounts, transactions
WHERE (transactions_fromaccount = accounts_id
OR transactions_toaccount = accounts_id)
AND accounts_id = 65
ORDER BY transactions_date;
使用 PostgreSQL 14.6
我正在尝试生成列"Amount"
然后使用sum("Amount")
,但是,我做错了什么。我收到此错误:
ERROR: column "Amount" does not exist
LINE 17: sum("Amount")
有没有办法解决它得到sum("Amount")
?
当您的 SELECT 中有计算列时,如下所示:
并且您想在同一个 SELECT 中重用它的结果,最通用的解决方案是将查询重写为派生表,以便您可以
"alias"
在外层引用:现在“别名”成为一个有效的引用,允许您按原样返回列并在表达式中重新使用它以生成基于它的其他列(在您的情况下,聚合结果)。
一个类似的选择是将原来的重写为 CTE(公用表表达式):
这两种方法可以被认为是等效的,唯一的区别(也是一个重要的区别)是 CTE 在 PostgreSQL 中是可实现的,这意味着选择一种方式而不是另一种方式可能会对性能产生影响。
还有第三种方法,它不如前两种方法普遍,但适用于您的情况。PostgreSQL 支持
LATERAL
连接。"alias"
您可以在一个单独的FROM
-less中定义SELECT
,然后您可以LATERAL
-join 到您的其他表。更具体地说,它看起来像这样:这些是我所知道的唯一不涉及在数据库中创建新对象的选项。如果创建新对象不是问题,那么您还可以考虑将原始查询定义为视图的可能性:
然后你的主要查询将从视图中选择,使你能够
"Amount"
以任何必要的方式引用已经定义的列:此方法与前两个选项非常相似,不同之处在于您的原始查询会保留在数据库中。其他查询现在也可以从该视图读取,允许您在更大范围内重用您的代码,而不仅仅是单个列定义(当然,这是视图的主要目的之一)。
使用以下 SQL,我设法获得了每个特定帐户的借方和贷方的适当运行余额。感谢贡献者。