我需要写一条产生客户报告的报表,该报表显示所有客户,帐户和交易。包括没有交易的帐户。使用JOIN ... ON解决。并且需要-如果transaction_type是借方,请登录我的transaction_amount输出。
我尝试过使用格式,如果不是,那么... etc
这是我到目前为止所拥有的。无法弄清楚-请为我的输出签名。
select c.first_name "First", c.surname "Last",c.date_entered "Acct Date",
t.account_type "Type", t.transaction_number "Trans #",
t.transaction_date "Trs Date", t.transaction_amount "Amount"
from wgb_customer c
join wgb_transaction t on (c.customer_number=t.customer_number)
order by 1,2,3,4;
答案 0 :(得分:0)
首先,您还需要加入帐户表,并在交易表上使用LEFT OUTER JOIN
,因为它也应该为没有交易的帐户创建输出。
此外,在事务类型上使用CASE
语句以添加减号。
像这样的东西(尽管我没有尝试;它甚至可能无法编译):
SELECT cust.first_name AS first_name,
cust.surname AS last_name,
acct.account_type AS acct_type,
acct.date_created AS acct_date,
trns.transaction_number AS trans_nr,
trns.transaction_date AS trans_date,
CASE
WHEN trns.transaction_type IS NULL THEN NULL
WHEN trns.transaction_type = 'D' THEN '-' || TO_CHAR(transaction_amount, 'L99G999D99')
ELSE TO_CHAR(transaction_amount, 'L99G999D99')
END AS amount
FROM wgb_customer cust
JOIN wgb_account acct
ON acct.customer_number = cust.customer_number
LEFT OUTER JOIN wgb_transaction trns
ON trns.customer_number = acct.customer_number
AND trns.account_type = acct.account_type
ORDER BY last_name,
first_name,
acct_type,
acct_date,
trans_nr;