Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 6th, 2004, 06:49 AM
Authorized User
 
Join Date: Jun 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to get pervious record?

Hi,

   i have boiled my head morning to evening but could not make any solution nor logic....

    Poblem is

   I have one table with following columns

 VehicleName /* Simple Vehicle Name */
 FuelQty /* Filled Fuel Qty */
 MeterReading /* Meter Reading at Filling time */
 CDate /* Current Date of filling vehicle */

   Now i need following results inculding above columns in report...

   KiloMeters /* formula is KiloMeters =(Current MeterReading) - (Last MeterReading ) */
   AVG /* Formula is AVG =(KiloMeters % FuelQty )

....

   Hope you have gotten problem...

thanks




Stay Beautiful,
Abdul Salam
__________________
Stay Beautiful,
Abdul Salam
 
Old July 6th, 2004, 11:13 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

To get "Last MeterReading", you will need to construct a subquery that ties into the main query to get the "previous" record.
 
Old July 6th, 2004, 01:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Abdul,

I am trying to give you a step-by-step logic here. Not sure if this is what you are trying to achieve.

Code:
Declare @CurrentMeterReading INT
SET ROWCOUNT 1
select @CurrentMeterReading = MeterReading from YOURTABLE where VehicleName = "SOMETHING" order by CDATE desc

Declare @LastMeterReading INT
Declare @FuelQty INT
SET ROWCOUNT 1
select @LastMeterReading = MeterReading, @FuelQty=FuelQty from YOURTABLE where CDate < (Select Max(CDate) from YOURTABLE) order by CDATE desc

Declare @KiloMeters INT, @Avg INT
Select @KiloMeters = @CurrentMeterReading - @LastMeterReading
Select @Avg = @KiloMeters / @FuelQty

Select Kilometers=@KiloMeters, Average=@Avg
SET ROWCOUNT 0
You can test this out to see if this output is what you are looking for. If so you can simplify this logic as needed.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old July 8th, 2004, 02:23 AM
Authorized User
 
Join Date: Jun 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Vijay.
   But i have done it throgh a long and tempraroy way ... you may say with a trick( with creating new 3 tables
    and 4 views one by one .....very headech ) okay

   Your given code can hlep me .. but how i can loop from BOF to EOF ....
   you may check images on http://www.geocities.com/askhamosh/prob.htm
   hope you will come with greate help ...

 thanks




Stay Beautiful,
Abdul Salam
 
Old July 8th, 2004, 02:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi there,

You can put this code under a stored procedure and execute that within your ASP pages if any.

Code:
Create procedure MYPROC (@VehicleName varchar(100)=NULL)
as
Declare @CurrentMeterReading INT
SET ROWCOUNT 1
select @CurrentMeterReading = MeterReading from YOURTABLE where VehicleName = @VehicleName order by CDATE desc

Declare @LastMeterReading INT
Declare @FuelQty INT
SET ROWCOUNT 1
select @LastMeterReading = MeterReading, @FuelQty=FuelQty from YOURTABLE where VehicleName = @VehicleName and CDate < (Select Max(CDate) from YOURTABLE) order by CDATE desc

Declare @KiloMeters INT, @Avg INT
Select @KiloMeters = @CurrentMeterReading - @LastMeterReading
Select @Avg = @KiloMeters / @FuelQty

Select Kilometers=@KiloMeters, Average=@Avg
SET ROWCOUNT 0
Return
Code:
Execute MYPROC "VehicleName"
I think this is what you are looking for.
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old July 9th, 2004, 02:45 AM
Authorized User
 
Join Date: Jun 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Sorry this not that mean ...

   i'm going on leave see you later

Stay Beautiful,
Abdul Salam
 
Old July 19th, 2004, 05:01 AM
Authorized User
 
Join Date: Jun 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Vijay ... i have solved by your code with some change....



Stay Beautiful,
Abdul Salam





Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete a record row, not just the record. Coby Access VBA 1 April 30th, 2007 06:29 AM
how to add new record as first record in dataset [email protected] ASP.NET 1.0 and 1.1 Professional 4 April 21st, 2006 05:23 AM
Record locking - user needs the next queued record cbtoolkit SQL Server 2000 0 December 6th, 2004 08:29 AM
Sub record not associating with Main record Ron V Access 1 August 31st, 2004 09:21 AM
record logs rajanikrishna Pro VB 6 0 April 2nd, 2004 03:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.