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)
 [Resolved] datepart - add month

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-01 : 14:14:05
Have following:

sum(case when datepart(month, date) = datepart(month, @DateFrom  then gross else 0 end) as quantity1


Now I would like to capture quantities using @DateFrom + 1 month, like:

sum(case when datepart(month, date) = datepart(month, dateadd(month, 1, @DateFrom)  then gross else 0 end) as quantity2


Is this possible?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-01 : 14:32:21
-- Current month
sum(case when datepart(month, date, @datefrom) = 0 then gross else 0 end) as quantity2

-- Negative value n means date is n months after @datefrom
sum(case when datepart(month, date, @datefrom) = {negative value} then gross else 0 end) as quantity2

-- Positive value n means date is n months before @datefrom
sum(case when datepart(month, date, @datefrom) = {positive value} then gross else 0 end) as quantity2


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-01 : 14:47:09
Peso, sorry I am slow today. I assume this measn my code is correct? As an alternative could I have used "over(partition by....", would this have been better? Thank you.

(hmmmm working Saturday night, thought you would be out having a Pripps...)
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-08-01 : 16:00:10
I'm not sure exactly what you are looking for, but hopfully this helps you out.


declare @Mytable table (Mydate datetime,gross money)

insert Into @MYtable(Mydate,Gross)
select '06/12/2009',11 union all
select '05/12/2009',11 union all
select '07/12/2009',21 union all
select '06/22/2009',51 union all
select '07/13/2009',71 union all
select '01/21/2009',21 union all
select '07/20/2009',11 union all
select '08/12/2009',2 union all
select '06/5/2009',5 union all
select '06/5/2009',3 union all
select '08/2/2009',1
--select datediff(month,'7/1/2009','06/2/2009')
-- Current month
declare @Datefrom datetime
set @Datefrom = '7/5/2009'
select
sum(case when datediff(month, @datefrom,mydate ) = 0 then gross else 0 end) as CurrentMonth
,sum(case when datediff(month,@datefrom,mydate) = 1 then gross else 0 end) as NextMonth
,sum(case when datediff(month,@datefrom,mydate) = -1 then gross else 0 end) as PriorMonth
from
@mytable a

--or you can do something like

select month(mydate) as [Month],sum(Gross)
from
@mytable a
where month(mydate) between month(@DateFrom) -1 and month(@DateFrom) +1
group by month(mydate)




Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-01 : 16:06:05
quote:
Originally posted by snufse

(hmmmm working Saturday night, thought you would be out having a Pripps...)
Pripps? When there is Carlsberg?


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-08-01 : 16:13:28
I think it was labled correct?

declare @Datefrom datetime
set @Datefrom = '7/5/2009'

select datediff(month,@datefrom,'08/2/2009')

--8/2/2009 if I interprited right should be labled the "NextMonth", correct?


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-01 : 16:27:03
Thank guys for input, think I got it working, maybe not the best code, but... last 12 month qty summary (not including current month)....

declare @DateFrom datetime
declare @DateTo datetime

set @DateFrom = dateadd(month, datediff(month, 0, getdate()) - 13, 0) -- start date
set @DateTo = dateadd(month, datediff(month, 0, getdate()) - 1, 30) -- end date

[code]insert into #Table1
(
petroex_code,
product_code,
product_name,
qty_month_1,
qty_month_2,
qty_month_3,
qty_month_4,
qty_month_5,
qty_month_6,
qty_month_7,
qty_month_8,
qty_month_9,
qty_month_10,
qty_month_11,
qty_month_12
)
select petroexcode as PetroEXCode,
branded as ProductCode,
substring(name,1,30) as ProductName,
sum(case when datepart(month, date) = datepart(month, @DateFrom) then gross else 0 end) as quantity1,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 1, @DateFrom)) then gross else 0 end) as quantity2,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 2, @DateFrom)) then gross else 0 end) as quantity3,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 3, @DateFrom)) then gross else 0 end) as quantity4,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 4, @DateFrom)) then gross else 0 end) as quantity5,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 5, @DateFrom)) then gross else 0 end) as quantity6,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 6, @DateFrom)) then gross else 0 end) as quantity7,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 7, @DateFrom)) then gross else 0 end) as quantity8,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 8, @DateFrom)) then gross else 0 end) as quantity9,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 9, @DateFrom)) then gross else 0 end) as quantity10,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 10, @DateFrom)) then gross else 0 end) as quantity11,
sum(case when datepart(month, date) = datepart(month, dateadd(month, 11, @DateFrom)) then gross else 0 end) as quantity12

from [SFM-TP6000-1].TP6000.dbo.product as p
inner join [SFM-TP6000-1].TP6000.dbo.loadcomp as l on l.branded = p.product
where (@DateFrom is null or date >= @DateFrom) and
(@DateTo is null or date < dateadd(day, 1, @DateTo))
group by petroexcode, branded, name
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-01 : 16:31:55
[code]sum(case when datediff(month, date, @DateFrom) = 0 then gross else 0 end) as quantity1,
sum(case when datediff(month, date, @DateFrom) = 1 then gross else 0 end) as quantity2,
sum(case when datediff(month, date, @DateFrom) = 2 then gross else 0 end) as quantity3,
sum(case when datediff(month, date, @DateFrom) = 3 then gross else 0 end) as quantity4,
sum(case when datediff(month, date, @DateFrom) = 4 then gross else 0 end) as quantity5,
sum(case when datediff(month, date, @DateFrom) = 5 then gross else 0 end) as quantity6,
sum(case when datediff(month, date, @DateFrom) = 6 then gross else 0 end) as quantity7,
sum(case when datediff(month, date, @DateFrom) = 7 then gross else 0 end) as quantity8,
sum(case when datediff(month, date, @DateFrom) = 8 then gross else 0 end) as quantity9,
sum(case when datediff(month, date, @DateFrom) = 9 then gross else 0 end) as quantity10,
sum(case when datediff(month, date, @DateFrom) = 10 then gross else 0 end) as quantity11,
sum(case when datediff(month, date, @DateFrom) = 11 then gross else 0 end) as quantity12[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-01 : 16:39:33
Peso, OK I get our point !!!!

Just thought you were supporting the economy? That's why I suggested Pripps.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-01 : 16:41:13
There are some limits though on what is acceptable to drink.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-01 : 16:49:41
If that is the case, then you need to go "Norsk", no pollution there and no taste of petroleum either! (as far as I remember going years back).... now I am sticking to Tecate (being close to the border and influenced by the culture)....
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-02 : 12:40:28
hehehe MY pricing on carls(first bottle USD6.5(RM25) following bottle USD0.1(rm0.50))


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page
   

- Advertisement -