简体   繁体   English

在 SQL 服务器中使用内连接时如何排除不相关的数据?

[英]How to exclude unrelated data when using inner join in SQL Server?

I have 2 tables in hand.我手头有两张桌子。 Table_A contains the Sales Date, fruit type, quantity sold and total price. Table_A 包含销售日期、水果类型、销售数量和总价。 Table_B contains the Unit Price of different type of fruit at different time period. Table_B 包含不同类型水果在不同时间段的单价。

Table_A:表_A:

Sales_Date    Fruit     Quantity  Total_Price
20200515      Apple        2          4 
20200601      Apple        4          10
20200601      Banana       4          7.2
20200606      Orange       6          7.8

Table B:表 B:

Fruit      Valid_Price_From     Valid_Price_To     Unit_Price 
Apple         20200301            20200531          2.0
Banana        20200301            20200531          1.5
Orange        20200301            20200531          1.0
Apple         20200601            20200831          2.5
Banana        20200601            20200831          1.8
Orange        20200601            20200831          1.3

I'm required to form a table C combining sales related info with Unit Price which is valid within that period with this format.我需要形成一个表格 C 将销售相关信息与在该期间内有效的单价与此格式相结合。

Table C:表 C:

Sales_Date      Fruit      Quantity     Unit_Price    Total_Price
20200515        Apple         2             2             4 
20200601        Apple         4            2.5            10
20200601        Banana        4            1.8            7.2
20200606        Orange        6            1.3            7.8

The first query that I used was我使用的第一个查询是

select A.Sales_Date, A.Fruit, A.Quantity, B.Unit_Price, A.Total Price
from table_A A 
inner join
table_B B on A.Fruit=B.Fruit

But I got an error when joining these 2 tables.但是在加入这两个表时出现错误。

I guess it is caused by there are 2 types of Unit Price (valid in different periods) for each type of fruit.我猜这是因为每种水果有两种Unit Price (在不同时期有效)。 Then, I tried to compare the Sales_Date with the Valid_Price_From and Valid_Price_To然后,我尝试将Sales_DateValid_Price_FromValid_Price_To进行比较

The second query that I used was:我使用的第二个查询是:

select A.Sales_Date, A.Fruit, A.Quantity, B.Unit_Price, A.Total Price
from table_A A 
inner join
table_B B on A.Fruit=B.Fruit
where A.Sales_Date > B.Valid_Price_From and A.Sales_Date < B.Valid_Price_To

But it didn't work properly as well.但它也不能正常工作。

May I know how should I construct my inner join query to get the desired output as shown in Table C?我可以知道我应该如何构造我的内部连接查询以获得所需的 output,如表 C 所示?

Your date comparisons are not correct in your query:您的查询中的日期比较不正确:

select A.Sales_Date, A.Fruit, A.Quantity, B.Unit_Price, A.Total_Price
from table_A A inner join
     table_B B
     on A.Fruit = B.Fruit and
        A.Sales_Date >= B.Valid_Price_From and 
        A.Sales_Date <= B.Valid_Price_To;

In other words, you are not including the beginning and end dates, but your data model suggests that you do want to include them.换句话说,您没有包括开始日期和结束日期,但您的数据 model 表明您确实想要包括它们。

If you are concerned about filtering out rows with no matches -- well, you might need to fix your data.如果您担心过滤掉不匹配的行——那么,您可能需要修复您的数据。 But you can keep then using left join instead of inner join .但是您可以继续使用left join而不是inner join

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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