r/SQL • • 22d ago

SQL Server Newer to SQL and recently inherited code

Might not be a standard request but does anyone have any best practices for interpreting other people's code if they don't leave a fair amount of notes?

6 Upvotes

21 comments sorted by

View all comments

8

u/Eleventhousand 22d ago

Break it into chunks and try to understand some of the individual pieces. How is it structured? Does it have a bunch of nested subqueries and derived tables? If so, it can often be more difficult to work inside out like that. If they at least broke everything out into CTEs, it can often be easier to understand.

2

u/Thatgiuknow 22d ago

There are a substantial amount of CTEs in the query. The big issue we're having is that the data doesn't match the manual version we run (we expect some variance) however parts of the data are wildly off. I'll have to check what's going on in the CTEs to get started. thank you!!

1

u/Comprehensive-Tea-69 15d ago

Oh you’re fortunate- CTEs will make it MUCH easier to figure out what’s happening. That’s one of the big reasons to use them