| Author |
Topic |
|
Petronas
Posting Yak Master
134 Posts |
Posted - 2008-11-17 : 12:24:34
|
| Hi,The below query needs to pulls data for the last month and the month before that depending on the startdate.So, if I enter startDate ='11/15/2008', it should shows me data for Oct 08 and Sept 08.if I enter startDate ='9/15/2008' it should show me data for Aug 08 and Jul 08.The query that I have is not changing the numbers for the "last month", "Two_months_ago".even if I enter diffrent dates in the start_date, it just shows the same numbers. Is somthing wrong in the logic ?Declare @Get_Date datetime, @StartDate Varchar(30)select @StartDate = '11/1/2008'Select @Get_Date = convert(varchar ,month(Getdate()) )+ '/01/' +convert(varchar, year(Getdate()))select distinct i.Type, i.Bill_Desc, i.group Product_Group ,isnull(Union.Last_Month,0) Last_Month , isnull(Union.Two_Months_Ago,0) Two_Months_Agofrom v_customer ileft join (select Type, Bill_Desc, group, sum(Last_Month) 'Last_Month', sum(Two_Months_Ago) 'Two_Months_Ago' from (select Type, Bill_Desc, group, Mon_Num, Case when Mon_Num = 'MON1' then sum(Tot_Count) else '' end 'Last_Month',Case when Mon_Num = 'MON2' then sum(Tot_Count) else '' end 'Two_Months_Ago',From(select 'MON1' Mon_Num, isnull(count(*),0) Tot_Count, Type, Bill_Desc, group from v_customer where order_date >= dateadd(m,-1,@Get_Date) and order_date < @Get_DateGroup by Type, Bill_Desc, groupUnion allselect 'MON2' Mon_Num, isnull(count(*),0) Tot_Count, Type, Bill_Desc, groupfrom v_customer where order_date >= dateadd(m,-2,@Get_Date) and order_date < dateadd(m,-1,@Get_Date)Group by Type, Bill_Desc, groupGroup by Type, Bill_Desc, group) MonthlyTable1Group by Mon_Num, Type, Bill_Desc, group) MonthlyTable2Group by Type, Bill_Desc, group) UnionOn i.Type = Union.Type and i.Bill_Desc = Union.Bill_Desc and i.group = Union.groupWhere isnull(Union.Last_Month,0)+ isnull(Union.Two_Months_Ago,0) > 0and i.Customer_Type <> 'ABC'Thanks for looking,Petronas. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-17 : 12:29:51
|
dont use varchar for dates. treat it as date itselfDeclare @Get_Date datetime, @StartDate datetimeselect @StartDate = '11/1/2008'Select @Get_Date = dateadd(mm,datediff(mm,0,@StartDate),0)select distinct i.Type, i.Bill_Desc, i.group AS Product_Group,isnull(Union.Last_Month,0) AS Last_Month , isnull(Union.Two_Months_Ago,0) AS Two_Months_Agofrom v_customer ileft join(select Type, Bill_Desc, group, sum(Last_Month) 'Last_Month', sum(Two_Months_Ago) 'Two_Months_Ago'from(select Type, Bill_Desc, group, Mon_Num,Case when Mon_Num = 'MON1' then sum(Tot_Count) else '' end 'Last_Month',Case when Mon_Num = 'MON2' then sum(Tot_Count) else '' end 'Two_Months_Ago',From(select 'MON1' Mon_Num, isnull(count(*),0) Tot_Count, Type, Bill_Desc, groupfrom v_customerwhere order_date >= dateadd(m,-1,@Get_Date) and order_date < @Get_DateGroup by Type, Bill_Desc, groupUnion allselect 'MON2' Mon_Num, isnull(count(*),0) Tot_Count, Type, Bill_Desc, groupfrom v_customerwhere order_date >= dateadd(m,-2,@Get_Date) and order_date < dateadd(m,-1,@Get_Date)Group by Type, Bill_Desc, groupGroup by Type, Bill_Desc, group) MonthlyTable1Group by Mon_Num, Type, Bill_Desc, group) MonthlyTable2Group by Type, Bill_Desc, group) UnionOn i.Type = Union.Type and i.Bill_Desc = Union.Bill_Desc and i.group = Union.groupWhere isnull(Union.Last_Month,0)+ isnull(Union.Two_Months_Ago,0)> 0and i.Customer_Type <> 'ABC' |
 |
|
|
Petronas
Posting Yak Master
134 Posts |
Posted - 2008-11-17 : 12:53:47
|
| Thanks, again for your help. It worked when I did not use varchar. But in the report I have to define startdate as varchar(string) because it takes two parmaeters either 'M' (last month) or you can enter the date. Is there anyone that I can keep startdate as varchar and convert the query . I beleive I need to convert just this part for it to workSelect @Get_Date = dateadd(mm,datediff(mm,0,@StartDate),0)Truly appreciate your help,Petronas |
 |
|
|
|
|
|