sql server - How to improve SQL query performance (correlated subqueries)? -


i write below query in better & efficient way help?

select   a.assetnum asset,          a.assettag asset_tag,          a.manufacturer manufacturer,          a.serialnum serial,          a.description description,          (              select case  a.isrunning                     when  1                       'operational'                      when  0                       'down'                      end          )  condition ,          l.kbs_loctag location,          (              select top 1 wo.wonum                 workorder wo                wo.assetnum = a.assetnum                     , wo.worktype = 'un'              order wo.reportdate desc          ) last_workorder,          (              select wo.statusdate                 workorder wo                wo.wonum in                     (                         select   top 1 wo.wonum                              workorder wo                            wo.assetnum = a.assetnum                                  , wo.worktype = 'un'                         order wo.reportdate desc                     )          ) last_status_date,          (              select top 1 lt.memo                labtrans lt               lt.assetnum = a.assetnum                      , lt.transtype = 'repair'              order lt.transdate desc        ) action   asset         left outer join locations l            on a.location = l.location  (            a.description '%wash%'             or a.description '%dryer%'        ) order  l.location,         a.description 

in cases prefer use apply operator instead of correlated subquery.

in case suggest next solution:

select   a.assetnum asset,          a.assettag asset_tag,          a.manufacturer manufacturer,          a.serialnum serial,          a.description description,          case  a.isrunning              when  1  'operational'               when  0  'down'           end  condition,          l.kbs_loctag location,          wo.wonum last_workorder,          wo.statusdate last_status_date,          lt.memo action   asset         left outer join locations l on a.location = l.location        outer apply (              select top 1 wonum, statusdate                workorder                assetnum = a.assetnum                     , worktype = 'un'              order reportdate desc) wo        outer apply (              select top 1 memo                labtrans               assetnum = a.assetnum                      , transtype = 'repair'              order transdate desc) lt   (            a.description '%wash%'             or a.description '%dryer%'        ) order  l.location, a.[description] 

btw - can find amazing video lesson (from itzik ben-gan)about using apply operator here.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -