[英]get previous date using sql
My query is 我的查询是
SELECT
matno,
MAT_NAME,
MAX(SWITCH(deldate=? ,ORDCASES )) AS [1/1/2014],
MAX(SWITCH(deldate=DateAdd("d", -1, ?),ORDCASES)) AS [previous_day]
FROM invorder
WHERE (invorder.strno =54009) OR ([invorder.deldate] IS NULL)
GROUP BY matno,MAT_NAME;
Here I can just check one before date of selected date,but if their are no records with previous date the I should decrement date by one more day and check until I find the records. 在这里,我只可以检查所选日期之前的日期,但是如果它们不是以前日期的记录,则我应该再减少日期一天,直到找到记录为止。 Can someone help me how can I decrement and find the previous date?
有人可以帮助我如何减少和找到上一个日期吗?
Without understanding the exact case, I would suggest an approach like: 在不了解确切情况的情况下,我建议采用以下方法:
SELECT MAX(date)
FROM myTable
WHERE date < '2014-01-01' --> Replace this as needed
This would result in the previous date with a record . 这将导致前一个日期带有记录 。 Without the need of checking each and every day by substraction.
无需每天通过减法检查。
I've tried to match your case, try this: 我已尝试匹配您的情况,请尝试以下操作:
SELECT invorder.matno,
invorder.MAT_NAME,
MAX(invorder.date) AS this_record_date,
MAX(previous.date) AS prev_record_date
FROM invorder
JOIN invorder AS previous
ON invorder.matno = previous.matno
AND invorder.strno = previous.strno
AND invorder.date > previous.date
WHERE invorder.strno =54009
GROUP BY invorder.matno,
invorder.MAT_NAME
This selects the maximum record by date
of strno
54009, it's matno
, MAT_NAME
and date
. 这
strno
date
strno
54009选择最大记录,即matno
, MAT_NAME
和date
。 This row is extended with the maximum date of the same matno
and strno
excluding the date we already selected (so pretty much the second most maximum date). 该行扩展了具有相同
matno
和strno
的最大日期,但不包括我们已经选择的日期(几乎是第二个最大日期)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.