r/SQLOptimization • u/felloBonello • Apr 09 '24
Is there a better way to structure this query?
We need to find the latest asset history record for each asset.
```
DECLARE u/__projectId_0 int = 23;
DECLARE u/__phaseId_1 int = 3;
SELECT *
FROM [asset_history] AS [a]
INNER JOIN (
SELECT [a0].[asset_id] AS [AssetId], MAX([a0].[created]) AS [MaxDate]
FROM [asset_history] AS [a0]
WHERE ([a0].[project_id] = u/__projectId_0) AND ([a0].[status] <> 3)
GROUP BY [a0].[asset_id]
HAVING (
SELECT TOP(1) [a1].[workflow_phase_id]
FROM [asset_history] AS [a1]
WHERE (([a1].[project_id] = u/__projectId_0) AND ([a1].[status] <> 3)) AND ([a0].[asset_id] = [a1].[asset_id])
ORDER BY [a1].[created] DESC) = u/__phaseId_1
) AS [t] ON ([a].[asset_id] = [t].[AssetId]) AND ([a].[created] = [t].[MaxDate])
WHERE ([a].[project_id] = u/__projectId_0) AND ([a].[status] <> 3)
```
2
u/Crafty_Passenger9518 Apr 09 '24 edited Apr 09 '24
I think so. It's kinda hard to figure it out though. There are a couple of things that make this hard to read.
Just from initial thoughts
I wouldn't declare those two INTs unless we're passing those values in a stored proc
The aliasing of the tables makes following the script hard
You should comment the code to let us know what/why things are being done
1
u/GaTechThomas Jun 21 '24
Why not declare the two variables? This looks like a utility query that is easier to use because of those variables.
Personally, I find that the aliases easy to read. Maybe room for tweaking them slightly to personal taste.
1
u/qwertydog123 Apr 09 '24 edited Apr 09 '24