我有这个观点1
SELECT min(l.id) AS id,
to_char(l.date::timestamp with time zone, 'YYYY:IW'::text) AS name,
sum(l.debit - l.credit) AS balance, sum(l.debit) AS debit,
sum(l.credit) AS credit, a.type, cumulative_balance
FROM account_move_line l
LEFT JOIN account_account a ON l.account_id = a.id
WHERE l.state::text <> 'draft'::text
GROUP BY to_char(l.date::timestamp with time zone, 'YYYY:IW'::text), a.type;
和View2
SELECT min(l.id) AS id,
to_char(l.date::timestamp with time zone, 'YYYY:IW'::text) AS name,
sum(l.debit - l.credit) AS balance, sum(l.debit) AS debit,
sum(l.credit) AS credit, a.type, DATE 'yesterday'
FROM account_move_line l
LEFT JOIN account_account a ON l.account_id = a.id
WHERE l.state::text <> 'draft'::text AND l.date = DATE 'yesterday'
GROUP BY to_char(l.date::timestamp with time zone, 'YYYY:IW'::text), a.type;
VIEW3
SELECT min(l.id) AS id,
to_char(l.date::timestamp with time zone, 'YYYY:IW'::text) AS name,
sum(l.debit - l.credit) AS balance, sum(l.debit) AS debit,
sum(l.credit) AS credit, a.type, DATE 'today'
FROM account_move_line l
LEFT JOIN account_account a ON l.account_id = a.id
WHERE l.state::text <> 'draft'::text AND l.date = DATE 'today'
GROUP BY to_char(l.date::timestamp with time zone, 'YYYY:IW'::text), a.type;
我希望将view2中的列balance
和view1中的列cumulative_balance
以及此列cumulative_balance
中的列{{1}}加入view2.balance和view3.balance
答案 0 :(得分:1)
这是最终得到的解决方案,注意view2和view3是表t和y,这是view1
SELECT yesterday.yesterday, today.today, report.id, report.balance,
report.debit, report.credit, cumulative.cumulative, report.name
FROM ( SELECT y.balance AS yesterday
FROM report_account_receivable_yesterday) yesterday,
( SELECT t.balance AS today
FROM report_account_receivable_today) today,
( SELECT sum(y.balance + t.balance) AS cumulative
FROM report_account_receivable_yesterday y,
report_account_receivable_today t) cumulative,
( SELECT min(l.id) AS id,
to_char(l.date::timestamp with time zone, 'YYYY:IW'::text) AS name,
sum(l.debit - l.credit) AS balance, sum(l.debit) AS debit,
sum(l.credit) AS credit, a.type, DATE 'now()' - interval '1 day' AS tarehe
FROM account_move_line l
LEFT JOIN account_account a ON l.account_id = a.id
WHERE l.state::text <> 'draft'::text AND l.date = DATE 'now()' - interval '1 day'
GROUP BY to_char(l.date::timestamp with time zone, 'YYYY:IW'::text), a.type) report
ORDER BY report.id DESC;