Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Tricky SQL query (any sql experts here?)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
petrjanda
Veteran
Veteran


Joined: 05 Sep 2003
Posts: 1557
Location: Brno, Czech Republic

PostPosted: Fri May 04, 2012 3:27 am    Post subject: Tricky SQL query (any sql experts here?) Reply with quote

Ok here is the data you KNOW - Aircraft ID, and a Timestamp before you run the query.

You have a table that tracks every change done to a Part (instead of UPDATE, it does INSERT INTO on every update to hold a complete history of the PartID)

Code:

RevisionID, PartID, AircraftID, RecordCreatedAt
1, 5, 5, 2012-04-01 12:00:00 - partID 5 created on aircraft 5
2, 5, 3, 2012-05-01 12:00:00 - partID 5 moved to aircraft 3
3, 5, 3, 2012-05-01 12:30:00 - partID 5 update - see the time change
4, 5, 4, 2012-05-03 12:00:00 - partID 5 moved to aircraft 4
5, 6, 3, 2012-05-01 12:00:00 - partID 6 created on aircraft 3


RevisionID is autoincrement.

And this is the goal:

Get all the parts present on an AircraftID 3 at the given time 2012-05-02 12:00:00. I know I can do this in the code, but for the sake of performance I want to do this at the SQL level.

Using the data above, the final SELECT should return:

Code:

RevisionID, PartID, AircraftID, RecordCreatedAt
3, 5, 3, 2012-05-01 12:30:00 - notice the returned row shows the most up-to-date version, the previous version made at 12:00:00 is ignored
5, 6, 3, 2012-05-01 12:00:00


As those 2 parts where present on aircraftID 3 at the given time.

The database is sqlite3.

Do we have any SQL experts that can help me out on this?
_________________
There is, a not-born, a not-become, a not-made, a not-compounded. If that unborn, not-become, not-made, not-compounded were not, there would be no escape from this here that is born, become, made and compounded. - Gautama Siddharta
Back to top
View user's profile Send private message
TheLexx
n00b
n00b


Joined: 04 Dec 2005
Posts: 26
Location: Austin Tx

PostPosted: Fri May 04, 2012 9:06 pm    Post subject: Reply with quote

It looks like you just need to use a query for date ranges.

http://www.databasejournal.com/features/mssql/article.php/10894_2209321_2/Working-with-SQL-Server-DateTime-Variables-Part-Three---Searching-for-Particular-Date-Values-and-Ranges.htm
Back to top
View user's profile Send private message
GoldenMouse
Tux's lil' helper
Tux's lil' helper


Joined: 29 Sep 2005
Posts: 112

PostPosted: Sat May 05, 2012 6:23 am    Post subject: Reply with quote

It's too late to come up with a query for you; I tried, but my brain is too fried to pull it together. I'll try to come back and answer this better, but it seems as if you need a subquery or two in there.

Start with a query that returns the most recent record before your stated time for a given part and a given aircraft:

SELECT *
FROM `TheTable`
WHERE `PartID` = ?
AND `AircraftID` = ?
AND `RecordCreatedAt` < ?
ORDER BY `RecordCreatedAt` DESC
LIMIT 1

You then need to wrap this such that it runs once for each PartID on that aircraft. I know it can be done, but it's past 2am right now. :(
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum