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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query from 2 difrent dates
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

silvino90
Starting Member

7 Posts

Posted - 09/23/2013 :  07:02:22  Show Profile  Reply with Quote
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

India
52326 Posts

Posted - 09/23/2013 :  13:38:38  Show Profile  Reply with Quote
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 - 09/24/2013 :  04:50:56  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.16 seconds. Powered By: Snitz Forums 2000