I am struggling with an assignment and I am not sure if there is a solution. To be clear: I am not looking for the amount of years in between(datediff()).
I have to join two tables:
- Table 1 with attributes:
- ID
- P_Startyear
- P_Endyear
- Table 2 with attributes:
- ID
- B_Year
There are more attributes in the tables, but for this question they do not matter.
As example: The P_Startyear is 2008 and the P_endyear is 2010. Every P_year has to match a B_Year. If not, then the P_row has to be returned. The problem however is that I also have to check the years in between the P_startyear and P__endyear (in this case 2009). So I also have to check if 2009 has a matching B_year and if that is not the case, the row has to be returned(together with the other matches).
I have already wrote some working code to check matches of the P_startyear and P_endyear:
SELECT
P.ID,
YEAR(P.P_Startyear) as [P_Startyear],
YEAR(P.P_Endyear) as [P_Endyear],
B.B_Year
FROM Table1 P
LEFT OUTER Join Tabl2 B
on P.ID = B.ID
Where
(YEAR(P.P_Startyear) <> B.B_Year
AND YEAR(P.P_Endyear) <> B.B_Year
-- Check if the year(s) in between startyear and end year match a B.B_year with the same ID).
Result:
CaseIdentifier P_Startyear P_Endyear B_Year
DCM_TEST 2011 2012 2010
DCm2011____25 2011 2012 2010
DCm2011____71 2012 2012 2009
DCm2011____71 2012 2012 2011
DCm2013____37 2013 2014 2007
DCm2013____37 2013 2014 2009
DCm2013____37 2013 2014 2012
DCm2013____56 2021 2022 2012
DCm2013____8 2012 2012 2010
DCm2013____9 2012 2012 2010
I have tried to get the year in between by filling a temp_table, check the datedifference and then IF-Else Case it. This should work when there is only 1 year in between the start and endyear, but not when there are multiple years, because you can only add one value in an if-else case statement(Boolean).
Example of the wrong testcode:
-- Create a Temp_table list with startdates per P_ID.
SELECT
p.ID,
p.P_Startyear as [Startyear1],
p.P_Startyear as [Startyear2],
p.P_Startyear as [Startyear3],
p.P_Startyear as [Startyear4],
p.P_Startyear as [Startyear5],
INTO #Years
FROM Table1 as p
--=====================================================================
Select
DATEDIFF(year,P_Startyear, P_Endyear) as DiffYears,
J.Startyear1,
J.Startyear2,
J.Startyear3,
J.Startyear4,
J.Startyear5,
-- Case if 1 year then Startyear1 +1, if 2 years Startyear1 +1 and Startyear2 +2 etc.
case
when DATEDIFF(year,P_Startyear, P_Endyear) = '1' then Startyear1 +1
when DATEDIFF(year,P_Startyear, P_Endyear) = '2' then Startyear1 +1 AND Startyear2 +2
when DATEDIFF(year,P_Startyear, P_Endyear) = '3' then J.Startyear1 +1 AND J.Startyear2 +2 AND J.Startyear3 +3
when DATEDIFF(year,P_Startyear, P_Endyear) = '4' then J.Startyear1 +1 AND J.Startyear2 +2 AND J.Startyear3 +3 AND J.Startyear4 +4
when DATEDIFF(year,P_Startyear, P_Endyear) = '4' then J.Startyear1 +1 AND J.Startyear2 +2 AND J.Startyear3 +3 AND J.Startyear4 +4 AND J.Startyear5 +5
end as [Startyear]
FROM Table 1 as p
Join #Years as J
On P.ID = J.ID
Drop table #Years
I am aware that the Case statement is incorrect, but it is just to show the potentional solution. Excuse me for any typo's I have made. I had to rename everything due to Dutch names and privacy matters. I have tried to explain the situation as comprehensive as possible, but if you have any questions, don't hesitate to ask them. Any help is appreciated, even if you are sure this can not be solved I would like to hear it. I am working with Microsoft SQL server 2008.