Skip to content
Advertisement

Incrementing / Decrementing data based on Running Distinct Count for Book lending scenario using Oracle SQL

I have a scenario which needs to be handled in Oracle SQL – that is a requirement.

This is a book lending scenario which I am trying to create.

  • Each student is entitled only so many number of books which is based on a credit system. In this sample Student ID ‘100’ is entitled for 4 books.
  • Need to generate number of books that the student has checked in till date.
  • The data under column header “#Active Books under this student” is what I am trying to achieve through SQL
  • Some cases the records may be invalid due to invalid bar code scan data – in which cases we need to maintain the same count that the student is entitled to. This is available in the column header “Recurring Update” and “Update Status”

Could any one please help me building an Oracle SQL for achieving the data under column header “#Active Books under this student” ?

Thanks Lakshminarasu Chenduri

Advertisement

Answer

You can do this using a running SUM. There is one piece of information missing. You are assuming that the data is stored in the order you provided, but that is not captured in any column. So for my example I added a column transaction_id. Now I can guarantee the same order in the select. If the date column would contain a time portion (which it probably does, but that is not included in the sample data) then the ORDER BY dt) would be enough.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement