Advertisement
bytecoded

Top 5 Query SQL

Mar 23rd, 2019
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 11.23 KB | None | 0 0
  1. (@P1 varbinary(16),@P2 NUMERIC(10),@P3 varbinary(16),@P4 NUMERIC(10),@P5 NUMERIC(10),@P6 NUMERIC(10),@P7 nvarchar(4000),@P8 NUMERIC(10),@P9 varbinary(16),@P10 NUMERIC(10),@P11 NUMERIC(10),@P12 NUMERIC(10),@P13 NUMERIC(10),@P14 NUMERIC(10),@P15 datetime2(3),@P16 varbinary(16),@P17 NUMERIC(10),@P18 NUMERIC(10),@P19 NUMERIC(10),@P20 datetime2(3),@P21 varbinary(16),@P22 NUMERIC(10),@P23 NUMERIC(10),@P24 NUMERIC(10),@P25 datetime2(3),@P26 datetime2(3),@P27 varbinary(16),@P28 NUMERIC(10),@P29 NUMERIC(10),@P30 NUMERIC(10),@P31 NUMERIC(10),@P32 nvarchar(4000),@P33 NUMERIC(10),@P34 varbinary(16),@P35 NUMERIC(10),@P36 NUMERIC(10),@P37 NUMERIC(10),@P38 NUMERIC(10),@P39 NUMERIC(10),@P40 datetime2(3),@P41 varbinary(16),@P42 NUMERIC(10),@P43 NUMERIC(10),@P44 NUMERIC(10),@P45 NUMERIC(10),@P46 NUMERIC(10),@P47 nvarchar(4000),@P48 NUMERIC(10),@P49 varbinary(16),@P50 NUMERIC(10),@P51 NUMERIC(10),@P52 NUMERIC(10),@P53 NUMERIC(10),@P54 NUMERIC(10),@P55 varbinary(16),@P56 varbinary(16),@P57 datetime2(3))SELECT TOP 25
  2. T39._IDRRef,
  3. T39._Marked,
  4. T39._Number,
  5. T39._Date_Time,
  6. T39._Posted,
  7. T39._Fld4621RRef,
  8. T39._Fld4622RRef,
  9. T39._Fld4625RRef,
  10. T39._Fld4633,
  11. T39._Fld4635RRef,
  12. T39._Fld4657RRef,
  13. CASE WHEN ((T39._Fld4657RRef = @P1) AND T39._Posted = 0x01) THEN CASE WHEN (ISNULL(CAST(T52.Fld10346Balance_ AS NUMERIC(33, 2)),0.0) > 0.0) THEN CASE WHEN (ISNULL(CAST(T40.Fld11330Turnover_ AS NUMERIC(27, 2)),0.0) = ISNULL(CAST(T42.Fld10545Balance_ AS NUMERIC(27, 2)),0.0)) THEN 4.0 WHEN ((ISNULL(CAST(T42.Fld10545Balance_ AS NUMERIC(27, 2)),0.0) = 0.0) AND (ISNULL(CAST(T40.Fld11330Turnover_ AS NUMERIC(27, 2)),0.0) > 0.0)) THEN 3.0 WHEN ((CAST(ISNULL(CAST(T40.Fld11330Turnover_ AS NUMERIC(27, 2)),0.0) AS NUMERIC(33, 8)) / 2.0) > ISNULL(CAST(T42.Fld10545Balance_ AS NUMERIC(27, 2)),0.0)) THEN 6.0 ELSE 5.0 END ELSE CASE WHEN (ISNULL(CAST(T40.Fld11330Turnover_ AS NUMERIC(27, 2)),0.0) = ISNULL(CAST(T42.Fld10545Balance_ AS NUMERIC(27, 2)),0.0)) THEN 0.0 WHEN ((ISNULL(CAST(T42.Fld10545Balance_ AS NUMERIC(27, 2)),0.0) = 0.0) AND (ISNULL(CAST(T40.Fld11330Turnover_ AS NUMERIC(27, 2)),0.0) > 0.0)) THEN 3.0 WHEN ((CAST(ISNULL(CAST(T40.Fld11330Turnover_ AS NUMERIC(27, 2)),0.0) AS NUMERIC(33, 8)) / 2.0) > ISNULL(CAST(T42.Fld10545Balance_ AS NUMERIC(27, 2)),0.0)) THEN 2.0 ELSE 1.0 END END ELSE 7.0 END,
  14. T57._Fld12313RRef,
  15. CASE WHEN ((ISNULL(CAST(T58.Fld10335Balance_ AS NUMERIC(27, 2)),0.0) + ISNULL(CAST(T42.Fld10545Balance_ AS NUMERIC(27, 2)),0.0)) > 0.0) THEN (ISNULL(CAST(T58.Fld10335Balance_ AS NUMERIC(27, 2)),0.0) + ISNULL(CAST(T42.Fld10545Balance_ AS NUMERIC(27, 2)),0.0)) ELSE 0.0 END,
  16. T68._Fld1191,
  17. T39._Fld4642
  18. FROM dbo._Document233 T39
  19. LEFT OUTER JOIN (SELECT
  20. T41._Fld11328RRef AS Fld11328RRef,
  21. CAST(SUM(T41._Fld11330) AS NUMERIC(33, 8)) AS Fld11330Turnover_
  22. FROM dbo._AccumRgTn11332 T41
  23. WHERE ((T41._Fld595 = @P2)) AND (((T41._Fld11324RRef = @P3)) AND (T41._Fld11330 <> @P4))
  24. GROUP BY T41._Fld11328RRef
  25. HAVING (CAST(SUM(T41._Fld11330) AS NUMERIC(33, 8))) <> 0.0) T40
  26. ON (T39._IDRRef = T40.Fld11328RRef)
  27. LEFT OUTER JOIN (SELECT
  28. T43._Fld10540_TYPE AS Fld10540_TYPE,
  29. T43._Fld10540_RTRef AS Fld10540_RTRef,
  30. T43._Fld10540_RRRef AS Fld10540_RRRef,
  31. CAST(SUM(T43._Fld10545) AS NUMERIC(33, 8)) AS Fld10545Balance_
  32. FROM dbo._AccumRgT10549 T43
  33. WHERE (((T43._Fld595 = @P5)) AND (EXISTS(SELECT
  34. 1
  35. FROM (SELECT 1 AS SDBL_DUMMY) SDBL_DUAL
  36. LEFT OUTER JOIN dbo._Reference42 T44
  37. ON ((T43._Fld10538RRef = T44._IDRRef)) AND (T44._Fld595 = @P6)
  38. WHERE EXISTS(SELECT
  39. 0x01 AS Q_001_F_000_
  40. FROM dbo._Reference98 T45
  41. INNER JOIN dbo._Reference21 T46
  42. ON (((T45._Fld1685 = @P7) AND EXISTS(SELECT
  43. 0x01 AS Q_004_F_000_
  44. FROM dbo._InfoRg11709 T47
  45. WHERE ((T47._Fld595 = @P8)) AND ((T47._Fld12065RRef = T45._IDRRef) AND (T47._Fld12066RRef = T46._IDRRef)))) AND T46._IDRRef IN
  46. (SELECT
  47. T48._Reference21_IDRRef AS Q_005_F_000RRef
  48. FROM dbo._Reference21_VT782 T48
  49. INNER JOIN dbo._InfoRg9937 T49
  50. ON ((T49._Fld9939_TYPE = 0x08 AND T49._Fld9939_RTRef = 0x00000095 AND T49._Fld9939_RRRef = @P9) AND (T49._Fld9938_TYPE = T48._Fld784_TYPE AND T49._Fld9938_RTRef = T48._Fld784_RTRef AND T49._Fld9938_RRRef = T48._Fld784_RRRef))
  51. WHERE ((T48._Fld595 = @P10)) AND (T49._Fld595 = @P11)))
  52. WHERE ((T46._Fld595 = @P12)) AND ((CASE WHEN T44._Fld12836RRef IS NULL THEN 0x01 ELSE CASE WHEN T44._Fld12836RRef IS NOT NULL THEN 0x08 END END <> 0x01) AND (CASE WHEN EXISTS(SELECT
  53. 0x01 AS Q_002_F_000_
  54. FROM dbo._InfoRg11710 T50
  55. WHERE ((T50._Fld595 = @P13)) AND ((T50._Fld12072RRef = T46._IDRRef) AND (T50._Fld12073_TYPE = CASE WHEN T44._Fld12836RRef IS NOT NULL THEN 0x08 END AND T50._Fld12073_RTRef = CASE WHEN T44._Fld12836RRef IS NOT NULL THEN 0x0000001C END AND T50._Fld12073_RRRef = T44._Fld12836RRef))) THEN 0x01 ELSE 0x00 END = CASE WHEN EXISTS(SELECT
  56. 0x01 AS Q_003_F_000_
  57. FROM dbo._InfoRg11715 T51
  58. WHERE ((T51._Fld595 = @P14)) AND ((T51._Fld12199RRef = T46._IDRRef) AND T51._Fld12200_TYPE = CASE WHEN T44._Fld12836RRef IS NOT NULL THEN 0x08 END AND T51._Fld12200_RTRef = CASE WHEN T44._Fld12836RRef IS NOT NULL THEN 0x0000001C END AND (T51._Fld12201 = 0x00))) THEN 0x01 ELSE 0x00 END)))))) AND (T43._Period = @P15 AND ((T43._Fld10536RRef = @P16)) AND (T43._Fld10545 <> @P17) AND (T43._Fld10545 <> @P18))
  59. GROUP BY T43._Fld10540_TYPE,
  60. T43._Fld10540_RTRef,
  61. T43._Fld10540_RRRef
  62. HAVING (CAST(SUM(T43._Fld10545) AS NUMERIC(33, 8))) <> 0.0) T42
  63. ON (0x08 = T42.Fld10540_TYPE AND 0x000000E9 = T42.Fld10540_RTRef AND T39._IDRRef = T42.Fld10540_RRRef)
  64. LEFT OUTER JOIN (SELECT
  65. T53.Fld10344RRef AS Fld10344RRef,
  66. CAST(SUM(T53.Fld10346Balance_) AS NUMERIC(38, 8)) AS Fld10346Balance_
  67. FROM (SELECT
  68. T54._Fld10344RRef AS Fld10344RRef,
  69. CAST(SUM(T54._Fld10346) AS NUMERIC(33, 8)) AS Fld10346Balance_
  70. FROM dbo._AccumRgT10349 T54
  71. WHERE ((T54._Fld595 = @P19)) AND (T54._Period = @P20 AND ((T54._Fld10340RRef = @P21)) AND (T54._Fld10346 <> @P22) AND (T54._Fld10346 <> @P23))
  72. GROUP BY T54._Fld10344RRef
  73. HAVING (CAST(SUM(T54._Fld10346) AS NUMERIC(33, 8))) <> 0.0
  74. UNION ALL SELECT
  75. T55._Fld10344RRef AS Fld10344RRef,
  76. CAST(CAST(SUM(CASE WHEN T55._RecordKind = 0.0 THEN -T55._Fld10346 ELSE T55._Fld10346 END) AS NUMERIC(27, 8)) AS NUMERIC(27, 2)) AS Fld10346Balance_
  77. FROM dbo._AccumRg10339 T55
  78. WHERE ((T55._Fld595 = @P24)) AND (T55._Period >= @P25 AND T55._Period < @P26 AND T55._Active = 0x01 AND ((T55._Fld10340RRef = @P27)))
  79. GROUP BY T55._Fld10344RRef
  80. HAVING (CAST(CAST(SUM(CASE WHEN T55._RecordKind = 0.0 THEN -T55._Fld10346 ELSE T55._Fld10346 END) AS NUMERIC(27, 8)) AS NUMERIC(27, 2))) <> 0.0) T53
  81. GROUP BY T53.Fld10344RRef
  82. HAVING (CAST(SUM(T53.Fld10346Balance_) AS NUMERIC(38, 8))) <> 0.0) T52
  83. ON (T39._IDRRef = T52.Fld10344RRef)
  84. LEFT OUTER JOIN dbo._InfoRg9134 T56
  85. ON ((0x08 = T56._Fld9136_TYPE AND 0x000000E9 = T56._Fld9136_RTRef AND T39._IDRRef = T56._Fld9136_RRRef)) AND (T56._Fld595 = @P28)
  86. LEFT OUTER JOIN dbo._InfoRg11720 T57
  87. ON ((0x08 = T57._Fld12312_TYPE AND 0x000000E9 = T57._Fld12312_RTRef AND T39._IDRRef = T57._Fld12312_RRRef)) AND (T57._Fld595 = @P29)
  88. LEFT OUTER JOIN (SELECT
  89. T59._Fld10332_TYPE AS Fld10332_TYPE,
  90. T59._Fld10332_RTRef AS Fld10332_RTRef,
  91. T59._Fld10332_RRRef AS Fld10332_RRRef,
  92. CAST(SUM(T59._Fld10335) AS NUMERIC(33, 8)) AS Fld10335Balance_
  93. FROM dbo._AccumRgT10337 T59
  94. WHERE (((T59._Fld595 = @P30)) AND (EXISTS(SELECT
  95. 1
  96. FROM (SELECT 1 AS SDBL_DUMMY) SDBL_DUAL
  97. LEFT OUTER JOIN dbo._Reference42 T60
  98. ON ((T59._Fld10330RRef = T60._IDRRef)) AND (T60._Fld595 = @P31)
  99. WHERE EXISTS(SELECT
  100. 0x01 AS Q_001_F_000_
  101. FROM dbo._Reference98 T61
  102. INNER JOIN dbo._Reference21 T62
  103. ON (((T61._Fld1685 = @P32) AND EXISTS(SELECT
  104. 0x01 AS Q_004_F_000_
  105. FROM dbo._InfoRg11709 T63
  106. WHERE ((T63._Fld595 = @P33)) AND ((T63._Fld12065RRef = T61._IDRRef) AND (T63._Fld12066RRef = T62._IDRRef)))) AND T62._IDRRef IN
  107. (SELECT
  108. T64._Reference21_IDRRef AS Q_005_F_000RRef
  109. FROM dbo._Reference21_VT782 T64
  110. INNER JOIN dbo._InfoRg9937 T65
  111. ON ((T65._Fld9939_TYPE = 0x08 AND T65._Fld9939_RTRef = 0x00000095 AND T65._Fld9939_RRRef = @P34) AND (T65._Fld9938_TYPE = T64._Fld784_TYPE AND T65._Fld9938_RTRef = T64._Fld784_RTRef AND T65._Fld9938_RRRef = T64._Fld784_RRRef))
  112. WHERE ((T64._Fld595 = @P35)) AND (T65._Fld595 = @P36)))
  113. WHERE ((T62._Fld595 = @P37)) AND ((CASE WHEN T60._Fld12836RRef IS NULL THEN 0x01 ELSE CASE WHEN T60._Fld12836RRef IS NOT NULL THEN 0x08 END END <> 0x01) AND (CASE WHEN EXISTS(SELECT
  114. 0x01 AS Q_002_F_000_
  115. FROM dbo._InfoRg11710 T66
  116. WHERE ((T66._Fld595 = @P38)) AND ((T66._Fld12072RRef = T62._IDRRef) AND (T66._Fld12073_TYPE = CASE WHEN T60._Fld12836RRef IS NOT NULL THEN 0x08 END AND T66._Fld12073_RTRef = CASE WHEN T60._Fld12836RRef IS NOT NULL THEN 0x0000001C END AND T66._Fld12073_RRRef = T60._Fld12836RRef))) THEN 0x01 ELSE 0x00 END = CASE WHEN EXISTS(SELECT
  117. 0x01 AS Q_003_F_000_
  118. FROM dbo._InfoRg11715 T67
  119. WHERE ((T67._Fld595 = @P39)) AND ((T67._Fld12199RRef = T62._IDRRef) AND T67._Fld12200_TYPE = CASE WHEN T60._Fld12836RRef IS NOT NULL THEN 0x08 END AND T67._Fld12200_RTRef = CASE WHEN T60._Fld12836RRef IS NOT NULL THEN 0x0000001C END AND (T67._Fld12201 = 0x00))) THEN 0x01 ELSE 0x00 END)))))) AND (T59._Period = @P40 AND ((T59._Fld10328RRef = @P41)) AND (T59._Fld10335 <> @P42) AND (T59._Fld10335 <> @P43))
  120. GROUP BY T59._Fld10332_TYPE,
  121. T59._Fld10332_RTRef,
  122. T59._Fld10332_RRRef
  123. HAVING (CAST(SUM(T59._Fld10335) AS NUMERIC(33, 8))) <> 0.0) T58
  124. ON (0x08 = T58.Fld10332_TYPE AND 0x000000E9 = T58.Fld10332_RTRef AND T39._IDRRef = T58.Fld10332_RRRef)
  125. LEFT OUTER JOIN dbo._Reference51 T68
  126. ON (T39._Fld4635RRef = T68._IDRRef) AND (T68._Fld595 = @P44)
  127. WHERE (((((T39._Fld595 = @P45) AND (EXISTS(SELECT
  128. 1
  129. FROM (SELECT 1 AS SDBL_DUMMY) SDBL_DUAL
  130. LEFT OUTER JOIN dbo._Reference42 T69
  131. ON ((T39._Fld4625RRef = T69._IDRRef)) AND (T69._Fld595 = @P46)
  132. WHERE EXISTS(SELECT
  133. 0x01 AS Q_001_F_000_
  134. FROM dbo._Reference98 T70
  135. INNER JOIN dbo._Reference21 T71
  136. ON (((T70._Fld1685 = @P47) AND EXISTS(SELECT
  137. 0x01 AS Q_004_F_000_
  138. FROM dbo._InfoRg11709 T72
  139. WHERE ((T72._Fld595 = @P48)) AND ((T72._Fld12065RRef = T70._IDRRef) AND (T72._Fld12066RRef = T71._IDRRef)))) AND T71._IDRRef IN
  140. (SELECT
  141. T73._Reference21_IDRRef AS Q_005_F_000RRef
  142. FROM dbo._Reference21_VT782 T73
  143. INNER JOIN dbo._InfoRg9937 T74
  144. ON ((T74._Fld9939_TYPE = 0x08 AND T74._Fld9939_RTRef = 0x00000095 AND T74._Fld9939_RRRef = @P49) AND (T74._Fld9938_TYPE = T73._Fld784_TYPE AND T74._Fld9938_RTRef = T73._Fld784_RTRef AND T74._Fld9938_RRRef = T73._Fld784_RRRef))
  145. WHERE ((T73._Fld595 = @P50)) AND (T74._Fld595 = @P51)))
  146. WHERE ((T71._Fld595 = @P52)) AND ((CASE WHEN T69._Fld12836RRef IS NULL THEN 0x01 ELSE CASE WHEN T69._Fld12836RRef IS NOT NULL THEN 0x08 END END <> 0x01) AND (CASE WHEN EXISTS(SELECT
  147. 0x01 AS Q_002_F_000_
  148. FROM dbo._InfoRg11710 T75
  149. WHERE ((T75._Fld595 = @P53)) AND ((T75._Fld12072RRef = T71._IDRRef) AND (T75._Fld12073_TYPE = CASE WHEN T69._Fld12836RRef IS NOT NULL THEN 0x08 END AND T75._Fld12073_RTRef = CASE WHEN T69._Fld12836RRef IS NOT NULL THEN 0x0000001C END AND T75._Fld12073_RRRef = T69._Fld12836RRef))) THEN 0x01 ELSE 0x00 END = CASE WHEN EXISTS(SELECT
  150. 0x01 AS Q_003_F_000_
  151. FROM dbo._InfoRg11715 T76
  152. WHERE ((T76._Fld595 = @P54)) AND ((T76._Fld12199RRef = T71._IDRRef) AND T76._Fld12200_TYPE = CASE WHEN T69._Fld12836RRef IS NOT NULL THEN 0x08 END AND T76._Fld12200_RTRef = CASE WHEN T69._Fld12836RRef IS NOT NULL THEN 0x0000001C END AND (T76._Fld12201 = 0x00))) THEN 0x01 ELSE 0x00 END)))))) AND (T39._Fld4636RRef = @P55)) AND (T39._Fld4621RRef = @P56)) AND (T39._Posted = 0x00)) AND T39._Date_Time > @P57
  153. ORDER BY (T39._Date_Time), (T39._IDRRef)
  154. OPTION (FAST 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement