【译文】在一个日期范围内匹配记录-周芳
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-表


【译文】在一个日期范围内匹配记录

发表时间:2012/4/21 15:08:52 评论(7) 浏览(20503)  评论 | 加入收藏 | 复制
   
摘 要:将一个表中的记录和另一个有日期范围的表相匹配
正 文:

原作者:Juan Soto 翻译:周芳    

     一天,我叫Ben帮我解决一个棘手的问题,我需要将一个表中的记录和另一个有日期范围的表相匹配。这是我要更新的表,表名为tblWidgets.




 WidgetID DateProduced    WidgetWeekID
-------  ------------    ------------
1           1/2/11
2           1/16/11
3           1/31/11

WidgetWeekID是另一个表名为tblWidgetWeeks中的字段:

WidgetWeekID     StartDate    EndDate
------------     --------     -------
 1                12/29/10    1/5/11
 2                1/6/11      1/12/11
 3                1/13/11     1/20/11
 4                1/21/11     1/28/11
 5                1/29/11      2/5/11

    我需要一个查询来找到StartDate和EndDate之间的DateProduced ,然后用正确的编号更新WidgetWeekID。这是他想出的解决方案:

Update tblWidgets AS w
INNER JOIN tblWidgetWeeks AS wwk
  ON w.[DateProduced] >= wwk.StartDate AND w.[DateProduced ] <= wwk.EndDate
 SET w.WidgetWeekID = [wwk].[WidgetWeekID];

     Ben用连接日期范围来解决的方法出色在哪呢,其实它是一个远比使用代码更好的解决办法。运行查询后显示是这样的:

WidgetID DateProduced   WidgetWeekID
-------  ------------   ------------
1          1/2/11            1
2          1/16/11           3
3          1/31/11           5

你还有什么其他不寻常的想法可以加入吗?下面请赐教。谢谢Ben。


另附示例:

点击下载此附件

【原文】
Matching records using a date range

The other day I asked Ben to help me with a thorny problem, I needed to match records in one table with records in another table using a date range. Here’s the table I needed to update, called tblWidgets:

WidgetID DateProduced   WidgetWeekID
-------  ------------   -----------
1           1/2/11
2           1/16/11
3           1/31/11

WidgetWeekID comes from another table called tblWidgetWeeks:


WidgetWeekID      StartDate    EndDate
 ------------     --------     -------
 1                12/29/10     1/5/11
 2                1/6/11       1/12/11
 3                1/13/11      1/20/11
 4                1/21/11      1/28/11
 5                1/29/11       2/5/11

I needed a query that would find DateProduced between StartDate and EndDate and update WidgetWeekID with the right number. Here’s the solution he came up with:

Update tblWidgets AS w
INNER JOIN tblWidgetWeeks AS wwk
  ON w.[DateProduced] >= wwk.StartDate AND w.[DateProduced ] <= wwk.EndDate
 SET w.WidgetWeekID = [wwk].[WidgetWeekID];

What's brilliant about Ben's solution is the join on the date range, a far better solution than using code. Once I ran the query the destination table looks like this:

WidgetID    DateProduced   WidgetWeekID
-------     ------------   ------------
1              1/2/11           1
2              1/16/11          3
3              1/31/11          5

What other unusual joins have you done? Please comment below. Thanks Ben!


相关类似示例:

数据字段值区间转换示例


Access软件网交流QQ群(群号:198465573)
 
 相关文章
access 2007查找具有最近或最早日期的记录  【Microsoft  2008/4/24】
[access查询][示例]按日期分组统计查询二种方法  【askheart  2008/9/12】
Access数据字段值区间转换示例\匹配成绩优良差、合格的示例,分...  【红尘如烟  2011/1/25】
根据不同的批发数量取不同的销售单价  【金宇  2013/4/22】
常见问答
技术分类
相关资源
文章搜索
关于作者

周芳

文章分类

文章存档

友情链接