Frage

I have a fee table that contains fees in a list of price cutoffs based on item pricing. For example the first fee range is for items that are priced at $0 and up to the next tier have a $1 fee. Items that have a price of $25 up to the next tier have a $2 fee, etc. Here is the fee table:

Fee

Cutoff Fee
------ ---
0      1
25     2
100    3

Here is the item table with the prices:

Item

Id     Price
------ ------
1      32
2      18
3      2
4      100

The result should look like this:

Desired Item Fees Result

Id    Price   Fee     Total Price with fee
----- ------- ------- -----------
1     32      2       34
2     18      1       19
3     2       1       3
4     100     3       103

Creating the result has been challenging. Here is what the cartesian product of the join result looks like between the two tables:

Id  Price   Cutoff  Fee
--- ------- ------- ---
1   32      0       1 -- eliminate because price is above the next cut 25
2   18      0       1
3   2       0       1
4   100     0       1 -- eliminate because price is above the next cut 25
1   32      25      2
2   18      25      2 -- eliminate because price is below this cut
3   2       25      2 -- eliminate because price is below this cut
4   100     25      2 -- eliminate because price is above (equal to) the next cut 100
1   32      100     3 -- eliminate because price is below this cut
2   18      100     3 -- eliminate because price is below this cut
3   2       100     3 -- eliminate because price is below this cut
4   100     100     3

the first where is simple:

where price >= cut

this narrows the list down to:

Id  Price   Cutoff  Fee
--- ------- ------- ---
1   32      0       1 -- eliminate because price is above the next cut 25
2   18      0       1
3   2       0       1
4   100     0       1 -- eliminate because price is above the next cut 25
1   32      25      2
4   100     25      2 -- eliminate because price is above (equal to) the next cut 100
4   100     100     3

Here's the question: how do i filter out records that are in the next pricing tier? This is the sql i have so far.

select price, cutoff, fee from item, fee
where price >= cutoff;

I have tried a subquery:

select price, cutoff, fee, 
    (select min(cutoff), fee from fee where cutoff > price) as nextfee 
from item, fee
where price >= cutoff;

but this gives the error:

Operand should contain 1 Column(s)

War es hilfreich?

Lösung 2

What about this? No subjoins, better for performance and simpler.

SELECT i.id, i.price, MAX(f.fee), i.price + MAX(f.fee) 
FROM item i INNER JOIN fee f
ON i.price > f.cutoff
GROUP BY i.id, i.price

Andere Tipps

One way to approach this problem is to use a correlated subquery to get the first fee in the fee table where the cutoff is greater than or equal to the price:

select id, price, fee, (price + fee) as totalprice
from (select id, price,
             (select fee
              from fee
              where price >= cutoff
              order by cutoff desc
              limit 1
             ) as fee
      from item
     ) i
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top