选择最大日期和之前的日期

发布于 2024-09-08 03:05:50 字数 371 浏览 5 评论 0原文

我有一个数据表,除了周六和周日之外,每天都会更新。 问题在于,当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

楠木可依 2024-09-15 03:05:50

您可以选择至少一天前的最新记录。那么周末并不是一个特例。

You can select the newest record that is at least one day old. Then the weekend isn't a special case.

无畏 2024-09-15 03:05:50

您实际上是在尝试查找数据集中的第二高日期。

查找数据中的最高日期。查找小于此值的值,将结果限制为 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.

∝单色的世界 2024-09-15 03:05:50

这里还有一些其他选项也适用于任意 target_date。

如果每个日期只有一条记录,

SELECT * FROM table WHERE date<=target_date ORDER BY date DESC LIMIT 2

如果每个日期有很多记录,

SELECT * FROM table WHERE date IN (SELECT DISTINCT date FROM table WHERE date<=target_date ORDER BY date DESC LIMIT 2)

当然,您还可以使用数据库的日期/时间函数来检查星期一。

Here's some other options which work with an arbitrary target_date as well.

If you only have one record for each date,

SELECT * FROM table WHERE date<=target_date ORDER BY date DESC LIMIT 2

If you have many records per date,

SELECT * FROM table WHERE date IN (SELECT DISTINCT date FROM table WHERE date<=target_date ORDER BY date DESC LIMIT 2)

You could also use the date/time functions of your database to check for Monday, of course.

浅语花开 2024-09-15 03:05:50

嗯,您可能需要有关字符串的更多详细信息

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

倾听心声的旋律 2024-09-15 03:05:50

对我有用的一个简单方法是:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文