Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.42 KB | None | 0 0
  1. WITH    a6
  2.           AS ( SELECT   cdc.col2
  3.                        ,maxLimit.col15
  4.                FROM     table1 AS cdc
  5.                JOIN     table2 AS cdt  -- comment out this join to get seek
  6.                         ON cdt.col1 = cdc.col1
  7.                OUTER APPLY ( SELECT TOP 1
  8.                                     rules.col11
  9.                                    ,rules.col15
  10.                              FROM   ( SELECT    clr.col11
  11.                                                ,clr.col15
  12.                                                ,Priority = ROW_NUMBER() OVER ( ORDER BY clr.col2 DESC, clr.col1 DESC, clr.col10 DESC, clr.col12 DESC, clr.col13 DESC, clr.col7 DESC , clr.col14 DESC )
  13.                                       FROM      table4 AS clr
  14.                                       WHERE     clr.col7 IS NULL OR clr.col7 = cdc.col7
  15.                                     ) AS rules
  16.                              WHERE  rules.Priority = 1
  17.                            ) maxLimit
  18.              )
  19.     SELECT  a1.[col2] AS [col22]
  20.     FROM    [dbo].[table1] a1
  21.     INNER JOIN [dbo].[table5] a2
  22.             ON a1.[col2] = a2.[col2]
  23.     INNER JOIN [dbo].[table6] a3
  24.             ON a2.[col2] = a3.[col18]
  25.     INNER JOIN [dbo].[table1] a4
  26.             ON a3.[col19] = a4.[col2]
  27.     INNER JOIN a6
  28.             ON a6.col2 = a4.col2
  29.     WHERE   0 < ISNULL(a6.[col15], 0)            
  30.             AND a1.col2 = '1234123412341234';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement