November 26, 2013 at 11:24 am
hi to all,
i am facing issue for the following query:
for set @FromDate='1/1/2013' set @ToDate='1/14/2013' for this date the sum output is showing correct,but i have changed the todate as
set @FromDate='1/1/2013' set @ToDate='1/30/2013' exact output not displayed
please help me on this:
declare @FromDate date
declare @ToDate date
declare @Timeperiod varchar(30)='Weekly'
declare @vendor varchar(50)='V00616'
set @FromDate='1/1/2013' set @ToDate='1/30/2013'
if(@Timeperiod='Weekly')
begin
;with cte as
(
SELECT CASE DATEPART(WEEKDAY,@FromDate)
WHEN 1 THEN DATEadd (DAY,5,@FromDate)
WHEN 2 THEN DATEadd (DAY,4,@FromDate)
WHEN 3 THEN DATEadd (DAY,3,@FromDate)
WHEN 4 THEN DATEadd (DAY,2,@FromDate)
WHEN 5 THEN DATEadd (DAY,1,@FromDate)
WHEN 6 THEN DATEadd (DAY,0,@FromDate)
WHEN 7 THEN DATEadd (DAY,6,@FromDate)
END WeekEnding
,@ToDate EndDate
union all
select dateadd(Wk, 1, WeekEnding),dateadd(Wk, 1, EndDate)
from cte
where dateadd(Wk, 0, WeekEnding) < =@ToDate
)
select WeekEnding,
((SELECT sum([Quantity]) from
where ([ Date] Between WeekEnding and EndDate ))
+
(select sum([Quantity]) from [table1]
where ([Document Type]= 1) and ([Item Category Code] = 'STYLES') and
([Buy-from Vendor No_] in (@vendor)) and ([Quantity]>0) and ([Prod_ Order No_]='') and ([ Date] Between WeekEnding and EndDate))) 'Items Ordered from QT (MPRO/PO)'
from cte
end
November 26, 2013 at 11:33 am
So what is the question?
Once we know what the actual problem is we still need a couple things:
1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data
Please take a few minutes and read the first article in my signature for best practices when posting questions.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply