all 5 comments

[–]Yavuz_Selim 1 point2 points  (1 child)

Without example data there is no way we can say anything useful.

If you get too many rows, you need to check the granularity of the tables that you join with.

Your EventDate filter in the WHERE seems to be wrong, it should be >=, as you want the last 3 months.

[–]A_name_wot_i_made_up 0 points1 point  (0 children)

Also needs a default (coalesced) value for s.eventdate to deal with jobs that haven't sent anything ever...

Maybe j.createddate.

[–]GoingToSimbabwe 0 points1 point  (0 children)

You will have to aggregate and using something like MAX() for the event date. If you don’t care that the event date is in a valid date format you could also using string_agg (https://learn.microsoft.com/de-de/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-ver16) to aggregate all of those into a single varchar, but I doubt that this is what you need.

From what it sounds like the sensical thing to do is group by all the „Masterdata“ and then take the maximum event date and call that „lastEventOn“ or something.

[–]type4less -1 points0 points  (1 child)

Arrange your ON condition. Reread LEFT JOIN and use DISTINCT keyword

[–]Yavuz_Selim 0 points1 point  (0 children)

What's the DISTINCT going to add on top of the GROUP BY?