用于帐户报表的SQL

时间:2011-08-16 14:57:31

标签: sql postgresql

我想提前感谢您的帮助。

我的问题涉及MySQL 中的两个表(现在切换到 postgresql 。这些表与票务数据库有关。

a) booking. It has four columns       ccode,date,time,amount
b) account  It has three columns      ccode,date,amount

预订表有机票预订,账户表有预付款和付款。

我必须根据ccode(客户代码)准备一份帐户报表。

该声明显示以下列

*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

到目前为止,我已经成功创建了一个如下所示的联接。 (在下面的讨论之后,已经能够使用IF生成TYPE列)

   
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

显示所有记录。可以使用此表生成报告。

但是有没有办法只用SQL显示格式化的报告。 只要记录类型已知并且针对每条记录显示运行余额,我就可以更改列的顺序,甚至可以添加或删除现有列。

样本报告----报告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 

样本报告----报告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

2 个答案:

答案 0 :(得分:1)

由于您的数据未针对此进行规范化,因此您需要支付查询复杂性的价格,如下所示:

SELECT    ccode, date, time, type, amount, 
          (SELECT SUM(amount) AS balance 
          FROM numbered AS n 
          WHERE numbered.rownum <= n.rownum AND numbered.ccode = n.ccode) AS balance 
FROM      (SELECT sorted.*, @rownum := @rownum + 1 AS rownum 
          FROM      (SELECT * 
                    FROM      (SELECT ccode, date, time, 'B' AS type, amount 
                              FROM booking
                              UNION
                              SELECT ccode, date, "0000" AS time, 'A' AS type, -amount 
                              FROM account) AS unsorted
                    ORDER BY ccode, date, time, type) AS sorted) AS numbered

这里的想法是,您首先需要按照上面的“未分类”声明排列您的预订(借记)和帐户(积分)。然后,您需要按照“已排序”语句中的日期和时间对它们进行排序。接下来,在结果中添加行号,如“编号”语句中所示。最后,选择所有数据以及行数小于或等于与您的ccode匹配的当前行号的金额总和。

将来,请考虑使用某种交易表,将所有帐户余额更改保存在一个表中。

答案 1 :(得分:0)

我找到了答案。

使用累积sql语句查找运行余额