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.
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 ADVANCEdeclare @RunDate as datetimeset @RunDate='09-18-2013'selectmarca,data,nrore FROM tbl_PO_OreSuplimentare as Weeks17group by marca,data,nrorehaving 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 datetimeset @RunDate='09-18-2013'selectmarca,data,nrore FROM tbl_PO_OreSuplimentare as Weeks17group by marca,data,nrorehaving 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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
silvino90
Starting Member
7 Posts |
Posted - 2013-09-24 : 04:50:56
|
something like that. only that i have to limit to month of @RunDatethank you very much :)declare @RunDate as datetimeset @RunDate='09-18-2013'selectmarca,data,nrore FROM tbl_PO_OreSuplimentare as Weeks17group by marca,data,nrorehaving 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 |
|
|
|
|
|
|
|