选择最大日期和之前的日期
我有一个数据表,除了周六和周日之外,每天都会更新。 问题在于,当我使用 max (date) 和 max (date) -1 检索数据时,
但是当我尝试检索今天(星期一)和昨天(星期日)的数据时,当 max (date) -1 不存在时,它会失败存在。
数据可以在周六和周日更新,但由于是汇率,我会更新。 周五、周六和周日的汇率会相同吗?
这是解决问题的一种方法,但是还有更好的方法
string weekend = DateTime.Now.DayOfWeek.ToString();
if (weekend == "Monday")
{
select ***** where max(date)-3 from *****
}
I have a data table which is updated every day except Saturday and Sunday.
The problem lies in so that when I retrieve data with max (date) and max (date) -1
But it fails when I try to retrieve data for today (Monday) and yesterday (Sunday) when max (date) -1 does not exist.
the data can be updated on Saturday and Sunday, but since it's exchange rate I update.
Will it give the same exchange rates Friday, Saturday and Sunday.
This is one way to solve the problem this way, but there is a better
string weekend = DateTime.Now.DayOfWeek.ToString();
if (weekend == "Monday")
{
select ***** where max(date)-3 from *****
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以选择至少一天前的最新记录。那么周末并不是一个特例。
You can select the newest record that is at least one day old. Then the weekend isn't a special case.
您实际上是在尝试查找数据集中的第二高日期。
查找数据中的最高日期。查找小于此值的值,将结果限制为 1,并且数据集中包含前一天。
任何其他方法在费率未更新的其他日子(例如圣诞节)都会失败。
You are effectively trying to find the second highest date in a dataset.
Find the highest date in the data. Find values less than this, limit the results to 1 and you have the previous day in the dataset.
Any other method will fail on other days when rates are not updated, e.g. christmas.
这里还有一些其他选项也适用于任意 target_date。
如果每个日期只有一条记录,
如果每个日期有很多记录,
当然,您还可以使用数据库的日期/时间函数来检查星期一。
Here's some other options which work with an arbitrary target_date as well.
If you only have one record for each date,
If you have many records per date,
You could also use the date/time functions of your database to check for Monday, of course.
嗯,您可能需要有关字符串的更多详细信息
SQL 字符串为
SELECT m.Navn, m.Beskrivelse, p_idag.Points AS Points_idag, p_igaar.Points AS Points_igaar FROM medlem m LEFT JOIN (SELECT * FROM point WHERE Datoen = '06- 09-2010') AS p_idag ON m.Navn = p_idag.Navn LEFT JOIN (SELECT * FROM point WHERE Datoen = '06-08-2010') AS p_igaar ON m.Navn = p_igaar.Navn ORDER BY m.Navn;
第一个日期由用户选择,下一个日期即日期-1天或-3天
hmm you might need som more details on the string
The sql string is
SELECT m.Navn, m.Beskrivelse, p_idag.Points AS Points_idag, p_igaar.Points AS Points_igaar FROM medlem m LEFT JOIN (SELECT * FROM point WHERE Datoen = '06-09-2010') AS p_idag ON m.Navn = p_idag.Navn LEFT JOIN (SELECT * FROM point WHERE Datoen = '06-08-2010') AS p_igaar ON m.Navn = p_igaar.Navn ORDER BY m.Navn;
The first date is selected by the user, the next date that date -1 day or -3
对我有用的一个简单方法是:
select * from yourtable where date=(select max(date) from yourtable
其中日期 < (从您的表中选择最大(日期)))
不确定它的效率如何
An easy way that works for me is:
select * from yourtable where date=(select max(date) from yourtable
where date < (select max(date) from yourtable))
Not sure how efficient it is