Question

Good Afternoon Everyone,

Utiliisng the code located below, I create a view that will be used to calculate the average time per mile.

CREATE VIEW AverageRunTime AS
SELECT UserDetails.username AS username,
CardiovascularRecords.distance_in_miles AS distance,
CardiovascularRecords.start_time AS starttime,
CardiovascularRecords.end_time AS endtime,
sum(strftime('%s', end_time) - strftime('%s', start_time) / distance_in_miles) AS averagespeedpermile
FROM UserDetails INNER JOIN CardiovascularRecords
ON Userdetails.username=CardiovascularRecords.username

The Issue I am encountering pretains to the distance variable taken into account, with the REAL datatype being utilised I have several decimal values in place such as a range from 0.1 to 1.

When performing the above sum the view will only present data relating to whole numbers ignoring any record containing decimal values entirely.

Any Assistance with this issue would be most welcome, the code relating to my schema is also included below to allow others to replicate my work.

CREATE TABLE "CardiovascularRecords" ("record_ID" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE CHECK (record_ID>0) , "distance_in_miles" REAL NOT NULL CHECK (distance_in_miles>0) , "username" TEXT NOT NULL , "date" DATETIME DEFAULT CURRENT_DATE, "notes" TEXT(50), "start_time" TEXT, "end_time" TEXT, FOREIGN KEY(distance_in_miles) REFERENCES DistanceinMiles(distance_in_miles) ON DELETE CASCADE ON UPDATE CASCADE,FOREIGN KEY(username) REFERENCES UserDetails(username) ON DELETE CASCADE ON UPDATE CASCADE)

CREATE TABLE "UserDetails" ("username" TEXT PRIMARY KEY  NOT NULL  UNIQUE  CHECK (length(username)>0), "password" TEXT NOT NULL  CHECK (length(password)>3), "email_address" TEXT NOT NULL  UNIQUE CHECK (length(email_address)>3) , "weight_in_pounds" REAL NOT NULL CHECK(weight_in_pounds>0) CHECK (length(weight_in_pounds)>0), "height_in_inches" REAL NOT NULL CHECK(height_in_inches>0) CHECK (length(height_in_inches)>0), "age" INTEGER CHECK(age>0), WITHOUT ROWID)

Thank you in advance.

Was it helpful?

Solution

SQLite uses dynamic typing, so it is possible to insert strings into the distance_in_miles column.

The check distance_in_miles>0 does not help because any string compares larger than any number.

Fix your program to store the values as numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top