I am a newbie to SQL and I am trying to add conditions in my SQL query.
I have 4 product pricing file for each year and another table for products.
price_table_2020
price_table_2019
price_table_2019
All the tables have the data as follows
price_table_2020
:
product price product_description
-------------------------------------
ball 100 plastic ball
ink 80 ink for pen
pen 1000 pen
bucket 200 bucket
price_table_2019
:
product price product_description
------------------------------------
ball 90 plastic ball
ink 70 ink for pen
pen 900 pen
bucket 100 bucket
price_table_2018
:
product price product_description
-------------------------------------
ball 80 plastic ball
ink 60 ink for pen
pen 800 pen
bucket 300 bucket
Product table looks like this:
product invoice_year
---------------------
pen 2019
ball 2020
ink 2020
Depending upon the invoice year, I am supposed to get the respective product's price from the respective tables.
Expected output as below:
product invoice_year price
----------------------------
pen 2019 900
ball 2020 100
ink 2020 80
I am able to get the price from one of the table as below
select A.price, B.product, B.product_description
from product A
inner join price_table_2020 B on A.product = B.product
Can someone help how to do if I have to include the logic based on product.invoice_year
? Thanks.
What you are trying to do is difficult, to say the least. I would highly recommend restructuring your tables to put all the price data in one table, adding a year
column to that table to indicate which year the price is applicable to. Then your query becomes simple:
SELECT pr.product,
pr.invoice_year,
pc.price
FROM product pr
JOIN price_table pc ON pc.product = pr.product
AND pc.year = pr.invoice_year
ORDER BY pr.invoice_year, pr.product
Output (for your sample data):
product invoice_year price
pen 2019 900
ball 2020 100
ink 2020 80
If you have to maintain your current table structure, you could emulate the price
table above using a UNION
of all the price
tables:
SELECT pr.product,
pr.invoice_year,
pc.price
FROM product pr
JOIN (
SELECT *, 2020 AS year
FROM price_table_2020
UNION ALL
SELECT *, 2019
FROM price_table_2019
UNION ALL
SELECT *, 2018
FROM price_table_2018
) pc ON pc.product = pr.product
AND pc.year = pr.invoice_year
ORDER BY pr.invoice_year, pr.product
The output of this query is the same. The downside of this approach is that you have to edit the query every time you add a new price
table.
This is a very bad data model. Data belongs in tables, not in the database structure. The year is mere data (as can be seen from your product table), and it should be data in a price table. Instead you have a separate price table per year. This makes writing queries a nuisance, as can be seen below. I recommend you change this data model.
select p.*, coalesce(p2018.price, p2019.price, p2020.price) as invoice_price
from products p
left join price_table_2018 p2018 on p2018.product = p.product and p.invoice_year = 2018
left join price_table_2019 p2019 on p2019.product = p.product and p.invoice_year = 2019
left join price_table_2020 p2020 on p2020.product = p.product and p.invoice_year = 2020
order by p.product;
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.