Book Royalties
A publisher pays an author a percentage of the revenue of each book, by cumulative brackets: the first 30,000 of revenue since publication at 5%, from 30,001 to 175,000 at 15%, and so on.
books(id, title, author)
brackets(book, cum_from, cum_to, percent)
sales(book, sold_on, country, quantity, amount)
Write a function royalties(p_book, p_year, p_previous) that returns the royalties for a year, where p_previous is the revenue of all the previous years: the year's revenue fills the brackets starting where the previous years left off. Read only the brackets the calculation needs.
Run schema.sql (tables and example data), then solution.sql.
Example
Brackets for AS45: 0–30,000 at 5%, 30,001–175,000 at 15%, 175,001–300,000 at 20%, 300,001–500,000 at 12%. Sales in 2012 total 50,000.
royalties('AS45', 2012, 130000): revenue moves from 130,000 to 180,000, so 45,000 falls in the second bracket and 5,000 in the third: 45,000 × 15% + 5,000 × 20% = 7,750.
45000 at 15%
5000 at 20%
2012, after 130,000: 7750
30000 at 5%
20000 at 15%
2012, first year: 4500
Approach
Compute the year's revenue, then treat the year as the interval (previous, previous + year] of cumulative revenue. A bracket contributes the length of its overlap with that interval:
amount = LEAST(cum_to, previous + year) - GREATEST(cum_from - 1, previous)
A cursor with parameters reads only the brackets that overlap (cum_to > lo AND cum_from <= hi), which is the "only the brackets needed" requirement: the first bracket, already exhausted, and the last, not reached, are never fetched.
The brackets are written with inclusive bounds (30,001 after 30,000), so cum_from - 1 is the real lower edge; for the first bracket GREATEST(..., 0) keeps it at 0.
Lesson: PL/SQL cursors.
Solutions
solution.sql
SET SERVEROUTPUT ON FORMAT WRAPPED
SET FEEDBACK OFF
SET LINESIZE 120 PAGESIZE 100
CREATE OR REPLACE FUNCTION royalties(p_book books.id%TYPE, p_year NUMBER, p_previous NUMBER)
RETURN NUMBER IS
-- only the brackets that the range (p_lo, p_hi] reaches
CURSOR c_brackets(p_lo NUMBER, p_hi NUMBER) IS
SELECT cum_from, cum_to, percent
FROM brackets
WHERE book = p_book AND cum_to > p_lo AND cum_from <= p_hi
ORDER BY cum_from;
v_year NUMBER;
v_total NUMBER := 0;
v_lower NUMBER;
v_amount NUMBER;
BEGIN
SELECT NVL(sum(amount), 0) INTO v_year
FROM sales
WHERE book = p_book AND EXTRACT(YEAR FROM sold_on) = p_year;
FOR b IN c_brackets(p_previous, p_previous + v_year) LOOP
v_lower := GREATEST(b.cum_from - 1, 0); -- brackets are written 0-30000, 30001-175000
v_amount := LEAST(b.cum_to, p_previous + v_year) - GREATEST(v_lower, p_previous);
DBMS_OUTPUT.PUT_LINE(' ' || v_amount || ' at ' || b.percent || '%');
v_total := v_total + v_amount * b.percent / 100;
END LOOP;
RETURN v_total;
END;
/
BEGIN
DBMS_OUTPUT.PUT_LINE('2012, after 130,000: ' || royalties('AS45', 2012, 130000));
DBMS_OUTPUT.PUT_LINE('2012, first year: ' || royalties('AS45', 2012, 0));
END;
/
schema.sql
CREATE TABLE books (id VARCHAR2(10) PRIMARY KEY, title VARCHAR2(80), author VARCHAR2(40));
CREATE TABLE brackets (
book VARCHAR2(10) REFERENCES books,
cum_from NUMBER NOT NULL,
cum_to NUMBER NOT NULL,
percent NUMBER NOT NULL,
PRIMARY KEY (book, cum_from)
);
CREATE TABLE sales (
book VARCHAR2(10) REFERENCES books,
sold_on DATE NOT NULL,
country VARCHAR2(20),
quantity NUMBER,
amount NUMBER NOT NULL
);
INSERT INTO books VALUES ('AS45', 'Databases in Practice', 'Ada');
INSERT INTO brackets VALUES ('AS45', 0, 30000, 5);
INSERT INTO brackets VALUES ('AS45', 30001, 175000, 15);
INSERT INTO brackets VALUES ('AS45', 175001, 300000, 20);
INSERT INTO brackets VALUES ('AS45', 300001, 500000, 12);
-- 2012: 50,000 in total
INSERT INTO sales VALUES ('AS45', DATE '2012-02-10', 'IT', 1000, 20000);
INSERT INTO sales VALUES ('AS45', DATE '2012-07-01', 'FR', 900, 18000);
INSERT INTO sales VALUES ('AS45', DATE '2012-11-30', 'DE', 600, 12000);
INSERT INTO sales VALUES ('AS45', DATE '2013-01-02', 'IT', 100, 2000);
COMMIT;