SQL - 两个日期之间的查询范围(非 VBA)
我看到了有关堆栈溢出的各种主题,但没有一个适合 MS-Access 的内容...
给定开始日期和结束日期,是否有办法通过 SQL 返回时间范围内每个给定月份的记录?
EG:
A record has a Start Date of #1/1/2010# and an End Date of #1/31/2010#
Running the query against that single record, I would like the results to be
#1/4/2010# (Monday the 4th)
#1/11/2010# (Monday the 11th)
#1/18/2010# ...etc
#1/25/2010#
限制
- MS-Access 2003:SQL 内无 Case/Loops(IIF 语句很好)
- 这只是一个视图,不会使用 VBA,因为数据不会被篡改。断开连接的记录集是我的最后一个选择。我更愿意找出某种方法来调用 SQL 中的自定义函数来帮助返回这些值...当您迭代此日期范围时,某些类存储在全局范围内...
这可能吗?我看到很多不,但如果有一种方法可以将值传递到函数中,我可以找到一种方法来完成这项工作。遗憾的是我没有办法在不使用 ad/c 记录集的情况下模拟存储过程,至少我知道......那里的专家知道一种方法吗?
I see various topics on this around stack overflow but none that fit the contect of MS-Access...
Given a starting date and an ending date, is there a way through SQL to return records for each given month within the time frame?
EG:
A record has a Start Date of #1/1/2010# and an End Date of #1/31/2010#
Running the query against that single record, I would like the results to be
#1/4/2010# (Monday the 4th)
#1/11/2010# (Monday the 11th)
#1/18/2010# ...etc
#1/25/2010#
Restrictions
- MS-Access 2003 :No Case/Loops inside the SQL (IIF statements are good)
- This is a view only, NO VBA will be used since the data will not be tampered with. Disconnected recordset is my last option. I would prefer to find out theres some way to call your customized functions in the SQL to help return these values... some class stored on a global scope while you iterate through this date range maybe...
Is this possible? I see many no's, but if there was a way to pass a value into a function I could find a way to make this work. Sad that I don't have a way to simulate a stored procedure without using a d/c recordset, at least that I know of... any experts out there know a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于这可能与报告有关,您可以使用临时表方法,这还不错,特别是如果您可能在给定时间段内运行多个报告(但要注意多个用户)。
现在,只是为了好玩,这里有一种超级丑陋但半灵活的方法(使用有 10 个条目的表,工作范围为 273 年)
创建一个名为 t10 的表,其中包含一个字段 - id(让它很长并且是主键)并输入十行:0,1,2,3..9
之后
返回从 2010-1-1 到 2283-10-15 的所有星期一,并且速度并不慢,因为它很难看。
当然,要获得问题的解决方案,您必须过滤字段 date1 和 date2 的结果。
这本质上是与临时表相同的解决方案,主要区别在于您不必在每个查询上重建表。
您可以仅使用一个条目为 0..99999 的表来实现相同的结果,如果我要使用临时表,我可能会使用类似的东西。
Since this is probably related to reporting you could use temporary table approach, that is not so bad, especially if you might be running multiple reports against a given period (but do take care about multiple users).
Now, just for fun, here's a super-ugly, but semi-flexible approach (uses table with 10 entries and works for a range of 273 years)
Make a table called t10 with one field - id (make it long and primary key) and enter ten rows: 0,1,2,3..9
After that
returns all Mondays from 2010-1-1 to 2283-10-15 and is not as slow as it is ugly.
Of course to get a solution for your question you'll have to filter the results for your fields date1 and date2.
This is in essence same solution as having temporary table, with the main difference that you don't have to rebuild the table on each query.
You could achieve the same result with only one table with entries 0..99999, and if i was to use temp tables I would probably use something like that.
您正在讨论从单个记录中生成多个记录(每个日期一个)的查询。我想这应该通过存储过程(如果您的数据库是 SQL)或通过记录集来完成。任何查询都无法解决问题。创建记录集并在需要时添加记录对我来说似乎非常简单。你应该去争取。
You are talking about a query that generates multiple records (one per date) out of a single record. I guess this should be made through a stored procedure (if your database is SQL) or through recordsets. No query will do the trick. Creating a recordset and adding records where needed seems quite straightforward to me. You should go for it.
我希望我正确理解你的问题。为此,我们需要创建一个笛卡尔积 - 因此您应该创建另一个表。它只需要一个 ID,其中包含从 1 到日期范围将包含的最大周数的数字。
对于测试数据,我使用您的数据创建了一个模拟表
,将生成结果的 msaccess sql 语句如下所示:
如果您想将日期从星期一更改为另一天,请更改工作日函数中的参数。
1=星期日->7=星期六
希望对您有用。
I hope I understand your question correctly. To to this we need to create a cartesian product - so you should create another table. It only needs an ID in it with numbers that go from 1 to the highest number of weeks that your date range will contain.
for test data I created a mock table with your data
the msaccess sql statement that will produce your results looks like
If you want to change the day from Monday to another day, change the argument in the weekday function.
1=Sunday->7=Saturday
Hope that works for you.
您可以创建一个巨大的日历表,其中包含日期和天数。 2010 年 4 月 12 日,星期一; 2010 年 4 月 13 日、星期二等。然后查询该巨型表以获取结果。过去和未来需要多大的范围?尽管这是一个笨拙的解决方案,但它肯定会使查询变得干净简单。
You could create a giant calendar table, with dates and days in it. 4/12/10, Monday; 4/13/10, Tuesday, etc. Then query that giant table for the results. How big of range in the past and future do you need? Even though this is a clumsy solution, it would certainly make the queries clean and simple.