SQL Query Question (MS Sql Server)

Status
Not open for further replies.

eXceLon

Ars Legatus Legionis
48,707
Subscriptor++
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:

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.
 

wallinbl

Ars Legatus Legionis
14,319
Subscriptor
Code:
SELECT Date, Attribute1, Attribute2, COUNT(*) As Launches
FROM Launches
GROUP BY Date, Attribute1, Attribute2
HAVING COUNT(*) > 1

Shows instances of launches on same day with same attributes.

Alternately
Code:
SELECT L1.LaunchID, COUNT(L2.LaunchID) As RecentDuplicates
FROM Launches L1
INNER JOIN Launches L2 
     ON L1.LaunchID <> L2.LaunchID 
     AND L1.Attribute1 = L2.Attribute1
     AND L1.Attribute2 = L2.Attribute2
     AND L2.InsertTime BETWEEN DATEADD(HOUR, -24, L1.InsertTime) AND L1.InsertTime

Shows launches that had similar launches within the prior 24 hours.
 

eXceLon

Ars Legatus Legionis
48,707
Subscriptor++
I like the second method best, since it makes it easier to change the range and see how it would impact the results. Right now the thought is 24 hours, but that could easily change.

I always forget about joining a table back onto itself.

I'm not getting that query to work though. The only thing I'm changing is to make sure the actual table/column names match our real environment, otherwise it's exactly as you written.

Column 'Launches.LaunchID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
 

eXceLon

Ars Legatus Legionis
48,707
Subscriptor++
Duh, I should have seen that.

I made one minor tweak: Changing to a left join instead of an inner join shows the rows with 0 recent matches, instead of just those with one or more. Otherwise this is exactly what I needed, thanks!

Code:
SELECT L1.LaunchID, COUNT(L2.LaunchID) As RecentDuplicates
FROM Launches L1
LEFT JOIN Launches L2
     ON L1.LaunchID <> L2.LaunchID
     AND L1.Attribute1 = L2.Attribute1
     AND L1.Attribute2 = L2.Attribute2
     AND L2.InsertTime BETWEEN DATEADD(HOUR, -24, L1.InsertTime) AND L1.InsertTime
GROUP BY L1.LaunchID
 
Status
Not open for further replies.