You should always try to avoid placing any type of function or conversion on predicate columns used in the WHERE or JOINs.
In other words you want to avoid this type of thing:
"ON DATEPART(YEAR, a.[date]) = DATEPART(YEAR, b.entryDate) AND DATEPART(WEEK, a.[date]) = DATEPART(WEEK, b.entryDate)"
Its also best to avoid using "SELECT *"
Specifically, the reason for avoiding " SELECT * " is because unless you can guarantee that the underlying tables will never change, it is Best Practice to always type each column you need. I've heard it described as the Bus Rule: If you get hit
by a bus, you want your code to be easy to understand by another programmer.
As such, it is expedient for programmers if they don't have to guess what data is being extracted from which table. Therefore, it is also Best Practice to include the master database name your data is in located in (USE <databasename>), as well as
name the SCHEMA of your tables ensure polymorphism throughout the query
(SELECT DATE FROM dbo.tblWagesWeeks) as opposed to (SELECT DATE FROM tblWagesWeeks).
The great part about doing this is that even if the company you work for has an office full of idiots who ignore normalization rules everywhere by not differentiating tables in different SCHEMAs, another programmer will still be able to execute your queries
without fear of unknown problems (and undoubtedly will bless your name, too!).
-
Edited by
Conquistador0
1 hour 34 minutes ago
For clarity