Solved

Getting last few rows depending on a criteria

Posted on 2011-09-06
5
344 Views
Last Modified: 2012-05-12
Here is my sample table.

I am wanting to generate the fee slips for each student. SAMPLE_FEE is actually a view from a table.

What I want from the data is

       SID        LID LDATE     DESCR                             Amount
---------- ---------- --------- ------------------------- ----------
         456  18722 06-SEP-11 2 Month(s) TUITION FEE          1600
         458  18707 05-SEP-11 Arrears                                1200

         464  18418 20-AUG-11 Arrears                                 600
         464  18683 01-SEP-11 1 Month(s) TUITION FEE         1000  

         466  18696 01-SEP-11 1 Month(s) TUITION FEE           600
   
OK
I do not want
sid = 417 because it has no due amount (gtotal = 0)

But I want the following for the reasons given as well.

sid = 456 has no arrears but has not paid last two months fee (gtotal = 1600)
sid = 458 has paid some amount so arrears are there (gtotal = 1200)
sid = 464 should print two lines because it has arrears + most recent fee as well
sid = 466 has no arrears but has not paid most recent fee so far.

In simple words, report should print any arrears in one line and all the TUITION FEE after last Payment received in multiples of TUITION FEE. Once a payment is made, if it is full then do not print that sid, if it is partially paid then it should print as arrears.

Thanks for your time.

(The sample script is attached as well)


FEE> SELECT * FROM SAMPLE_FEE;

       SID        LID LDATE     DESCR                             DB         CR     GTOTAL
---------- ---------- --------- ------------------------- ---------- ---------- ----------
       417      16624 05-APR-11 REGISTERATION FEE                           500        500
                16625 05-APR-11 TUITION FEE                                 600       1100
                16633 05-APR-11 EXAM FEE 2011                               600       1700
                16641 05-APR-11 PAYMENT RECEIVED                1700                     0
                17127 01-MAY-11 TUITION FEE                                 600        600
                17375 01-JUN-11 TUITION FEE                                 600       1200
                17623 01-JUL-11 TUITION FEE                                 600       1800
                17871 01-AUG-11 TUITION FEE                                 600       2400
                17952 05-MAY-11 PAYMENT RECEIVED                2400                     0
                18639 01-SEP-11 TUITION FEE                                 600        600
                18700 05-SEP-11 PAYMENT RECEIVED                 600                     0

       456      16908 26-APR-11 REGISTERATION FEE                             0          0
                16909 26-APR-11 TUITION FEE                                   0          0
                16910 26-APR-11 EXAM FEE                                    600        600
                16912 26-APR-11 PAYMENT RECEIVED                 800                  -200
                16913 27-APR-11 PAYMENT RECEIVED                 600                  -800
                17166 01-MAY-11 TUITION FEE                                 800          0
                17414 01-JUN-11 TUITION FEE                                 800        800
                17662 01-JUL-11 TUITION FEE                                 800       1600
                17910 01-AUG-11 TUITION FEE                                 800       2400
                18201 04-JUN-11 PAYMENT RECEIVED                2400                     0
                18675 01-SEP-11 TUITION FEE                                 800        800
                18722 06-SEP-11 TUITION FEE                                 800       1600

       458      16917 29-APR-11 REGISTERATION FEE                          1500       1500
                16918 29-APR-11 TUITION FEE                                   0       1500
                16919 29-APR-11 EXAM FEE                                    600       2100
                16920 29-APR-11 PAYMENT RECEIVED                2700                  -600
                17168 01-MAY-11 TUITION FEE                                1200        600
                17416 01-JUN-11 TUITION FEE                                1200       1800
                17664 01-JUL-11 TUITION FEE                                1200       3000
                17912 01-AUG-11 TUITION FEE                                1200       4200
                18003 10-MAY-11 PAYMENT RECEIVED                3600                   600
                18677 01-SEP-11 TUITION FEE                                1200       1800
                18707 05-SEP-11 PAYMENT RECEIVED                 600                  1200

       464      18237 09-JUN-11 REGISTERATION FEE                          3000       3000
                18238 09-JUN-11 TUITION FEE                                1000       4000
                18239 09-JUN-11 EXAM FEE                                    600       4600
                18241 09-JUN-11 PAYMENT RECEIVED                4000                   600
                18377 01-JUL-11 JUNE JULY FEE                              2000       2600
                18418 20-AUG-11 PAYMENT RECEIVED                2000                   600
                18683 01-SEP-11 TUITION FEE                                1000       1600

       466      18352 13-AUG-11 REGISTERATION FEE                             0          0
                18353 13-AUG-11 TUITION FEE                                   0          0
                18354 13-AUG-11 EXAM FEE                                    200        200
                18357 13-AUG-11 PAYMENT RECEIVED                 200                     0
                18696 01-SEP-11 TUITION FEE                                 600        600


46 rows selected.

Open in new window

SAMPLE-FEE.txt
0
Comment
Question by:leoahmad
  • 2
  • 2
5 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 36490354
how about this?

SELECT   sid,
         lid,
         ldate,
         CASE
             WHEN lid = last_payment_lid THEN 'Arrears'
             ELSE TO_CHAR(cnt) || ' Month(s) TUITION FEE'
         END
             descr,
         CASE WHEN lid = last_payment_lid THEN gtotal ELSE crsum END amount
    FROM (SELECT sid,
                 lid,
                 ldate,
                 descr,
                 db,
                 gtotal,
                 last_payment_lid,
                 rn,
                 SUM(CASE WHEN lid > last_payment_lid THEN cr END) OVER (PARTITION BY sid) crsum,
                 COUNT(CASE WHEN lid > last_payment_lid THEN 1 END) OVER (PARTITION BY sid) cnt
            FROM (SELECT sid,
                         lid,
                         ldate,
                         descr,
                         db,
                         cr,
                         gtotal,
                         NVL(MAX(CASE WHEN db > 0 THEN lid END) OVER (PARTITION BY sid), 0)
                             last_payment_lid,
                         ROW_NUMBER() OVER (PARTITION BY sid ORDER BY lid DESC) rn
                    FROM sample_fee f))
           WHERE lid >= last_payment_lid AND gtotal != 0 AND ((rn = 1) OR (lid = last_payment_lid))
ORDER BY sid, lid
0
 
LVL 8

Expert Comment

by:Ghunaima
ID: 36493609
Try this code

 sql1.sql
0
 
LVL 8

Expert Comment

by:Ghunaima
ID: 36493635
Or more precisely sql1.sql
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36495059
36493635 requires 6 accesses to the sample_fee table, that will scale poorly as data volume increases.


http:#36490354 only needs to scan the table once.



0
 
LVL 14

Author Closing Comment

by:leoahmad
ID: 36497518
Millions of thanks
(words can't express my true feelings, just brilliant)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now