Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query from 2 difrent dates

Author  Topic 

silvino90
Starting Member

7 Posts

Posted - 2013-09-23 : 07:02:22
Hi there,
First please excuse my writing.
I would like some help in the next scenario case.
I have to run a query with "dynamic" dates: from a specific date @RunDate and from max(Date) of the month(@RunDate). If max(Date) is greater than @RunDate and is in the same month.. then the interval will be from max(Date)-17 weeks, else from @RunDate-17 weeks.
here is what i have done so far but i'm stuck.
THANK YOU IN ADVANCE


declare @RunDate as datetime
set @RunDate='09-18-2013'

select
marca,data,nrore FROM tbl_PO_OreSuplimentare as Weeks17
group by marca,data,nrore
having NrOre>0 -- and marca=20000301
and
(
Weeks17.Data > DateAdd(ww,-17,@RunDate) and Weeks17.Data <=DateAdd(ww,0,@RunDate)
OR Weeks17.Data > DateAdd(ww,-17,max(Data)) and Weeks17.Data <=DateAdd(ww,0,max(Data))
)
order by marca, data

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-23 : 13:38:38
do you mean this?

declare @RunDate as datetime
set @RunDate='09-18-2013'

select
marca,data,nrore FROM tbl_PO_OreSuplimentare as Weeks17
group by marca,data,nrore
having NrOre>0 -- and marca=20000301
and
(
Weeks17.Data > DateAdd(ww,-17,CASE WHEN max(Data) > @RunDate THEN max(Data) ELSE @RunDate END) and Weeks17.Data <=DateAdd(ww,0,CASE WHEN max(Data) > @RunDate THEN max(Data) ELSE @RunDate END)
)
order by marca, data


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

silvino90
Starting Member

7 Posts

Posted - 2013-09-24 : 04:50:56
something like that. only that i have to limit to month of @RunDate
thank you very much :)


declare @RunDate as datetime
set @RunDate='09-18-2013'

select
marca,data,nrore FROM tbl_PO_OreSuplimentare as Weeks17
group by marca,data,nrore
having NrOre>0 and marca=20009353
and
(
Weeks17.Data > DateAdd(ww,-17,CASE WHEN max(Data) > @RunDate and month(Data)=month(@Rundate) THEN max(Data) ELSE @RunDate END)
and Weeks17.Data <=DateAdd(ww,0,CASE WHEN max(Data) > @RunDate and month(Data)=month(@Rundate) THEN max(Data) ELSE @RunDate END)
)
order by marca, data
Go to Top of Page
   

- Advertisement -