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
 
Unread Codewalkers Forums Sponsor:
  #1  
Old August 8th, 2002, 04: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, 03: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: 3
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, 01: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, 02: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: 3
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, 02: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: 3
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, 02: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: 3
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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek