r/SQLServer • u/mongar23 • Jan 09 '26
Question This doesn't seem right
So I have been working on a school project to create a workout tracker/logger, now I have been a C# programmer for a few years but never interacted with dbs what so ever. I came up with this monstosity to get a list of all the exercise entries that can be labeled as pr. I would like to know if this is the correct approach to do this or if I could improve it. Thanks in advance
SELECT
et.Name,
e.KgsOrMtr,
e.RepsOrSecs,
et.MeasurementTypeID
FROM
Exercises e
INNER JOIN ExerciseTypes et ON
e.ExerciseTypeID = et.ID
WHERE
e.ID in (
SELECT
MIN(e2.ID) as exercise_id
FROM
Exercises e2
INNER JOIN ExerciseTypes et2 ON
e2.ExerciseTypeID = et2.ID
INNER JOIN (
SELECT
et3.ID,
MAX(IIF(et3.MeasurementTypeID = 1, (e2.KgsOrMtr * e2.RepsOrSecs), (ROUND((CAST(e2.KgsOrMtr AS float) / e2.RepsOrSecs), 1)))) AS total_max
FROM
Exercises e2
INNER JOIN ExerciseTypes et3 ON
e2.ExerciseTypeID = et3.ID
GROUP BY
et3.ID ) exercise_totals ON
et2.ID = exercise_totals.ID
AND IIF(et2.MeasurementTypeID = 1, (e2.KgsOrMtr * e2.RepsOrSecs), (ROUND((CAST(e2.KgsOrMtr AS float) / e2.RepsOrSecs), 1))) = exercise_totals.total_max
GROUP BY
et2.ID )
if it helps this is the ERD

4
Upvotes
•
u/AutoModerator Jan 09 '26
After your question has been solved /u/mongar23, please reply to the helpful user's comment with the phrase "Solution verified".
This will not only award a point to the contributor for their assistance but also update the post's flair to "Solved".
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/SQLBek 1 Jan 09 '26
No, this is far from optimal. I will suggest that you break this up into 2 or 3 different statements and materialize your resultset in between via a temp table.
3
u/GuanoLouco Jan 09 '26
Using select statements is just going to result in nesting and will eventually cause performance issues and database locking, depending on how much data you are querying.
You need to sit down and think about what you are trying to achieve in both the short and long term because making a mistake now can make additional development in the future very difficult (sometimes even impossible.)
If it’s just a small hobby app you might be able to get away with it. If you plan to use it in a commercial environment your decisions are very different.
If you really want to use views or direct queries then you need to break into temp tables or cte’s.
When designing your database, use the same logic in SQL as you would in programming. You want your code to be reusable and scalable so create objects exactly like you would in C#.
You can use functions to query and declare variables which you can pass to a stored procedure. You have a lot more flexibility and control with database programming than you do with database querying.
1
u/salva922 Jan 09 '26 edited Jan 09 '26
Example:
;WITH x AS ( SELECT et.Name, e.KgsOrMtr, e.RepsOrSecs, et.MeasurementTypeID, e.ID, Score = CASE WHEN et.MeasurementTypeID = 1 THEN e.KgsOrMtr * e.RepsOrSecs ELSE ROUND(CONVERT(float, e.KgsOrMtr) / NULLIF(e.RepsOrSecs, 0), 1) END FROM dbo.Exercises AS e JOIN dbo.ExerciseTypes AS et ON et.ID = e.ExerciseTypeID ), r AS ( SELECT Name, KgsOrMtr, RepsOrSecs, MeasurementTypeID, rn = ROW_NUMBER() OVER ( PARTITION BY MeasurementTypeID, Name -- or better: PARTITION BY ExerciseTypeID ORDER BY Score DESC, ID ASC ) FROM x ) SELECT Name, KgsOrMtr, RepsOrSecs, MeasurementTypeID FROM r WHERE rn = 1;
1
u/dbrownems Microsoft Employee Jan 09 '26
This is a job for window functions. https://learn.microsoft.com/en-us/sql/t-sql/queries/select-window-transact-sql?view=sql-server-ver17
Join Exercises to Users to get a relation that has
ae(ExercizeId, UserId, WorkoutDate, Score)
Then
1
u/gmen385 Jan 12 '26
So, what you want is "for every exercise type, among the ones sharing the max score, bring the one with min id".
Interesting. I'm taking a few mins thinking of a solution. I partly disagree with the positions here - this is not too complex a query to immediately think of temp tables - and this is even ignoring the lightness of your project. All in all, good tries until now.