473,394 Members | 2,160 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,394 software developers and data experts.

How to take averages of data and place them into a temp table?

Hi,

I have a table MyTable with the followind Data

Month Week Price
1 1 10
1 1 12
1 2 16
1 2 20
1 3 12
1 4 16

I need to apply this formula (Avg price of W2 - Avg price of W1) and put the result in a temp table and do the same for W4 and W3

So the temp table will be of the following form
Month Week Price
1 ResW1W2 7
1 ResW3W4 4

Any suggestion?

Thanks
Feb 2 '11 #1
3 1274
Jerry Winston
145 Expert 100+
Ok. There are a few ways to do this. It sounds like you want the difference of two averages. Because you omitted the comparison of weeks 2 and 3, I think you want the average of an arbitrary pair of weeks NOT the average over a two week interval. I'll give you two solutions. One iterative method using a CURSOR and another using a predefined pair Table.

The cursor method:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @baseTable TABLE([month] int, [week] int, price int)
  2. DECLARE @outTable TABLE([month] int, [cWeek] int, [pWeek] int, price int)
  3. DECLARE @cMonth INT
  4. DECLARE @cWeek INT
  5. DECLARE @cPrice INT
  6. DECLARE @pMonth INT
  7. DECLARE @pWeek INT
  8. DECLARE @pPrice INT
  9.  
  10.  
  11. SET @cMonth = 0
  12. SET @cWeek = 0
  13. SET @cPrice = 0
  14. SET @pMonth = 0
  15. SET @pWeek = 0
  16. SET @pPrice = 0
  17.  
  18. INSERT iNTO @baseTable VALUES(1,1,10)
  19. INSERT iNTO @baseTable VALUES(1,1,12)
  20. INSERT iNTO @baseTable VALUES(1,2,16)
  21. INSERT iNTO @baseTable VALUES(1,2,20)
  22. INSERT iNTO @baseTable VALUES(1,3,12)
  23. INSERT iNTO @baseTable VALUES(1,4,16)
  24.  
  25. DECLARE curT CURSOR 
  26. FOR
  27. SELECT [month],[week],avg([price])[aPrice] FROM @baseTable
  28. GROUP BY [month],[week]
  29. ORDER BY [month],[week]
  30.  
  31. OPEN curT
  32. FETCH NEXT FROM curT INTO
  33. @cMonth,@cWeek,@cPrice
  34.  
  35. WHILE (@@FETCH_STATUS = 0)
  36. BEGIN
  37.     if(NOT @pMonth = 0 AND NOT @pWeek = 0)
  38.     BEGIN
  39.         if((@pWeek+1) = @cWeek AND (@cWeek % 2) = 0 AND @pMonth =@cMonth)
  40.         BEGIN
  41.             INSERT iNTO @outTable VALUES( @pMonth,@cWeek,@pWeek,@cPrice-@pPrice)
  42.         END
  43.     END
  44. SET @pMonth=@cMonth
  45. SET @pWeek =@cWeek
  46. SET @pPrice = @cPrice
  47.  
  48. FETCH NEXT FROM curT INTO
  49. @cMonth,@cWeek,@cPrice
  50. END
  51.  
  52.  
  53. SELECT [month],'ResW'+cast(pWeek as varchar(2))+'W'+cast(cWeek as varchar(2)),price FROM @outTable
  54.  
  55. CLOSE curT
  56. DEALLOCATE curT
The pair table method:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @baseTable TABLE([month] int, [week] int, price int)
  2. DECLARE @pairTable TABLE(intervalID int,start int, [stop] int)
  3.  
  4. INSERT iNTO @baseTable VALUES(1,1,10)
  5. INSERT iNTO @baseTable VALUES(1,1,12)
  6. INSERT iNTO @baseTable VALUES(1,2,16)
  7. INSERT iNTO @baseTable VALUES(1,2,20)
  8. INSERT iNTO @baseTable VALUES(1,3,12)
  9. INSERT iNTO @baseTable VALUES(1,4,16)
  10.  
  11. INSERT INTO @pairTable values (10,1,2)
  12. INSERT INTO @pairTable values (20,3,4)
  13. INSERT INTO @pairTable values (30,5,6)
  14. INSERT INTO @pairTable values (40,7,8)
  15.  
  16. SELECT B1.[month],'ResW'+cast([stop] as varchar(2))+'W'+cast([Start] as varchar(2)),B2.aprice-B1.aprice FROM @pairTable I
  17. INNER JOIN
  18. (SELECT [month],[week],avg([price])[aPrice] FROM @baseTable
  19. GROUP BY [month],[week]) B1 ON I.start = B1.week
  20. INNER JOIN
  21. (SELECT [month],[week],avg([price])[aPrice] FROM @baseTable
  22. GROUP BY [month],[week]) B2 ON I.[stop] = B2.week
  23.  
  24.  
So, why two code examples? If you were looking for an interval-based solution, I think you could derive your own solution from the Cursor based solution. Alternatively, if you were looking for a comparison of user-defined pairs, the paired-value table might be more useful.
Feb 2 '11 #2
I developed my solution based on cursors.

But I have a concern if the cursor has any disadvantage or not.

thanks
Feb 3 '11 #3
Rabbit
12,516 Expert Mod 8TB
Depending on how many records you have, cursors can be very slow.
Feb 3 '11 #4

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

Similar topics

1
by: NotGiven | last post by:
Please recommend books or web sites to learn more about how to do complex queries using MySQL. A potential client needs lots of summarizations and sub-queries. Many thanks.
6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
1
by: Beachvolleyballer | last post by:
hi there anyone had an idea to join following 2 queries to 1???? ----- QUERY 1 --------------------------------------------- SELECT TMS_CaseF_2.Name AS TCDomain_0, TMS_CaseF_3.Name AS...
4
by: R | last post by:
Hi When an Access query is run against an ODBC table, is it possible in any way to tell what is passed to the server? I have a remote Sybase database linked into Access and the bandwidth is...
8
by: Steve Jorgensen | last post by:
Mailing List management is a good example of a case where my conundrum arises. Say there is a m-m relationship between parties and groups - anyone can be a member of any combintation of groups. ...
0
by: sales | last post by:
I am glad to present to the community of this group the new version of our component intended for visual building of SQL queries via an intuitive interface - Active Query Builder...
13
by: JayCallas | last post by:
I know this question has been asked. And the usual answer is don't use cursors or any other looping method. Instead, try to find a solution that uses set-based queries. But this brings up...
0
by: Christoph Haas | last post by:
Hi, list... I have written an application in Perl some time ago (I was young and needed the money) that parses multiple large text files containing nested data structures and allows the user to...
1
by: phlype.johnson | last post by:
Suppose we have to design a database for a recruitment agency. There will be a table "candidates" with fields "candidateid","last name","first name" ; the languages mastered by a candidate as well...
1
by: Rajkumar78 | last post by:
Hi Can any one help me regarding the title Regards Rajkumar
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.