sqlplus creating a report

时间:2019-02-24 03:01:45

标签: sql sqlplus

I'm using sqlplus (oracle) I'm trying to create a report of all customers and their transactions as shown in the image I have attached as expected output. expected output And there are a few thing that I need to consider in order to get an output.

  1. Note that the “Acct #” is a combination of the customer number along with the account type.
  2. Notice that the transaction amount is negative if the transaction type is a “D” (Debit), consider I'm trying to use CASE or DECODE to change the sign. I have attached ERD as well. ERD

This is what I have so far, and it doesn't work (as I expected).

set echo ON
SET LINESIZE 150
SET PAGESIZE 66
CLEAR COLUMNS
CLEAR BREAKS
TTITLE CENTER 'Transaction Report ' SKIP -
       CENTER 'Page: 1' SKIP 2
BTITLE CENTER 'Internal Use Only'

COMPUTE SUM LABEL 'Total Cost1: ' of ovalue on customer#

TTITLE CENTER 'Transaction Report ' SKIP -
    CENTER 'Page: 1' SKIP 2
BTITLE CENTER 'Internal Use Only'
COLUMN name FORMAT A24 HEADING 'Name'
COLUMN ID FORMAT 99 HEADING 'Cust#'
COLUMN title FORMAT A25 HEADING 'Date'
COLUMN cost FORMAT $99999.00 HEADING 'Amount'
BREAK ON name SKIP 1 ON Cust# on report
COMPUTE SUM LABEL 'Ttl Transaction' of transaction number
COMPUTE SUM LABEL 'Grand Total Cost' of amount on report
SELECT first_name|| surname  "Name", customer_number "Cust#", transaction_date "Date", transaction_amount "Amount"
SUM(DECODE(TRANSACTION_TYPE,'D',-1,'C',1,0)*TRANSACTION_AMOUNT)
from wgb_customer wc, wgb_account wa, wgb_transaction wt
where wc.customer_number=wa.customer_number and wa.account_type=wt.account_type
group by (first_name, surname, wc.customer_number,transaction_date, transaction_amount)
order by 1, 3;

I'm so lost on this one. Please help me!

0 个答案:

没有答案