在SQL中计算给定月份的第一个工作日
我必须计算每月前“N”天内已支付的所有发票。 我有两张桌子
。 INVOICE:有发票信息。 唯一重要的字段称为“datePayment”
。 HOLYDAYS:这是一个单列表。 该表中的条目的格式为“2009-01-01”, 2009-05-01”等等。
我还应该考虑周六和周日 (这可能不是问题,因为我可以在假日表中插入那些日子,以便在必要时将它们视为假日)
问题是计算哪个是“付款限额”。
select count(*) from invoice
where datePayment < PAYMENTLIMIT
我的问题是如何计算这个 PAYMENTLIMIT。 其中 PAYMENTLIMIT 是“每月的第五个工作日”。
查询应在 Mysql 和 Oracle 下运行,因此应使用标准 SQL。
有什么提示吗?
编辑 为了与问题标题保持一致,伪查询应如下所示:
select count(*) from invoice
where datePayment < FIRST_WORKING_DAY + N
然后可以将问题简化为计算每个月的 FIRST_WORKING_DAY 。
I have to calculate all the invoices which have been paid in the first 'N' days of a month. I have two tables
. INVOICE: it has the invoice information. The only field which does matter is called 'datePayment'
. HOLYDAYS: It is a one column table. Entries at this table are of the form "2009-01-01",
2009-05-01" and so on.
I should consider also Saturdays and Sundays
(this might be not a problem because I could insert those days at the Hollidays table in order to consider them as hollidays if neccesary)
The problem is to calculate which is the 'payment limit'.
select count(*) from invoice
where datePayment < PAYMENTLIMIT
My question is how to calculate this PAYMENTLIMIT. Where PAYMENTLIMIT is 'the fifth working day of every month'.
The query should be run under Mysql and Oracle therefore standard SQL should be used.
Any hint?
EDIT
In order to be consistent with the title of the question the pseudo-query should the read as follows:
select count(*) from invoice
where datePayment < FIRST_WORKING_DAY + N
then the question can be reduced to calculate the FIRST_WORKING_DAY of every month.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
返回当月的第一个星期一
Returns the first Monday of the current month
周六和周日是 5 点、6 点,因此您只需要两次检查即可获得第一个工作日
Saturdays and Sundays are 5, 6 so you only need two checks to get the first working day
您可以查找一个月中的第一个日期,该日期不在假期表中并且该日期不是周末:
You could look for the first date in a month, where the date is not in the holiday table and the date is not a weekend:
像这样的事情可能会起作用:
我不能 100% 确定“工作日”数字是固定的还是可能取决于 SQL Server 上的区域设置。 一探究竟!
马克
Something like this might work:
I'm not 100% sure about whether the "weekday" numbers are fixed or might depend on your locale on your SQL Server. Check it out!
Marc
我们没有使用要排除的假期表,而是使用日历表方法:一行代表应用程序需要的每一天(三十年跨越了适度的 11K 行)。 因此,它不仅具有
is_weekday
列,还具有与企业相关的其他内容,例如julianized_date
。 这样,每个可能的日期都会有一个现成的first_working_day_this_month
值,并且查找它需要一个简单的查找(SQL 产品往往会针对该值进行优化!),而不是每次都“计算”它飞。Rather than a Holidays table of days to exclude, we use the calendar table approach: one row for every day the application will ever need (thirty years spans a modest 11K rows). So not only does it have an
is_weekday
column, it has other things relevant to the enterprise e.g.julianized_date
. This way, every possible date would have a ready-prepared value forfirst_working_day_this_month
and finding it involves a simple lookup (which SQL products tend to be optimized for!) rather than 'calculating' it each time on the fly.我们的应用程序中有日期表(填充了数十年的所有日期和日期部分),它允许各种“缺失”日期操作,例如(在伪sql中):
We have dates table in our application (filled with all dates and date parts for some tens of years), what allows various "missing" date manipulations, like (in pseudo-sql):
好的,第一次尝试时,您可以将以下代码放入 UDF 中,并将年份和月份作为变量传递。 然后它可以返回 TestDate,这是该月的第一个工作日。
但我不喜欢这个解决方案的是,如果您的假期表包含该月的所有天,则会出现无限循环。 (您可以检查循环是否仍在查看正确的月份)它依赖于相同的日期,例如全部在时间 00:00:00。 最后,我使用字符串连接计算过去一个月的第一天的方法是一个捷径。 有很多更好的方法可以找到该月的实际第一天。
Ok, at a first stab, you could put the following code into a UDF and pass in the Year and Month as variables. It can then return TestDate which is the first working day of the month.
The things I don`t like with this solution though are, if your holidays table contains all days of the month there will be an infinite loop. (You could check the loop is still looking at the right month) It relies upon the dates being equal, eg all at time 00:00:00. Finally, the way I calculate the 1st of the month past in using string concatenation was a short cut. There are much better ways of finding the actual first day of the month.
获取 2009 年每个月的前 N 个工作日:
Gets the first N working days of each month of year 2009: