August 18, 2004 at 3:35 am
Hi
Needed a query giving the data from the last siX months.
Did now something like where month(dateorder) in (month(getdate()-1,month(getdate()-2, etc...
Is there an easier way ?
thx in advance
JV
August 18, 2004 at 3:46 am
August 19, 2004 at 6:38 am
Grasshopper you beat me to it...
However do want to include that the year should be defined as well.
Add datediff(yy,dateorder,getdate())=0
Had a report that gave too many results and remembered that I had not defined the year.
Doug
August 20, 2004 at 1:39 am
hi,
sorry but i still get different results when i check it with my query.
SELECT DISTINCT
TOP 100 PERCENT YEAR(DTFACT) AS Jaar, CODART AS Artikelcode, LBART1 AS artikelomschrijving, COUNT(CODNAT) AS [Aantal OL], SUM(QUANTI)
AS [Aantal Stuks], SUM(MTTHT) AS Omzet
FROM dbo.VLIGVEN
WHERE
my piece : --(MONTH(DTFACT) IN (MONTH(GETDATE()) - 1, MONTH(GETDATE()) - 2, MONTH(GETDATE()) - 3, MONTH(GETDATE()) - 4, MONTH(GETDATE()) - 5, MONTH(GETDATE()) - 6)) and year(dtfact) = year(getdate())
your piece :
datediff(yy,dtfact,getdate())=0 and DateDiff(month, dtfact, GetDate()) < 6
GROUP BY YEAR(DTFACT), CODART, LBART1
ORDER BY YEAR(DTFACT) DESC, CODART
ANy idea ???
Thx
JV
August 20, 2004 at 10:53 am
try doing datediff for number of days instead.
Beer is the answer to all the world's woes.
August 20, 2004 at 12:35 pm
You are missing an = in the second WHERE.
Your WHERE asks for 1, 2, 3, 4, 5 AND 6 months ago.
The other WHERE asks for months LESS than 6 months ago.
Try this (note the =)
datediff(yy,dtfact,getdate())=0 and DateDiff(month, dtfact, GetDate()) <= 6
-SQLBill
August 20, 2004 at 12:38 pm
Mr. Bill,
Ooooops.... Ya got me.....
Should have used that or <7
Thanks
Doug
August 22, 2004 at 4:39 pm
Sometimes, you have to add to subtract...
WHERE DateOrder >= DATEADD(mm,-6,GETDATE())
Notice that we added a NEGATIVE six months to the current date. Will also handle jumps across years, centuries, leap years, etc.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply