I'm trying to write a query that pulls multiple columns from two tables and then also looks to see if there is another instance containing the same kind of data within the last 28 days.
I've got the query working and it looks something like this:
SELECT
a.col_a
,a.col_b
,a.col_c
,b.col_a
,b.col_b
,CASE WHEN EXSISTS (SELECT a_sub.col_a
FROM a_sub
INNER JOIN b_sub
ON a_sub.x = b_sub.x
WHERE
b.col_a = b_sub.col_a
AND b.col_b <> b_sub.col_b
AND a.date > a_sub.date
AND a.date <= DATEADD(d, 28, a_sub.date)
AND a.col_c = a_sub.col_c
AND (a.col_d IS NULL OR a.col_d <> 7)
AND (a_sub.col_d IS NULL OR a_sub.col_d <> 7)
) THEN 'Yes'
ELSE 'No'
END AS IsRepeat28
FROM a
INNER JOIN b ON a.x = b.x
This isn't particularly quick, but nor is it too slow to be a problem. The issue is I need to repeat the above WHEN EXISTS subquery but for 21, 14, 7 and 1 day periods as well making 5 repetitions - the query time then goes from roughly 15 seconds to about 10 minutes.
I also need to adjust the dates so as well as flagging rows that are "IsRepeat" there is also a version for "HasRepeat" which is based on
AND a_sub.date > a.date AND a_sub.date <= DATEADD(d, 28, a.date)
instead of the original
AND a.date > a_sub.date AND a.date <= DATEADD(d, 28, a_sub.date)
This then means having 10 of these EXISTS subqueries and it takes almost 45 minutes.
My question is really twofold; is there a more efficient way of writing this subquery, and is there a better way of repeating it rather than having virtually the same code written out 10 times?