[英]SQL statement with conditions on year
I am a newbie to SQL and I am trying to add conditions in my SQL query.我是 SQL 的新手,我正在尝试在我的 SQL 查询中添加条件。
I have 4 product pricing file for each year and another table for products.我每年有 4 个产品定价文件和另一个产品表。
price_table_2020
price_table_2019
price_table_2019
All the tables have the data as follows所有表的数据如下
price_table_2020
: 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
: 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
: 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:预计 output 如下:
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
?如果我必须包含基于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.我强烈建议重组您的表格以将所有价格数据放在一个表格中,并在该表格中添加一个year
列以指示价格适用于哪一年。 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): Output(用于您的示例数据):
product invoice_year price
pen 2019 900
ball 2020 100
ink 2020 80
Demo on db-fiddle db-fiddle 上的演示
If you have to maintain your current table structure, you could emulate the price
table above using a UNION
of all the price
tables:如果您必须维护当前的表结构,您可以使用所有price
表的UNION
来模拟上面的price
表:
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.这个查询的output是一样的。 The downside of this approach is that you have to edit the query every time you add a new price
table.这种方法的缺点是每次添加新price
表时都必须编辑查询。
This is a very bad data model. Data belongs in tables, not in the database structure.这是一个非常糟糕的数据model。数据属于表,不属于数据库结构。 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.我建议你改这个数据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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.