简体   繁体   中英

SQL: lookup values from a table with multiple conditions to return final calculation

I am trying to build a query that calculates final revenue based off a number of conditions that affect the cost of items bought from multiple stores over a period of time. Each store/item/location has difference costs.

Currently I have scheduled, partitioned tables showing sales per store that update how many apples/pears I buy each day. This feeds into a table that looks like Table B below. Currently the costs are manually updated in the query as cost per item, per store. However it hasn't been accounting for differences in cities or delivery fees, so total costs will be inaccurate.

As there are so many cost variations it seemed easier to update a central table with different costs breakdowns. For example Table A contains the list of items and cost per items at different stores, cities and if delivery has cost us money.

Rather than manually update one long query with lots of conditional statements per store, Can I have one query that matches the costs just from Table A whenever it is updated?

Thanks

Table A:

Item Store City Costper item Delivery Delivery fee per item
Apples SnakMarket Townville $1 y $0.5
Pears SnakMarket Townville $3 y $0.5
Apples SnakMarket Cityshire $1.50 n $0.0
Pears SnakMarket Cityshire $3.50 n $0.0
Apples DollarFood Townville $2 y $0.75
Pears DollarFood Townville $4 y $0.75
Apples DollarFood Cityshire $3 n $0.0
Pears DollarFood Cityshire $4 n $0.0

Table B should have the outputs that groups the costs similarly to below.

Table B:

Item Quantity Total Revenue
Apples 350 $xxxx
Pears 601 $xxxx

You haven't displayed your quantities table which would be helpful, but from my understanding, it looks something like this:

Date Item Store City Quantity
2022/09/29 Pear SnakMarket Townville 69420
2022/09/29 Apple SnakMarket Townville 42069
2022/09/29 Pear SnakMarket Cityshire 10000
2022/09/29 Apple SnakMarket Cityshire 45454

... etc

If your table looks something like the above (irrespective of column order), you can perform a LEFT JOIN to add the costings per item

SELECT
A.Date,
A.Item,
A.Store,
A.City,
A.Quantity,
B.Costperitem,
B.Delivery,
B.Deliveryfee
FROM
`Quantity Table` AS A
LEFT JOIN
`Table 1` AS B
ON
A.Item = B.Item
AND
A.Store = B.Store
AND
A.City = B.City

This will combine your data and you will end up with and output like:

Date Item Store City Quantity Costperitem Delivery Deliveryfee
2022/09/29 Pear SnakMarket Townville 69420 3 y 0.5
2022/09/29 Apple SnakMarket Townville 42069 1 y 0.5
2022/09/29 Pear SnakMarket Cityshire 10000 3.5 n 0
2022/09/29 Apple SnakMarket Cityshire 45454 1.5 n 0

... etc

From this, you should be able to work out your groupings to get the total revenue generated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM