OK, I am going back to square one and starting over. A lot of very helpful ideas have been given and I have learned a lot, yet I am still not being successful in my query.
Currently my NON working query is as follows:
DECLARE @reportYear int = (year(getdate()) -1);
sum(Amount) AS TOTAL
FROM tbDESCRIPT as d
INNER JOIN tbEXPENSES as e ON d.ID=e.DESCRIPT_ID
WHERE LOADNUMBER LIKE (@reportYear + '%')
AND E.SECT_ID = '51'
AND e.SECT_ID = '6'
AND (e.DESCRIPT_ID ='97' or e.DESCRIPT_ID ='98' or e.DESCRIPT_ID ='99' or e.DESCRIPT_ID ='100')
GROUP BY LOADNUMBER
I have changed to doing an INNER join as was highly recommended by many of you.
Please notice in my
WHERE clause that I have coded to get the current year, minus 1 so I can look at 2018 records since it is now 2019. However still no records are being returned. Specifically, what is incorrect in the
WHERE clause to prevent me from getting records back?
What I have notice in the many suggestions, except one, that it appears most of the code, to me, is only bringing back the year, with no additional numbers afterwards. Please keep in mind I am still relatively new to SQL coding so my comments are not directed to anyone and is a result of either missing something or not understanding something.
The parameter value needs to be current year plus a sequential 3 numbers afterwards (end result should be any LOADNUMBER that is numbered 2018001 thru 2018999 if I want 2018 records).... Additionally, I am attempting to ONLY get back records with a LOADNUMBER beginning with, in the current case, 2018. However this needs to be coded so I am not forced to go back and re-code the SQL each year.
So in the simplest of terms I want to grab the YEAR and concatenate a wildcard after it to bring back a string of YEAR**+**nnn (ex. 2016085, 2018123, 2021031 etc.) or similar to:
WHERE LOADNUMBER LIKE @reportYear + '%'