I would like to thank in advance for any help.
My problem relates to two tables in MySQL (Now switching to postgresql). The tables are related to a ticketing database.
a) booking. It has four columns ccode,date,time,amount b) account It has three columns ccode,date,amount
The booking table has ticket bookings and account table has advances and payments received.
I have to prepare a statement of account based on ccode (customer code).
The statement shows columns as below
*Ccode Type Date time amount balance* - the report in sorted on ccode and then on date (account table row appears first) - Type column displays B or A depending on record type - Time column is present only in booking table - Report has a running balance for each row - At the end for a customercode, the totals of amount and balance is displayed
I have had success, so far in creating a join as below. (and after dicussion below, have been able to generate TYPE column using IF)
SELECT booking.cname, booking.bdate, booking.btime, booking.rate, booking.ID, IF(booking.btime IS NOT NULL, "B", "A") AS type, account.cname, account.date, account.amount, account.ID FROM booking LEFT JOIN account ON booking.bdate = account.date AND booking.cname=account.cname AND booking.rate = account.amount UNION SELECT booking.cname, booking.bdate, booking.btime, booking.rate, booking.ID, IF(booking.btime IS NOT NULL, "B", "A") AS type, account.cname, account.date, account.amount, account.ID FROM booking RIGHT JOIN account ON booking.bdate = account.date AND booking.cname=account.cname AND booking.rate = account.amount
It displays all the records. A report can be generated using this table.
But is there a way to display the formatted report just by SQL. I can change the order of columns and even add or remove existing ones as long as Record type is known and a running balance is displayed against each record.
A SAMPLE REPORT ---- REPORT A
CODE DATE TYPE AMOUNT BALANCE TIME A1 02/19/2011 A 50 50 A1 02/20/2011 B 35 15 1230 A1 02/21/2011 A 40 55 A1 02/21/2011 B 20 35 1830 optional > TOTAL Account = 90 Booking = 55 Balance = 35
A SAMPLE REPORT ---- REPORT B
CODE AMOUNT BOOKED AMOUNT PAID BALANCE A1 50 50 0 A1 35 15 20 A1 40 55 -15 A1 20 35 -15 this is a weekly statement version of REPORT A. the reason is i can add where and between to get only records in a given week. and since it is a weekly report, running balance is just omitted. It is report grouped for all entries with customercode A1 present in booking and account tables. thanx