d90
d90

Reputation: 787

Year Week column comparison

I have this query that spits out every Week for the past 2 years a company owes us money. I have to narrow the results down based on their starting year and week, which are 2 separate columns. This is my current query. How do I change this so it only shows data where the year is greater or equal to start year and the week is greater or equal to the start week without losing data? So for example if I have a company 023 and its start date is week 5 2012, I'll say year>=2012 and week>=5, but then it will exclude all the weeks in 2013 and 2014 where week is less than 5. I'm not sure how logically to get around that.

SELECT   MW.MW_Weeks.Year, 
         MW.MW_Weeks.Week, 
         MW.MW_CompanyCodes.cmp_code, 
         MW.MW_CompanyCodes.stWeek, 
         MW.MW_CompanyCodes.stYear
FROM     MW.MW_Weeks CROSS JOIN
         MW.MW_CompanyCodes
WHERE    (MW.MW_Weeks.WEDate <= GETDATE()) 
     AND (MW.MW_Weeks.Year > YEAR(GETDATE()) - 2);

Upvotes: 0

Views: 116

Answers (1)

Vulcronos
Vulcronos

Reputation: 3456

Use the following:

year >= 2012 and not (year = 2012 and week < 5)

This will get all data whose year is greater than or equal to 2012 and is not before week five in 2012.

Upvotes: 1

Related Questions