Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old August 8th, 2002, 03:20 PM
lambda lambda is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 2 lambda User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Is there adjacent row operation?

I Need help. Problem is like this:

BASE TABLE (say the historical price of product X)

Year Price
---- ------
1960 5
1962 7
1967 15
1970 20
1980 30
1986 44
1992 50
2000 60


QUERY RESULT (with filter say Year Between 1967 And 1986)

Year1 Year2 Price1 Price2 PriceInc
----- ----- ------ ------- --------
1967 1970 15 20 5
1970 1980 20 30 10
1980 1986 30 44 14

the last column is a calculated field (Price2-Price1).

can this query be produced by SQL statement?

Reply With Quote
  #2  
Old August 9th, 2002, 02:12 AM
sager sager is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 45 sager User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Is there adjacent row operation?

I'm not sure if it will work, but I think it might:

select Year1, Year2, Price1, Price2, (Price2 - Price1) as PriceInc ....

Not sure though, never tried it, but it's worth a shot;)

Reply With Quote
  #3  
Old August 9th, 2002, 12:38 PM
lambda lambda is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 2 lambda User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Is there adjacent row operation?

thanks, ShaneO and sager, for your response. i really appreciated your time spent on this.

However i must note that the peculiarity of this problem is the fact that the query result is always consecutive rows. And note that for a given row, the first column value (Year1) is always the same as the second column value (Year2) of the PRECEDING row. This is also true for the Price columns.

I really need help on this one. Thanks.

lambda

Reply With Quote
  #4  
Old August 9th, 2002, 01:19 PM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: Is there adjacent row operation?

Here is a hack:
php Code:
Original - php Code
  1. <?
  2. $query1 = "select year, price from table_name where year>=1967 and year<1986 order by year asc";
  3. $result1 = mysql_query($query1);
  4. while($row1 = mysql_fetch_array($result1)){
  5.   if($row1['year'] != '1986'){
  6.     $query2 = "select year, price from table_name where year>".$row1['year']."order by year asc limit 1";
  7.     $result2 = mysql_query($query2);
  8.     $row2 = mysql_fetch_array($result2);
  9.   }
  10.   echo $row1['year']." ".$row2['year']." ".$row1['price']." ".$row2['price']." ".($row2['price'] - $row1['price'])."<br>";
  11. }
  12. ?>

Reply With Quote
  #5  
Old August 9th, 2002, 01:22 PM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: Is there adjacent row operation?

The 2nd year comparison in $query1 should be <=

Reply With Quote
  #6  
Old August 9th, 2002, 01:43 PM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: Is there adjacent row operation?

hang-on ... wait a minute ... stop.
Sorry 'bout that but the mountain dew is wearing off and its past my bedtime. This is it:
php Code:
Original - php Code
  1. <?
  2. $query1 = "select year, price from table_name where year>=1967 and year<1986 order by year asc";
  3. $result1 = mysql_query($query1);
  4. while($row1 = mysql_fetch_array($result1)){
  5.   $query2 = "select year, price from table_name where year>".$row1['year']."order by year asc limit 1";
  6.   $result2 = mysql_query($query2);
  7.   $row2 = mysql_fetch_array($result2);
  8.  
  9.   echo $row1['year']." ".$row2['year']." ".$row1['price']." ".$row2['price']." ".($row2['price'] - $row1['price'])."<br>";
  10. }
  11. ?>

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > Is there adjacent row operation?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway