UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Calculating a lagged variable    
 
   
tom_mustillo
post Nov 14 2004, 10:50 PM
Post #1

New Member
Posts: 14



I have a table:
country election party vote
usa 1/1/2000 dem 48
usa 1/1/2004 dem 49
usa 1/1/2008 dem 51
usa 1/1/2012 dem 50

I want to create a new column, called, votelagged, which calculates the difference between one vote and the previous vote so that it would read:
null
1
2
-1

Various statistical packages do this as a built-in feature, but I can't figure out how to do it manually. Thanks for the consideration.
Tom
Go to the top of the page
 
+
wen
post Nov 15 2004, 08:30 AM
Post #2

UtterAccess Addict
Posts: 153
From: The Netherlands



Hi,

You need to create a self-outer join query. Then connect the current election with the previous election. See the code below. Keep in mind that this SQL statement cannot be shown in design view, only in SQL view, because of the joinexpression.

HTH, Wen

CODE
SELECT tblVotes.Country, tblVotes.Election, tblVotes.Party, [tblVotes_1].[Vote]-[tblVotes].[Vote] AS VoteDiff
FROM tblVotes LEFT JOIN tblVotes AS tblVotes_1 ON (tblVotes.Election-4) = tblVotes_1.Election;


Country    Election    Party    VoteDiff
usa    2000    dem    
usa    2004    dem    -1
usa    2008    dem    -2
usa    2012    dem    1
Go to the top of the page
 
+
tom_mustillo
post Nov 15 2004, 09:44 AM
Post #3

New Member
Posts: 14



Great. Forgive me, I should have included the full problem--if there are dozens of countries, hundreds of elections and thousands of parties, such that the increment is not always 4 years between elections and matches must be made on country and party. The table looks like this (and the new variable is last)
Country Election Party Vote VoteDiff
usa 2000 dem 48 null
usa 2000 rep 47 null
usa 2004 dem 49 1
usa 2004 rep 51 4
can 2000 lib 43 null
can 2000 con 41 null
can 2002 lib 42 -1
can 2002 con 38 -3
Thanks again,
Tom
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 23rd May 2013 - 11:34 AM