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
3 1274
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: - DECLARE @baseTable TABLE([month] int, [week] int, price int)
-
DECLARE @outTable TABLE([month] int, [cWeek] int, [pWeek] int, price int)
-
DECLARE @cMonth INT
-
DECLARE @cWeek INT
-
DECLARE @cPrice INT
-
DECLARE @pMonth INT
-
DECLARE @pWeek INT
-
DECLARE @pPrice INT
-
-
-
SET @cMonth = 0
-
SET @cWeek = 0
-
SET @cPrice = 0
-
SET @pMonth = 0
-
SET @pWeek = 0
-
SET @pPrice = 0
-
-
INSERT iNTO @baseTable VALUES(1,1,10)
-
INSERT iNTO @baseTable VALUES(1,1,12)
-
INSERT iNTO @baseTable VALUES(1,2,16)
-
INSERT iNTO @baseTable VALUES(1,2,20)
-
INSERT iNTO @baseTable VALUES(1,3,12)
-
INSERT iNTO @baseTable VALUES(1,4,16)
-
-
DECLARE curT CURSOR
-
FOR
-
SELECT [month],[week],avg([price])[aPrice] FROM @baseTable
-
GROUP BY [month],[week]
-
ORDER BY [month],[week]
-
-
OPEN curT
-
FETCH NEXT FROM curT INTO
-
@cMonth,@cWeek,@cPrice
-
-
WHILE (@@FETCH_STATUS = 0)
-
BEGIN
-
if(NOT @pMonth = 0 AND NOT @pWeek = 0)
-
BEGIN
-
if((@pWeek+1) = @cWeek AND (@cWeek % 2) = 0 AND @pMonth =@cMonth)
-
BEGIN
-
INSERT iNTO @outTable VALUES( @pMonth,@cWeek,@pWeek,@cPrice-@pPrice)
-
END
-
END
-
SET @pMonth=@cMonth
-
SET @pWeek =@cWeek
-
SET @pPrice = @cPrice
-
-
FETCH NEXT FROM curT INTO
-
@cMonth,@cWeek,@cPrice
-
END
-
-
-
SELECT [month],'ResW'+cast(pWeek as varchar(2))+'W'+cast(cWeek as varchar(2)),price FROM @outTable
-
-
CLOSE curT
-
DEALLOCATE curT
The pair table method: - DECLARE @baseTable TABLE([month] int, [week] int, price int)
-
DECLARE @pairTable TABLE(intervalID int,start int, [stop] int)
-
-
INSERT iNTO @baseTable VALUES(1,1,10)
-
INSERT iNTO @baseTable VALUES(1,1,12)
-
INSERT iNTO @baseTable VALUES(1,2,16)
-
INSERT iNTO @baseTable VALUES(1,2,20)
-
INSERT iNTO @baseTable VALUES(1,3,12)
-
INSERT iNTO @baseTable VALUES(1,4,16)
-
-
INSERT INTO @pairTable values (10,1,2)
-
INSERT INTO @pairTable values (20,3,4)
-
INSERT INTO @pairTable values (30,5,6)
-
INSERT INTO @pairTable values (40,7,8)
-
-
SELECT B1.[month],'ResW'+cast([stop] as varchar(2))+'W'+cast([Start] as varchar(2)),B2.aprice-B1.aprice FROM @pairTable I
-
INNER JOIN
-
(SELECT [month],[week],avg([price])[aPrice] FROM @baseTable
-
GROUP BY [month],[week]) B1 ON I.start = B1.week
-
INNER JOIN
-
(SELECT [month],[week],avg([price])[aPrice] FROM @baseTable
-
GROUP BY [month],[week]) B2 ON I.[stop] = B2.week
-
-
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.
I developed my solution based on cursors.
But I have a concern if the cursor has any disadvantage or not.
thanks
Depending on how many records you have, cursors can be very slow.
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
by: Rajkumar78 |
last post by:
Hi
Can any one help me regarding the title
Regards
Rajkumar
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |