# Order the dealer’s offers by the difference in ad_price and
# average selling price.

from nested import product_db, dealer_db
from pythonql.helpers import print_table

res = [ select (d.dealer, d.make, d.model, d.year, d.ad_price, diff)
        for d in dealer_db,
            p in product_db
        let p_make = try p['manufacturer']['company'] except p['manufacturer']

        where p['model'] == d.model and p_make == d.make

        let pr = p['price'],
            p_price = try pr['average_price'] except pr,
            diff = d.ad_price - p_price

        order by diff desc
    ]

print_table(res)

play
# Order the dealer’s offers by the difference in ad_price and
# average selling price.

from nested import product_db, dealer_db
from pythonql.helpers import print_table

res = [ select (d.dealer, d.make, d.model, d.year, d.ad_price, diff)
        for d in dealer_db,
            p in product_db
        let p_make = try p['manufacturer']['company'] except p['manufacturer']

        where p['model'] == d.model and p_make == d.make

        let pr = p['price'],
            p_price = try pr['average_price'] except pr,
            diff = d.ad_price - p_price

        order by diff desc
    ]

print_table(res)

play