Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. USE [EForTDB]
  2. GO
  3. /****** Object: StoredProcedure [FOREX].[sp_GetAccountEquity] Script Date: 10/22/2019 3:58:48 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER PROCEDURE [FOREX].[sp_GetAccountEquity]
  9. @loginId INT,
  10. @startDate datetime,
  11. @endDate datetime,
  12. @timeFrame INT
  13. AS
  14.  
  15. BEGIN
  16. SELECT 0 AS Id, aa.LoginId, cc.[Open], aa.High, aa.Low, bb.[Close], aa.Volume, aa.TimeSlot, GETDATE() AS CreatedAt, GETDATE() AS UpdatedAt, GETDATE() AS DeletedAt, CONVERT(BIT,0) AS IsDeleted
  17. FROM (
  18. SELECT b.LoginId, MIN(b.Low) as Low, MAX(b.High) as High, SUM(b.Volume) as Volume,
  19. -- Add the rounded seconds back onto epoch to get rounded time
  20. DATEADD(
  21. MINUTE,
  22. (DATEDIFF(MINUTE, '1990-01-01 00:00:00', b.TimeSlot) / @timeFrame) * @timeFrame,
  23. '1990-01-01 00:00:00'
  24. ) AS 'TimeSlot'
  25. FROM FOREX.AccountEquity b
  26. WHERE b.LoginId=@loginId AND (b.TimeSlot BETWEEN @startDate AND @endDate)
  27. GROUP BY (DATEDIFF(MINUTE, '1990-01-01 00:00:00', b.TimeSlot) / @timeFrame), b.LoginId
  28. ) aa
  29.  
  30. INNER JOIN (
  31. SELECT LoginId, [Close],
  32. DATEADD(
  33. MINUTE,
  34. (DATEDIFF(MINUTE, '1990-01-01 00:00:00', TimeSlot) / @timeFrame) * @timeFrame,
  35. '1990-01-01 00:00:00'
  36. ) AS 'TimeSlot'
  37. FROM FOREX.AccountEquity
  38. WHERE TimeSlot IN (
  39. SELECT MAX(TimeSlot)
  40. FROM FOREX.AccountEquity b
  41. GROUP BY (DATEDIFF(MINUTE, '1990-01-01 00:00:00', b.TimeSlot) / @timeFrame)
  42. )
  43. )bb ON aa.TimeSlot = bb.TimeSlot and aa.LoginId = bb.LoginId
  44.  
  45. INNER JOIN (
  46. SELECT LoginId, [Open],
  47. DATEADD(
  48. MINUTE,
  49. (DATEDIFF(MINUTE, '1990-01-01 00:00:00', TimeSlot) / @timeFrame) * @timeFrame,
  50. '1990-01-01 00:00:00'
  51. ) AS 'TimeSlot'
  52. FROM FOREX.AccountEquity
  53. WHERE TimeSlot IN (
  54. SELECT MIN(TimeSlot)
  55. FROM FOREX.AccountEquity b
  56. GROUP BY (DATEDIFF(MINUTE, '1990-01-01 00:00:00', b.TimeSlot) / @timeFrame)
  57. )
  58. ) cc ON aa.TimeSlot = cc.TimeSlot and aa.LoginId = cc.LoginId
  59. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement