I've been asked to look into making some changes to reduce redundant work performed by some of our applications. Part of my task is to produce a report that will help quantify the potential benefits.
Basically we have programs that create "launches", then other programs that look for these newly created launch records and start doing some work. We'd like to limit the number of launches that are created, by preventing new launches being created with the same attributes as a recent launch.
I've actually created the application logic, but before I can implement it I have to prove it's theoretical value with the aforementioned report. What I'm having a bit of trouble doing is coming up with the SQL for the report.
Note that our database environment here is MS Sql Server 2005.
So I can look at launches from the past week with this pseudo-SQL:
Now with the information about an individual launch in hand, it's easy enough to write a query to figure out if we have any recent launches (in this case, 24 hours) with the same attributes:
That's basically the query that I'm using in the application, and if I see any matching rows I perform a different set of logic instead of creating a new launch. Simple enough.
What I'm stuck on is putting this together into one query for creating the report. Basically I'd like something that looks like the first query, but with an additional true/false column indicating whether a recent launch had been found. Then I'd be able to see the proportion of launches that wouldn't have been created had the new logic been in place.
It would be a simple enough matter in a script where I could just loop through the results from the first query and then run the second query on each individual row, but I always get hung up when trying to translate such tasks into a single complex SQL statement.
Basically we have programs that create "launches", then other programs that look for these newly created launch records and start doing some work. We'd like to limit the number of launches that are created, by preventing new launches being created with the same attributes as a recent launch.
I've actually created the application logic, but before I can implement it I have to prove it's theoretical value with the aforementioned report. What I'm having a bit of trouble doing is coming up with the SQL for the report.
Note that our database environment here is MS Sql Server 2005.
So I can look at launches from the past week with this pseudo-SQL:
Code:
SELECT LaunchID, Attribute1, Attribute2, InsertTime
FROM Launches
WHERE InsertTime > DATEADD(DAY, -7, GETDATE())
Now with the information about an individual launch in hand, it's easy enough to write a query to figure out if we have any recent launches (in this case, 24 hours) with the same attributes:
Code:
SELECT LaunchID
FROM Launches
WHERE LaunchID <> @LaunchID
AND InsertTime > DATEADD(HOUR, -24, @InsertTime))
AND Attribute1 = @Attribute1 AND Attribute2 = @Attribute2
That's basically the query that I'm using in the application, and if I see any matching rows I perform a different set of logic instead of creating a new launch. Simple enough.
What I'm stuck on is putting this together into one query for creating the report. Basically I'd like something that looks like the first query, but with an additional true/false column indicating whether a recent launch had been found. Then I'd be able to see the proportion of launches that wouldn't have been created had the new logic been in place.
It would be a simple enough matter in a script where I could just loop through the results from the first query and then run the second query on each individual row, but I always get hung up when trying to translate such tasks into a single complex SQL statement.