简体   繁体   English

在sql -ms access中返回带有额外列的查询

[英]Query returned with an extra column in sql -ms access

So I am wondering. 所以我想知道。 I fell into an interesting suggestion from another developer. 我从另一个开发人员那里得到了一个有趣的建议。 So i basically have two tables I join in a query and I want the resulting table from the query to have an extra column that comes from the table on from the joint. 因此,我基本上有两个要加入查询的表,并且我希望查询的结果表中有一个来自联合表的额外列。

Example: 例:

#table A: contains rating of players, changes randomly at any date depending
#on drop of form from the players  

PID| Rating | DateChange  |
1  |    2   | 10-May-2014 |
1  |    4   | 20-May-2015 |
1  |   20   | 1-June-2015 |
2  |    4   | 1-April-2014|
3  |    4   | 5-April-2014|
2  |    3   | 3-May-2015  |

#Table B: contains match sheets. Every player has a different match sheet
#and plays different dates.

MsID | PID  | MatchDate    | Win |
 1   |  2   | 10-May-2014  |  No |
 2   |  1   | 15-May-2015  | Yes |
 3   |  3   | 10-Apr-2014  |  No |
 4   |  1   | 21-Apr-2015  | Yes |
 5   |  1   | 3-June-2015  | Yes |
 6   |  2   | 5-May-2015   |  No |

#I am trying to achieve this by running the ms-access query: i want to get
#every players rating at the time the match was played not his current
#rating. 

MsID | PID  | MatchDate    | Rating |
 1   |  2   | 10-May-2014  |    4   |
 2   |  1   | 15-May-2015  |    2   |
 3   |  3   | 10-Apr-2014  |    4   |
 4   |  1   | 21-Apr-2015  |    4   |
 5   |  1   | 3-June-2015  |    20  |
 6   |  2   | 5-May-2015   |    3   |

This is what I have tried below: 这是我在下面尝试过的:

Select MsID, PID, MatchDate, A-table.rating as Rating from B-table
       left Join A-table 
          on B-table.PID = A-table.PID
             where B-table.MatchDate > A-table.Datechange;

any help is appreciated. 任何帮助表示赞赏。 The solution can be in Vba as long as it returns something like a view/table I can manipulate using other queries or report. 该解决方案可以在Vba ,只要它返回类似视图/表的内容即可,我可以使用其他查询或报表来操纵该视图/表。

Think of this in terms of sets of data... you need a set that lists the MAX dateChange for each player's and match date. 从数据集的角度考虑这一点……您需要一个列出每个球员的比赛日期的MAX dateChange的集合。

Soo... ...

SELECT MAX(A.DateChange) MDC, A.PID, B.Matchdate
FROM B-table B
INNER Join A-table A
 on B.PID = A.PID
and A.DateChange <= B.MatchDate
GROUP BY A.PID, B.Matchdate

Now we take this and join it back to what you've done to limit the results in table A and B to ONLY those with that date player and matchDate (my inline table C) 现在,我们将其重新加入到您所做的工作中,以将表A和B中的结果限制为仅具有该日期播放器和matchDate的结果(我的内联表C)

SELECT B.MsID, B.PID, B.MatchDate, A.rating as Rating 
FROM [B-table] B
INNER JOIN [A-table] A
  on B.PID = A.PID
INNER JOIN (     
  SELECT MAX(Y.DateChange) MDC, Y.PID, Z.Matchdate
  FROM [B-table] Z
  INNER Join [A-table] Y
   on Z.PID = Y.PID
  and Y.DateChange <= Z.MatchDate
  GROUP BY Y.PID, Z.Matchdate) C
  on C.mdc = A.DateChange
 and A.PID = C.PId
 and B.MatchDate = C.Matchdate

I didn't create a sample for this using your data so it's untested but I believe the logic is sound... 我没有使用您的数据为此创建一个示例,因此未经测试,但我相信逻辑是正确的...

Now Tested! 现在测试! SQL Fiddle using SQL server though... SQL Fiddle使用SQL Server ...

My results don't match yours exactly. 我的结果与您的结果不完全相同。 I think you're expected results are wrong though for MSID 4 given rules defined. 我认为,尽管对于给定规则的MSID 4,您期望的结果是错误的。

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

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