Author |
Topic |
iori
Starting Member
20 Posts |
Posted - 2006-08-30 : 17:07:18
|
here is what i am trying to do, if my query right returnsthis data:Quote:sku# | Region | Cost | EffMonth124 A $5 7/06345 A $6 6/06221 B $4 7/06345 B $6 4/06 i want it to display this:Quote:sku# | Region | CurrMonth | PrevMonth | Cost-Diff | Prev Month124 A $5 $6 $1(6-5) 6/06221 B $4 $6 $2(6-4) 4/06 anyone? |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2006-08-30 : 20:35:48
|
[code]select Current.sku#, Current.Region, Current.EffMonth as CurrMonth, Previous.EffMonth as PrevMonth, Current.Cost as CurrCost, Previous.Cost as PrevCost, Previous.Cost - Current.Cost as Cost-Difffrom YourTable Current left outer join YourTable Previous on Current.Region = B.Region and Current.EffMonth = dateadd(m, 1, B.EffMonth)[/code] |
 |
|
pootle_flump
1064 Posts |
Posted - 2006-08-31 : 12:05:46
|
Minor thing but you need to enclose the interval in quotes in Access:.... dateadd("m", 1, B.EffMonth) |
 |
|
iori
Starting Member
20 Posts |
Posted - 2006-09-03 : 18:09:50
|
thanks blindman and pootle, that works. Now i m tryiing to put some constraints on the data returned. here is my actual querySELECT DISTINCT CURRENT.SKU, CURRENT.REGION, CURRENT.COST_TOT, PREVIOUS.COST_TOT, CURRENT.COST_TOT-PREVIOUS.COST_TOT AS COSTDIFF, CURRENT.EFF_DATE, PREVIOUS.EFF_DATEFROM COST_SKU AS [CURRENT] LEFT JOIN COST_SKU AS PREVIOUS ON (month(current.eff_date)=month(previous.eff_date)+1) AND (CURRENT.sku=PREVIOUS.sku) AND (CURRENT.REGION=PREVIOUS.REGION)WHERE MONTH(CURRENT.EFF_DATE)<=7 AND DATEDIFF("M",CURRENT.EFF_DATE,"7/12/06")<=12ORDER BY CURRENT.EFF_DATE DESC;where the text in green will be my parameters. so MONTH(CURRENT.EFF_DATE)<=7 says to get the records starting from the 7th month and DATEDIFF("M",CURRENT.EFF_DATE,"7/12/06")<=12 says to get the last 12 months only.the only problem i am having now is marked in red i.e.if i have eff_date that is of previous year say 2005 it wouldnt show up because i am only comparing months in my join statement as u notice above:month(current.eff_date)=month(previous.eff_date)+1how can i make the join to compare rows if the year changes, here is an exampleeff_date | price12/1/06 5.001/2/05 4.00this should join as there is a difference of 1 in the month. my query fails bcuz i am using only month to check the date difference. any suggestions? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-04 : 08:47:51
|
.. ON (month(current.eff_date)=month(previous.eff_date)+1) AND ABS(previous.eff_date-current.eff_date) <= 32 AND ...Peter LarssonHelsingborg, Sweden |
 |
|
iori
Starting Member
20 Posts |
Posted - 2006-09-04 : 12:35:18
|
peso,what does ABS(previous.eff_date-current.eff_date) <= 32 do, it doesnt affect the result. blindman had the right code i.e Current.EffMonth = dateadd(m, 1, B.EffMonth)for the provided sample dates (without day, just month and year) i had in the beginning. unfortunately it doesnt work when the day is there for exampledate | price4/12/06 4.003/5/06 3.00this wouldnt join because adding 1 to 3/5/06 will give 4/5/06 and the query wouldnt join the records |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-04 : 13:02:29
|
As you wrote yourself, you do not want this month 2006 to also include this month 2005. Therefore I added a new AND clause in addition to the existing telling that dates can be no more than 32 days apart, thus preventing this month to be calculated with this month last year (more than 32 days away).Peter LarssonHelsingborg, Sweden |
 |
|
iori
Starting Member
20 Posts |
Posted - 2006-09-04 : 13:41:53
|
thanks for replying peso. I said i want to join records if the current month's year is one greater than the last years december. oops..i just noticed i have my date wrong in the example. here is the corrected exampleeff_date | price1/2/05 4.0012/1/04 5.00so this wouldnt join if i usemonth(current.eff_date)=month(previous.eff_date)+1) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-04 : 17:25:58
|
Try to use ON 12 * YEAR(current.eff_date) + MONTH(current.eff_date) = 12 * YEAR(PREVIOUS.eff_date) + MONTH(PREVIOUS.eff_date) + 1instead.Peter LarssonHelsingborg, Sweden |
 |
|
iori
Starting Member
20 Posts |
Posted - 2006-09-05 : 13:04:36
|
that works on my test data!!! :) i still have to check it on the actual data. but hey thanks alot! i m still trying to decode what u did here |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-05 : 13:06:42
|
I am creating a kind of "serial number" for the months.Peter LarssonHelsingborg, Sweden |
 |
|
|