473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Calculating YTD

Hi everyone ,

I have an urgent report to make in Ms Access and consider me a beginner.

The problem is that I have data in an excel sheet which i have linked in the Access.It is a Pharma data and I have to calculate YTD sales for some Companies.

Its a 37months data and the months are categorized as M1,M2....M37 where M37 is the latest month. so the calulation for ytd would look like as

Ytd for m1, Ytd for m2, Ytd for m3 .... till M12 ,Ytd for m13
m1 m1+m2 m1+m2+m3 m13..


and as the new year starts with M13 it will again resume from the first point when it reaches M25 and would again start from M37 if you consider M1 to be January

I could have hard coded this formulae in MsAccess using normal sum but as it is always a 37 months data and the months move ahead and M1 changes from January to feburary and so the whole coding fails.

Can anyone help me to do this Its really important
Please revert if if you need any more explanation on my part
Mar 31 '08 #1
32 5253
Delerna
1,134 Expert 1GB
If I understand you correctly I must say I don't like it. However I fully understand having to create a solution using the currently used data stores that users insist on keeping.

The way I see it you have a spread sheet where your 37 months are in columns.
This is not ideal for the query you want so you first need to create a query that will convert them to rows. Also since the 37 months are rolling you also need to tag the rows with the correct year and month. Once you have that query the YTD query becomes a simple aggregate query.

Expand|Select|Wrap|Line Numbers
  1. SELECT Now()-day(now())+1 AS Dte, YourLinkedTable.M37 as Qty FROM YourLinkedTable where company="The Company"
  2. union all
  3. SELECT DateAdd("m",-1,Now()-day(now())+1) AS Dte, YourLinkedTable.M36 FROM YourLinkedTable where company="The Company"
  4. union all
  5. SELECT DateAdd("m",-2,Now()-day(now())+1) AS Dte, YourLinkedTable.M35 FROM YourLinkedTable where company="The Company"
  6. union all
  7. SELECT DateAdd("m",-3,Now()-day(now())+1) AS Dte, YourLinkedTable.M34 FROM YourLinkedTable where company="The Company"
  8. union all
  9. SELECT DateAdd("m",-4,Now()-day(now())+1) AS Dte, YourLinkedTable.M33 FROM YourLinkedTable where company="The Company"
  10. union all
  11. SELECT DateAdd("m",-5,Now()-day(now())+1) AS Dte, YourLinkedTable.M32 FROM YourLinkedTable where company="The Company"
  12. UNION ALL 
  13. SELECT DateAdd("m",-6,Now()-day(now())+1) AS Dte, YourLinkedTable.M31 FROM YourLinkedTable where company="The Company"
  14. UNION ALL 
  15. SELECT DateAdd("m",-7,Now()-day(now())+1) AS Dte, YourLinkedTable.M30 FROM YourLinkedTable where company="The Company"
  16. UNION ALL 
  17. SELECT DateAdd("m",-8,Now()-day(now())+1) AS Dte, YourLinkedTable.M29 FROM YourLinkedTable where company="The Company"
  18. UNION ALL 
  19. SELECT DateAdd("m",-9,Now()-day(now())+1) AS Dte, YourLinkedTable.M28 FROM YourLinkedTable where company="The Company"
  20. UNION ALL 
  21. SELECT DateAdd("m",-10,Now()-day(now())+1) AS Dte, YourLinkedTable.M27 FROM YourLinkedTable where company="The Company"
  22. union all
  23. SELECT DateAdd("m",-11,Now()-day(now())+1) AS Dte, YourLinkedTable.M26 FROM YourLinkedTable where company="The Company"
  24. union all
  25. SELECT DateAdd("m",-12,Now()-day(now())+1) AS Dte, YourLinkedTable.M25 FROM YourLinkedTable where company="The Company"
  26. union all
  27. SELECT DateAdd("m",-13,Now()-day(now())+1) AS Dte, YourLinkedTable.M24 FROM YourLinkedTable where company="The Company"
  28. union all
  29. SELECT DateAdd("m",-14,Now()-day(now())+1) AS Dte, YourLinkedTable.M23 FROM YourLinkedTable where company="The Company"
  30. union all
  31. SELECT DateAdd("m",-15,Now()-day(now())+1) AS Dte, YourLinkedTable.M22 FROM YourLinkedTable where company="The Company"
  32. UNION ALL 
  33. SELECT DateAdd("m",-16,Now()-day(now())+1) AS Dte, YourLinkedTable.M21 FROM YourLinkedTable where company="The Company"
  34. UNION ALL 
  35. SELECT DateAdd("m",-17,Now()-day(now())+1) AS Dte, YourLinkedTable.M20 FROM YourLinkedTable where company="The Company"
  36. UNION ALL 
  37. SELECT DateAdd("m",-18,Now()-day(now())+1) AS Dte, YourLinkedTable.M19 FROM YourLinkedTable where company="The Company"
  38. UNION ALL 
  39. SELECT DateAdd("m",-19,Now()-day(now())+1) AS Dte, YourLinkedTable.M18 FROM YourLinkedTable where company="The Company"
  40. UNION ALL 
  41. SELECT DateAdd("m",-20,Now()-day(now())+1) AS Dte, YourLinkedTable.M17 FROM YourLinkedTable where company="The Company"
  42. union all
  43. SELECT DateAdd("m",-21,Now()-day(now())+1) AS Dte, YourLinkedTable.M16 FROM YourLinkedTable where company="The Company"
  44. union all
  45. SELECT DateAdd("m",-22,Now()-day(now())+1) AS Dte, YourLinkedTable.M15 FROM YourLinkedTable where company="The Company"
  46. union all
  47. SELECT DateAdd("m",-23,Now()-day(now())+1) AS Dte, YourLinkedTable.M14 FROM YourLinkedTable where company="The Company"
  48. union all
  49. SELECT DateAdd("m",-24,Now()-day(now())+1) AS Dte, YourLinkedTable.M13 FROM YourLinkedTable where company="The Company"
  50. union all
  51. SELECT DateAdd("m",-25,Now()-day(now())+1) AS Dte, YourLinkedTable.M12 FROM YourLinkedTable where company="The Company"
  52. UNION ALL 
  53. SELECT DateAdd("m",-26,Now()-day(now())+1) AS Dte, YourLinkedTable.M11 FROM YourLinkedTable where company="The Company"
  54. UNION ALL 
  55. SELECT DateAdd("m",-27,Now()-day(now())+1) AS Dte, YourLinkedTable.M10 FROM YourLinkedTable where company="The Company"
  56. UNION ALL 
  57. SELECT DateAdd("m",-28,Now()-day(now())+1) AS Dte, YourLinkedTable.M9 FROM YourLinkedTable where company="The Company"
  58. UNION ALL 
  59. SELECT DateAdd("m",29,Now()-day(now())+1) AS Dte, YourLinkedTable.M8 FROM YourLinkedTable where company="The Company"
  60. UNION ALL 
  61. SELECT DateAdd("m",-30,Now()-day(now())+1) AS Dte, YourLinkedTable.M7 FROM YourLinkedTable where company="The Company"
  62. union all
  63. SELECT DateAdd("m",-31,Now()-day(now())+1) AS Dte, YourLinkedTable.M6 FROM YourLinkedTable where company="The Company"
  64. union all
  65. SELECT DateAdd("m",-32,Now()-day(now())+1) AS Dte, YourLinkedTable.M5 FROM YourLinkedTable where company="The Company"
  66. union all
  67. SELECT DateAdd("m",-33,Now()-day(now())+1) AS Dte, YourLinkedTable.M4 FROM YourLinkedTable where company="The Company"
  68. union all
  69. SELECT DateAdd("m",-34,Now()-day(now())+1) AS Dte, YourLinkedTable.M3 FROM YourLinkedTable where company="The Company"
  70. union all
  71. SELECT DateAdd("m",-35,Now()-day(now())+1) AS Dte, YourLinkedTable.M2 FROM YourLinkedTable where company="The Company"
  72. UNION ALL SELECT DateAdd("m",-36,Now()-day(now())+1) AS Dte, YourLinkedTable.M1 FROM YourLinkedTable where company="The Company";
  73.  
Now you see why I dont like it, but not using properly designed tables leads to queries like this. Also you will need one of these for each company. If I sat down and thought about it for a bit there is probably a better way but since this is urgent it should work.

Anyway, now you just need an agregate query on that query
I'll assume you call the above query qryDatedDataForTheCompany

Expand|Select|Wrap|Line Numbers
  1. SELECT year(Dte),sum(Qty) as YTD
  2. FROM qryDatedDataForTheCompany
  3. GROUP BY year(Dte)
  4.  
You can create a union query of the above from each of the company queries in order to get the YTD results for each company.
Well thats one solution, not very pretty but it should work for you, that is if I understood your post correctly.
Hope it works for you
Mar 31 '08 #2
Sir, first of all Thanks for your reply and I am sorry I got you in this mess.

But there are few questions I need to ask:

Q1 Do I have to replace "YourLinkedTable" in your query by the name of the Query from which i am gettting the total months data.(I am actually retrieving this data from another query in which I sumup the monthly sales of each company as the names are repeating)

Q2 Can we use Running sum expression in it?

Also can I send you a mock data?So that it gets easier for us.

I am sorry if I am acting like an idiot.I have been trying to find out a soln to this problem but I havnt been able too.
Mar 31 '08 #3
Delerna
1,134 Expert 1GB
No problems, glad to be of assistance. Thats why I do this.

Q1) I wrote the big query with the assumption that it would be using the linked table that is actually a spreadsheet. And I also had the assumption that the months were horizontal (columns) and the companies where vertical(rows)
like this
......Company...M1...M2...M3...M4...M5........etc
The Company...5.......6 ...86....44....31
Company Two..12.....34...24....37....78

If that is so, or as you say there is another query that sums up the data but still looks like that
then yes YourLinkedTable needs to be changed to the name of the linked spreadsheet or the query, whichever applies

Q2) Yes you can get a running sum with modification to the second query
And If the solution I have here isn't quite right, then by all means post mock up data and table structures and we will find a solution
Mar 31 '08 #4
This is absolutely how my Data looks like, but there are certain issues,There are around 173 companies in my database and in your above query I will have to replace "The Company" by the name of a respective company from my database.

Also the next step in this report is to get the YTD (on the similar lines) for the products which are around 1700 in number.

I just had one idea and that can we somehow categorize M1 to M12 as num1 and M13 to M24 as num2 and M25 to M36 as num3 and M37 as num4 (supposedly if M1=Jan and so M37 becomes Jan too) in an excel sheet and linkit in the access and then we can add on the base of these categorizations and everytime a new month arrives we can just change these categorizations ie. M1 to M11 as num1 as now M1 will be Feb and so M36 to M37 as 4.

I hope I am not sounding Crazy :).Its just that I have these ideas but I dont have the programming knowledge to convert them into soutions.

"Thank you so much Delerna for the time you are investing in this."
Apr 1 '08 #5
Delerna
1,134 Expert 1GB
OK so the spreadsheet now looks like this

......Company....Product...M1...M2...M3...M4...M5. .......etc
The Company....PROD1...5.......6 ...86....44....31
The Company....PROD2..12......4 ...43....22....20
Company Two...PROD4..12.....34...24....37....78

and your summing query looks like this

......Company......M1...M2....M3....M4...M5....... .etc
The Company.....17.....10 ..129....66....51
Company Two....12.....34....24.....37....78

Is that correct?

and we wany to generate the YTD from the summing Query?
We should be able to remove the where clause from the union query.
Apr 1 '08 #6
Exactly thats how I am doing this first I sumup and eliminate repetition and then we calculate the YTD from the monthly sales for both Products and Companies.


Meanwhile I did what you said and I changed the "YourLinkedTable" to my query name which is "0_Company_Monthly_Total_Sum"

Also as you said I removed the where clause so it looks like this

SELECT DateAdd("m",-33,Now()-day(now())+1) AS Dte, 0_Company_Monthly_Total_Sum.M4 FROM 0_Company_Monthly_Total_Sum
union ALL
SELECT DateAdd("m",-34,Now()-day(now())+1) AS Dte, 0_Company_Monthly_Total_Sum.M3 FROM 0_Company_Monthly_Total_Sum
union ALL
SELECT DateAdd("m",-35,Now()-day(now())+1) AS Dte, 0_Company_Monthly_Total_Sum.M2 FROM 0_Company_Monthly_Total_Sum
UNION ALL SELECT DateAdd("m",-36,Now()-day(now())+1) AS Dte, 0_Company_Monthly_Total_Sum.M1 FROM 0_Company_Monthly_Total_Sum;



I copied and pasted it in the SQL view of a new query and tried running it but it is showing an error which is "Syntax error in query expression
' 0_Company_Monthly_Total_Sum.M37' "
Apr 1 '08 #7
Delerna
1,134 Expert 1GB
OK, let me go and try this in the mockup database I made last night.
I will be back in a bit
Apr 1 '08 #8
Delerna
1,134 Expert 1GB
This works for me. I have assumed field names Company and Product. You will need to change them if yours are different
Expand|Select|Wrap|Line Numbers
  1. SELECT Now()-day(now())+1 AS Dte,Company,Product, M37 as Qty FROM 0_Company_Monthly_Total_Sum
  2. union all
  3. SELECT DateAdd("m",-1,Now()-day(now())+1) AS Dte,Company,Product, M36 FROM 0_Company_Monthly_Total_Sum
  4. union all
  5. SELECT DateAdd("m",-2,Now()-day(now())+1) AS Dte,Company,Product, M35 FROM 0_Company_Monthly_Total_Sum
  6. union all
  7. SELECT DateAdd("m",-3,Now()-day(now())+1) AS Dte,Company,Product, M34 FROM 0_Company_Monthly_Total_Sum
  8. union all
  9. SELECT DateAdd("m",-4,Now()-day(now())+1) AS Dte,Company,Product,M33 FROM 0_Company_Monthly_Total_Sum
  10. union all
  11. SELECT DateAdd("m",-5,Now()-day(now())+1) AS Dte,Company,Product, M32 FROM 0_Company_Monthly_Total_Sum
  12. UNION ALL 
  13. SELECT DateAdd("m",-6,Now()-day(now())+1) AS Dte,Company,Product, M31 FROM 0_Company_Monthly_Total_Sum
  14. UNION ALL 
  15. SELECT DateAdd("m",-7,Now()-day(now())+1) AS Dte,Company,Product,M30 FROM 0_Company_Monthly_Total_Sum
  16. UNION ALL 
  17. SELECT DateAdd("m",-8,Now()-day(now())+1) AS Dte,Company,Product, M29 FROM 0_Company_Monthly_Total_Sum
  18. UNION ALL 
  19. SELECT DateAdd("m",-9,Now()-day(now())+1) AS Dte,Company,Product, M28 FROM 0_Company_Monthly_Total_Sum
  20. UNION ALL 
  21. SELECT DateAdd("m",-10,Now()-day(now())+1) AS Dte,Company,Product, M27 FROM 0_Company_Monthly_Total_Sum
  22. union all
  23. SELECT DateAdd("m",-11,Now()-day(now())+1) AS Dte,Company,Product, M26 FROM 0_Company_Monthly_Total_Sum
  24. union all
  25. SELECT DateAdd("m",-12,Now()-day(now())+1) AS Dte,Company,Product, M25 FROM 0_Company_Monthly_Total_Sum
  26. union all
  27. SELECT DateAdd("m",-13,Now()-day(now())+1) AS Dte,Company,Product, M24 FROM 0_Company_Monthly_Total_Sum
  28. union all
  29. SELECT DateAdd("m",-14,Now()-day(now())+1) AS Dte,Company,Product, M23 FROM 0_Company_Monthly_Total_Sum
  30. union all
  31. SELECT DateAdd("m",-15,Now()-day(now())+1) AS Dte,Company,Product,M22 FROM 0_Company_Monthly_Total_Sum
  32. UNION ALL 
  33. SELECT DateAdd("m",-16,Now()-day(now())+1) AS Dte,Company,Product, M21 FROM 0_Company_Monthly_Total_Sum
  34. UNION ALL 
  35. SELECT DateAdd("m",-17,Now()-day(now())+1) AS Dte,Company,Product,M20 FROM 0_Company_Monthly_Total_Sum
  36. UNION ALL 
  37. SELECT DateAdd("m",-18,Now()-day(now())+1) AS Dte,Company,Product, M19 FROM 0_Company_Monthly_Total_Sum
  38. UNION ALL 
  39. SELECT DateAdd("m",-19,Now()-day(now())+1) AS Dte,Company,Product, M18 FROM 0_Company_Monthly_Total_Sum
  40. UNION ALL 
  41. SELECT DateAdd("m",-20,Now()-day(now())+1) AS Dte,Company,Product, M17 FROM 0_Company_Monthly_Total_Sum
  42. union all
  43. SELECT DateAdd("m",-21,Now()-day(now())+1) AS Dte,Company,Product,M16 FROM 0_Company_Monthly_Total_Sum
  44. union all
  45. SELECT DateAdd("m",-22,Now()-day(now())+1) AS Dte,Company,Product,M15 FROM 0_Company_Monthly_Total_Sum
  46. union all
  47. SELECT DateAdd("m",-23,Now()-day(now())+1) AS Dte,Company,Product, M14 FROM 0_Company_Monthly_Total_Sum
  48. union all
  49. SELECT DateAdd("m",-24,Now()-day(now())+1) AS Dte,Company,Product, M13 FROM 0_Company_Monthly_Total_Sum 
  50. union all
  51. SELECT DateAdd("m",-25,Now()-day(now())+1) AS Dte,Company,Product, M12 FROM 0_Company_Monthly_Total_Sum 
  52. UNION ALL 
  53. SELECT DateAdd("m",-26,Now()-day(now())+1) AS Dte,Company,Product, M11 FROM 0_Company_Monthly_Total_Sum 
  54. UNION ALL 
  55. SELECT DateAdd("m",-27,Now()-day(now())+1) AS Dte,Company,Product, M10 FROM 0_Company_Monthly_Total_Sum
  56. UNION ALL 
  57. SELECT DateAdd("m",-28,Now()-day(now())+1) AS Dte,Company,Product, M9 FROM 0_Company_Monthly_Total_Sum 
  58. UNION ALL 
  59. SELECT DateAdd("m",29,Now()-day(now())+1) AS Dte,Company,Product,M8 FROM 0_Company_Monthly_Total_Sum
  60. UNION ALL 
  61. SELECT DateAdd("m",-30,Now()-day(now())+1) AS Dte,Company,Product, M7 FROM 0_Company_Monthly_Total_Sum
  62. union all
  63. SELECT DateAdd("m",-31,Now()-day(now())+1) AS Dte,Company,Product,M6 FROM 0_Company_Monthly_Total_Sum
  64. union all
  65. SELECT DateAdd("m",-32,Now()-day(now())+1) AS Dte,Company,Product, M5 FROM 0_Company_Monthly_Total_Sum 
  66. union all
  67. SELECT DateAdd("m",-33,Now()-day(now())+1) AS Dte,Company,Product,M4 FROM 0_Company_Monthly_Total_Sum
  68. union all
  69. SELECT DateAdd("m",-34,Now()-day(now())+1) AS Dte,Company,Product,M3 FROM 0_Company_Monthly_Total_Sum
  70. union all
  71. SELECT DateAdd("m",-35,Now()-day(now())+1) AS Dte,Company,Product, M2 FROM 0_Company_Monthly_Total_Sum
  72. UNION ALL SELECT DateAdd("m",-36,Now()-day(now())+1) AS Dte,Company,Product, M1 FROM 0_Company_Monthly_Total_Sum;
  73.  
Apr 1 '08 #9
Delerna
1,134 Expert 1GB
I just had one idea and that can we somehow categorize M1 to M12 as num1 and M13 to M24 as num2 and M25 to M36 as num3 and M37 as num4 (supposedly if M1=Jan and so M37 becomes Jan too) in an excel sheet and linkit in the access and then we can add on the base of these categorizations and everytime a new month arrives we can just change these categorizations ie. M1 to M11 as num1 as now M1 will be Feb and so M36 to M37 as 4.
By the way, this could be done but the if statements to handle it would be horrendous
Apr 1 '08 #10
Delerna
1,134 Expert 1GB
The YTD query will become
Expand|Select|Wrap|Line Numbers
  1. SELECT year(Dte),Company,Product,sum(Qty) AS YTD
  2. FROM qryDatedDataForTheCompany
  3. GROUP BY year(Dte),Company,Product
  4.  
Apr 1 '08 #11
I checked the query,It worked but still the months are not adding up as they should.and the dates are not macthing as the years are adding upto 2010 also the months are getting started from april2005 rather then feb2005.

Thanks&Regards
Apr 1 '08 #12
Delerna
1,134 Expert 1GB
Hang on
Mth37 is the current month, correct?
This means that today Mth37 is April 2008.

Therefore
Mth37=Apr 2008
Mth36=Mar 2008
Mth35=Feb 2008
Mth34=Jan 2008

Mth33=Dec 2007
Mth32=Nov 2007
Mth31=Oct 2007
Mth30=Sep 2007
Mth29=Aug 2007
Mth28=Jul 2007
Mth27=Jun 2007
Mth26=May 2007
Mth25=Apr 2007
Mth24=Mar 2007
Mth23=Feb 2007
Mth22=Jan 2007

Mth21=Dec 2006
Mth20=Nov 2006
Mth19=Oct 2006
Mth18=Sep 2006
Mth17=Aug 2006
Mth16=Jul 2006
Mth15=Jun 2006
Mth14=May 2006
Mth13=Apr 2006
Mth12=Mar 2006
Mth11=Feb 2006
Mth10=Jan 2006

Mth9=Dec 2005
Mth8=Nov 2005
Mth7=Oct 2005
Mth6=Sep 2005
Mth5=Aug 2005
Mth4=Jul 2005
Mth3=Jun 2005
Mth2=May 2005
Mth1=Apr 2005
Which is what I get in my mock up.
Or am I missing something?
Apr 1 '08 #13
Nope you are not missing anything its me who has missed it,I apologise for that its just that we get data with a lag of 1 to 1 and half months so in march 2008 I have to make a report based on the feb 2008 data.

Also 2010 was coming because "-" was missing in the 59th statement of the query.

But finally the months got alright and are capturing the right data now as I just manupilated the date function which you gave so that it starts from March.

The only issue left is that the final Ytd query is giving me an Yearly breakdown but I need the Monthly breakdown in YTD

Thanks&Regards
Apr 1 '08 #14
Delerna
1,134 Expert 1GB
Thats Great
Can you post a mock up of how you want the final YTD query results to look and I will have a go at writing a query to achieve that when I get home tonight. That is if you haven't already achieved it by that time.
Apr 1 '08 #15
Yup here is the structure

Monthly Figures
......Company......M1...M2....M3....M4...M5....... .etc
The Company......10....20 ....30.....40...50
Company Two.....12......24.....36....48..60

Final Ytd structure based on the above monthly data would be

......Company......ytdM1...ytdM2....ytdM3....ytdM4 ...ytdM5....... .etc
The Company........10........30 .........60.........100.......150
Company Two.......12........36...........72..........120.. ....180


I am trying Dsum on it (Method 1) but till now its somehow not worked
http://support.microsoft.com/kb/290136

This is the link in which they have given the usage but somehow when I wrote the same for the orders table it is not recognizing the field.

Also i don't want to create any alien function in VBA as I have to connect the data with excel in the end and it doesn't recognize the query which is calculated through an external function.

Thanks&Regards
Apr 2 '08 #16
Delerna
1,134 Expert 1GB
Didn't you want it by product also
Apr 2 '08 #17
Yup but I wll have to show that in a different table separately,What we do in the report is give out top 10 companies and calculate there Monthly, Ytd and Mat sales.

Then we take out top20 Products sepeately and do the same for them Ranking is done on the basis of MAT sales.
Apr 2 '08 #18
Delerna
1,134 Expert 1GB
OK as a starting point I have written 3 queries 2 as initial steps and the third joins those to obtain the YTD as you require. I think.

The first query I called qryPrelim1. You can call it what you want but you will need to change the third query to match.
Expand|Select|Wrap|Line Numbers
  1. SELECT Year(Dte) AS Y, Month(dte) AS M, Dte, Company, Product, Qty
  2. FROM qryDatedDataForTheCompany;
  3.  
The second query I called qryPrelim2.
Expand|Select|Wrap|Line Numbers
  1. SELECT Year(Dte) AS Y, Month(dte) AS M, Dte, Company, Product, Qty
  2. FROM qryDatedDataForTheCompany;
  3.  
And the third uses those for the YTD
Expand|Select|Wrap|Line Numbers
  1. SELECT a.Company, a.Product, a.Dte, a.Qty, a.Qty+IIf(b.Qty Is Null,0,b.Qty) AS YTD
  2. FROM qryPrelim1 AS a LEFT JOIN qryPrelim2 AS b ON a.Y = b.Y AND a.M = b.M AND a.Company = b.Company AND a.Product = b.Product
  3. ORDER BY a.Company, a.Product, a.Dte;
  4.  
All 3 could be written as a single query through the use of subqueries but access keeps changing the ( ) containing the subquery into [ ] everytime you edit the query so I thought this would be easier for you.


Take a look at that and modify it however you need to achieve what you want.
I will be around for a few more hours playing solitaire and answering questions here. If you need further assistance
Apr 2 '08 #19
Its giving me this result
Company Dte............ ...Qty YTD In actual it should be
a 4/1/2005 3:51:02 PM ...10 20...........................10
a 5/1/2005 3:51:02 PM ...20 40...........................30
a 6/1/2005 3:51:02 PM ...30 60...........................60
a 7/1/2005 3:51:02 PM ...40 80...........................100
a 8/1/2005 3:51:02 PM ...50 100.........................200
a 9/1/2005 3:51:02 PM ...60 120.........................320
a 10/1/2005 3:51:02 PM...70 140.........................660
a 11/1/2005 3:51:02 PM...80 160.........................820
a 12/1/2005 3:51:02 PM...90 180.........................1000
a 1/1/2006 3:51:02 PM ...100 200.........................200(new YR )
a 2/1/2006 3:51:02 PM ..110 220.........................420
a 3/1/2006 3:51:02 PM ...120 240..........................660

Just a question Can we use Dsum where we can put a condition that it goes on adding month-wise till they are in the same year and then start again when a new year starts.

Thanks & Regards
Apr 2 '08 #20
Delerna
1,134 Expert 1GB
DSum....Thats an excel function isn't it?
I don't know, perhaps.

Here is the data from my mock up. As you can see it works.
Not sure what is going wrong with yours

Company Product Dte Qty YTD
Company2 Prod2 2/1/2005 10 10
Company2 Prod2 3/1/2005 20 30
Company2 Prod2 4/1/2005 30 50
Company2 Prod2 5/1/2005 40 70
Company2 Prod2 6/1/2005 50 90
Company2 Prod2 7/1/2005 60 110
Company2 Prod2 8/1/2005 70 130
Company2 Prod2 9/1/2005 80 150
Company2 Prod2 10/1/2005 90 170
Company2 Prod2 11/1/2005 100 190
Company2 Prod2 12/1/2005 110 210
Apr 2 '08 #21
Delerna
1,134 Expert 1GB
Hang on, no its not, My mistake and my appologies I must be getting tired.
I will have another quick look
Apr 2 '08 #22
Oh! then you must take rest.

And Dsum is a function in MS access like Sumif in excel.But again I am very bad with the syntaxes in sql so i have yet to crack it but I believe it works well for running totals ,I just searched it on Google .Here is the link

http://support.microsoft.com/kb/290136
Apr 2 '08 #23
Delerna
1,134 Expert 1GB
OK, wow my mind went way out on a tangent with that one, Sorry about that.
try this. Its a single query this time
Expand|Select|Wrap|Line Numbers
  1. SELECT qryDatedDataForTheCompany.Dte, 
  2.       qryDatedDataForTheCompany.Company,    
  3.       qryDatedDataForTheCompany.Product, 
  4.       qryDatedDataForTheCompany.Qty,
  5.       (    select sum(Qty) as YTD 
  6.            from qryDatedDataForTheCompany a 
  7.            where Year(a.Dte)=year(qryDatedDataForTheCompany.Dte) 
  8.              and qryDatedDataForTheCompany.company=a.Company 
  9.              and a.Product=qryDatedDataForTheCompany.Product 
  10.              and qryDatedDataForTheCompany.Dte>=a.Dte
  11.        ) AS YTD
  12. FROM qryDatedDataForTheCompany;
  13.  
I hope it won't be too slow
Apr 2 '08 #24
Delerna
1,134 Expert 1GB
And here is a query that produces the same reult using DSum
Thanks for that, it was something I was unaware of

Expand|Select|Wrap|Line Numbers
  1. SELECT qryDatedDataForTheCompany.Company, qryDatedDataForTheCompany.Product, qryDatedDataForTheCompany.Dte, First(qryDatedDataForTheCompany.Qty) AS FirstOfQty, DSum("Qty","qryDatedDataForTheCompany","DatePart('m',[Dte])<=" & Month([Dte]) & " And DatePart('yyyy',[Dte])=" & Year([Dte]) & " And [company]='" & [company] & "' And [product]='" & [product] & "'") AS MTD
  2. FROM qryDatedDataForTheCompany
  3. GROUP BY qryDatedDataForTheCompany.Company, qryDatedDataForTheCompany.Product, qryDatedDataForTheCompany.Dte, DSum("Qty","qryDatedDataForTheCompany","DatePart('m',[Dte])<=" & Month([Dte]) & " And DatePart('yyyy',[Dte])=" & Year([Dte]) & " And [company]='" & [company] & "' And [product]='" & [product] & "'")
  4. ORDER BY qryDatedDataForTheCompany.Company, qryDatedDataForTheCompany.Product, qryDatedDataForTheCompany.Dte;
  5.  
I must say though that, at least on my computer, DSum is much slower than the previous query.
Apr 2 '08 #25
Delerna
1,134 Expert 1GB
This version of DSum is much faster but still not quite as fast
I had DSum as 'group by' instead of 'expression'.
Expand|Select|Wrap|Line Numbers
  1. SELECT qryDatedDataForTheCompany.Company, qryDatedDataForTheCompany.Product, qryDatedDataForTheCompany.Dte, First(qryDatedDataForTheCompany.Qty) AS FirstOfQty, DSum("Qty","qryDatedDataForTheCompany","DatePart('m',[Dte])<=" & Month([Dte]) & " And DatePart('yyyy',[Dte])=" & Year([Dte]) & " And [company]='" & [company] & "' And [product]='" & [product] & "'") AS MTD
  2. FROM qryDatedDataForTheCompany
  3. GROUP BY qryDatedDataForTheCompany.Company, qryDatedDataForTheCompany.Product, qryDatedDataForTheCompany.Dte
  4. ORDER BY qryDatedDataForTheCompany.Company, qryDatedDataForTheCompany.Product, qryDatedDataForTheCompany.Dte;
  5.  
  6.  
Apr 2 '08 #26
I don't know wats happening but when I run the final query,Its showing me this message "system resource exceeded".My system's RAM is already 1GB .Can u suggest what should I do.

Thanks&Regards
Apr 3 '08 #27
Delerna
1,134 Expert 1GB
A couple of things
1) Take a copy of the database and "compact and repair" it.
2) If that fails, try rebooting your computer.
3) If that fails, try creating a new fresh database and import the tables,queries etc from the database you were working with

good luck

by the way, did any of the queries work for you?
Apr 3 '08 #28
Thank you so much the above queries are working fine but not the last one.I have already Run the compact and repair command and also rebooted my system but its not working.

Should I export all the tables and queries to a new Database?

Thanks&Regards
Apr 3 '08 #29
Delerna
1,134 Expert 1GB
It couldn't hurt and might even help
Apr 3 '08 #30
What I did is ,I imported only few queries and formed a new database but sadly its still not workin.
Apr 3 '08 #31
Delerna
1,134 Expert 1GB
On the second query. The one with the DSum in group by. What happens if you design it and change it from 'Group By' to Expression'.

Thats what I did to get the third query that I pasted here. Maybe something is going wrong with all the copying and pasting from my machine through this site and on to your machine. I am saying that with a puzzled expression on my face.
Apr 3 '08 #32
On the second query. The one with the DSum in group by. What happens if you design it and change it from 'Group By' to Expression'.

Thats what I did to get the third query that I pasted here. Maybe something is going wrong with all the copying and pasting from my machine through this site and on to your machine. I am saying that with a puzzled expression on my face.
I will check it again

Thanks&Regards
Apr 4 '08 #33

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
0
by: Kasp | last post by:
Hi there, I am trying to make an OLAP cube on a table having two columns (datetime, Number_of_times_an_event_occured). My dimension is time and I want to measure the Min and Max times an event...
1
by: Joe Bongiardina | last post by:
What does the message "calculating...." mean in the lower left status area of a form? I have a form with no calculated, concatenated or lookup fields, yet it displays this msg. The form takes...
1
by: jlm | last post by:
I have a form which feeds table (TblEmpLeave) of Employee Leave Time (time taken off for Administrative, Annual, Sick, Compensation leave). I have EmpID, LeaveDate, LeaveType, LeaveHours fields on...
0
by: robin9876 | last post by:
In an Access 2000 database on some forms 'Calculating...' is continuously displayed in the status bar window and the text of the control is automatically selected. The only workaround is switching...
5
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int...
25
by: Umesh | last post by:
i want to calculate the time required to execute a program. Also i want to calcute the time remaining for the execution of the program. how can i do that? pl mention some good websites for...
1
by: laredotornado | last post by:
Hi, Can anyone recommend a free script for calculating UPS shipping? I am familiar with the script written in 2000 by Jason Costomiris, but UPS is using an XML interface and I wondered if...
4
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Say I have a class like, class Sample { public decimal first = 10; public decimal second = 20; } I have initialized it
4
by: sumit kale | last post by:
Hi, Can somebody help me resolve my problem ? I am getting error when calculating total using unbound textfiled in subform. I have a main form called purchase_register_master and a subform...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.