简体   繁体   English

SQL JOIN从左表获取数据,即使所有右列都匹配

[英]SQL JOIN get data from left table Only even if all right column match

I am using PostgreSQL 9.5. 我正在使用PostgreSQL 9.5。 I have tow tables 我有拖车桌

EmployeeReportMarch
EmployeeId      Bonus      Day
---------------------------------
1               10000      23

EmployeeReport20152016
EmployeeId      Bonus      Month
---------------------------------
1               10000      02
1               10000      03

EmployeeReport
EmployeeId      Bonus      FiscalYear
---------------------------------
1               100000      20152016
1               90000       20162017

Calendar
Day_Id          Day    Month     FiscalYear
--------------------------------------------
2015-03-21      21     3         20152016
2015-03-22      22     3         20152016
2015-03-23      23     3         20152016
2015-03-24      24     3         20152016
2015-03-25      25     3         20152016

Calendar table is from '2010-01-01' to '2016-12-31' . 日历表从'2010-01-01'到'2016-12-31'。 Fiscal Year is from 1st April to 31st March. 会计年度为4月1日至3月31日。

When I am joining both table to get data from 当我加入两个表以从中获取数据时

SELECT e.EmployeeeId, e.Bonus
FROM Employee e INNER JOIN Calendar c ON e.Day = c.Day
WHERE c.Day_Id BETWEEN '2015-02-01'::Date AND '2015-03-31'::Date;

Output : 输出:

EmployeeId     Bonus     Day_Id
-------------------------------------
1              10000     2015-02-23
1              10000     2015-03-23

SELECT e.EmployeeeId, e.Bonus
FROM Employee e INNER JOIN Calendar c ON e.Month = c.Month
WHERE c.Day_Id BETWEEN '2015-02-01'::Date AND '2015-03-31'::Date;

Output : 输出:

EmployeeId     Bonus
--------------------
1              100000
1              100000

--59 Rows (2 Month rows)

SELECT e.EmployeeeId, e.Bonus
FROM Employee e INNER JOIN Calendar c ON e.FiscalYear = c.FiscalYear
WHERE c.Day_Id BETWEEN '2015-02-01'::Date AND '2015-03-31'::Date;

Output : 输出:

EmployeeId     Bonus
--------------------
1              10000
1              10000

--365 Rows

But there is only one record left in table. 但是表中只剩下一个记录。 I have tried with OUTER JOIN also but the result was same any idea How to do it. 我也尝试过OUTER JOIN,但结果却是相同的,怎么做。

join combines matching rows from both tables. join合并两个表中的匹配行。 Since the day in employee table matches twice with day in calendar table within the range given in between statement, that is why two rows in output. 由于雇员表中的日期与日历表中的日期在两次语句之间给定的范围内匹配两次,因此这就是为什么输出两行。 I think if you use any join, it will give two rows in output. 我认为,如果您使用任何联接,它将在输出中提供两行。

It's problem of architecture, not JOIN statement... 这是架构问题,而不是JOIN语句...

Your table Calendar have Primary Key column Day_Id , but You trying to JOIN it by column Day, which is not unique. 您的表Calendar具有Primary Key列Day_Id ,但是您尝试按Day列进行联接,这不是唯一的。 There are a lot of records with Day = 23 (12 per Year, to be exact). 有很多记录,其中Day = 23(准确地说,每年12个)。 So, You get 1 row per each month in Your table. 因此,您每月在表中获得1行。

To fix this problem, You should JOIN it by column Day_Id (or by 3 columns at once: Year + Month + Day). 要解决此问题,您应该按Day_Id列(或同时按3列:Year + Month + Day)加入它。

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

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