Hello,I am fairly new to T-SQL and new to the forum. I was given the below query to see if there was a way to optimize it to be manageable. After trying to dissect and deconstruct it I could not avoid the massive row counts the conditional joins were causing. The main purpose of this exercise was to be able to add a rank order to each id to be able to request stats such as how many IDs were in xyz status on any given day. Any thoughts or feedback would be greatly appreciated.select backbone.id ,backbone.firstday ,backbone.datekey , trans.logdate , trans.tostatus , DENSE_RANK () OVER ( PARTITION by backbone.id, backbone.datekey ORDER BY trans.logdate DESC) RankOrder from (select source.id,source.FirstDay, datekey.Datekey from (select id, cast(Min(logdate) as date) FirstDay From Prod.dbo.history group by id) source cross join (select cast(FullDateKey as Date) DateKey From Fin.dbo.Calendar where fulldatekey <= '03-31-2016') datekey where datekey.Datekey >= source.FirstDay) backbone left join Prod.dbo.history trans on backbone.id=trans.id and backbone.datekey >= trans.logdate order by backbone.datekey, trans.logdate,RankOrderThanks!
↧