Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 please solve my query

Author  Topic 

dudekhan1
Starting Member

1 Post

Posted - 2005-04-14 : 21:07:10
Please provide a sql query specific for MS Access Database
Following is the refueling information in 2 tables

Table: Refuel
CarID<----->OdometerReading<----->LitersGas
3<----->18791<----->23.37
3<----->19065<----->22.74
1<----->1245<----->55.25
1<----->1457<----->16.96
1<----->1872<----->34.23
1<----->2193<----->25
1<----->2448<----->20.4
1<----->2508<----->5.52
1<----->2663<----->14.15
2<----->113731<----->29.92
2<----->114130<----->36.7
2<----->114535<----->36.98
2<----->114943<----->41.41
3<----->18091<----->30.25
3<----->18291<----->16.6
3<----->18506<----->18.27
3<----->19364<----->28.40
3<----->19569<----->18.65
1<----->3075<----->41.82
1<----->3387<----->27.77
2<----->112145<----->36
2<----->112972<----->34.24
2<----->113357<----->30.03

Table: CAR
CarID<----->Color<----->Make<----->Model
1<----->Blue<----->Ford<----->Taurus
2<----->Green<----->VW<----->Jetta
3<----->Blue<----->Acura<----->1.7EL

Description of columns:
OdometerReading – km reading on the odometer when the car is refueled
LitresGas – the amount of gas put into the car when it is refueled
CarID – Unique Identifier column per car


Primary Key on Refuel is CarID and OdometerReading
Primary key on CAR is CarID

Relationship between OdometerReading.CarID and CAR.CarID

Take it as a given that every time a car is refueled, the tank is filled.
To determine gas consumption (l/100km), take the LitersGas, divide it by kilometers traveled, and multiply by 100.
For example:
If the car takes 45 liters of gas to refuel, and had traveled 502 km since the previous time it was refueled, the fuel consumption would be: 45 liters / 502 km * 100 = 8.96 l/100km

Can you please give me a query to generate the following table?

OdometerReading<----->Consumption<----->Color<----->Make
1245<----->n/a<----->Blue<----->Ford
1457<----->8<----->Blue<----->Ford
1872<----->8.25<----->Blue<----->Ford
2193<----->7.79<----->Blue<----->Ford
2448<----->8<----->Blue<----->Ford
2508<----->9.2<----->Blue<----->Ford
2663<----->9.13<----->Blue<----->Ford
3075<----->10.15<----->Blue<----->Ford
3387<----->8.9<----->Blue<----->Ford
112145<----->n/a<----->Green<----->VW
112972<----->4.14<----->Green<----->VW
113357<----->7.8<----->Green<----->VW
113731<----->8<----->Green<----->VW
114130<----->9.2<----->Green<----->VW
114535<----->9.13<----->Green<----->VW
114943<----->10.15<----->Green<----->VW
18091<----->n/a<----->Blue<----->Acura
18291<----->8.3<----->Blue<----->Acura
18506<----->8.5<----->Blue<----->Acura
18791<----->8.2<----->Blue<----->Acura
19065<----->8.3<----->Blue<----->Acura
19364<----->9.5<----->Blue<----->Acura
19569<----->9.1<----->Blue<----->Acura

<edit> Moved to MS-Access Forum </edit>

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2005-04-15 : 00:23:28
what other school assignments can we complete for you?



:)



-ec
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-04-15 : 01:28:43
Here's an example of some of the techniques you could use. Of course, they might not be exactly, or even remotely close to what you need.


DROP PROCEDURE weirddatestuff
GO

CREATE PROCEDURE weirddatestuff

--SET DATEFORMAT YMD EXEC weirddatestuff '2001/04/01','2001/01/01',15

@startdate DATETIME,
@datefrom DATETIME,
@months INT

AS

SET DATEFORMAT YMD

SELECT @startdate = CAST(DATEPART(YY,@startdate) AS VARCHAR(4)) + '/' + CAST(DATEPART(MM,@startdate) AS VARCHAR(2)) + '/01'

--Create the table to hold all this crap.
CREATE TABLE #weirddatepopulation(
IhatecolumnscalledID INT IDENTITY(1,1),
PackID VARCHAR(4), --Why is this varchar(4)? It should be an INT.
DateFrom DATETIME,
DateTo DATETIME)

--Hold your PackID series.
DECLARE @packer TABLE(PackID INT) --Love these names.

INSERT @packer(PackID)
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4

--Populate a date table with the appropriate dates.
DECLARE @datestuff TABLE(datestuffed DATETIME)

DECLARE
@min INT,
@startdatemid DATETIME,
@startdateend DATETIME,
@minplusone INT

SELECT @min = 0

WHILE @min < @months
BEGIN

SELECT @minplusone = @min + 1

SELECT
@startdatemid = DATEADD(DD,14,DATEADD(MM,@min,@startdate)),
@startdateend = DATEADD(DD,-1,DATEADD(MM,@minplusone,@startdate))

INSERT @datestuff(datestuffed)
SELECT @startdatemid UNION ALL
SELECT @startdateend

SELECT @min = @min + 1
END

INSERT #weirddatepopulation(
PackID,
DateFrom,
DateTo)

SELECT
p.PackID,
@datefrom,
ds.datestuffed
FROM
@datestuff ds
CROSS JOIN @packer p
ORDER BY
ds.datestuffed,
p.PackID

SELECT
IhatecolumnscalledID,
PackID,
DateFrom,
DateTo
FROM
#weirddatepopulation
GO



MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-04-15 : 01:30:12
After a little more thought, I wouldn't use any of that for your particular problem.



MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -