Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 8.73 KB | None | 0 0
  1. --exec [XBRL].[Import.AccountDepoByPeriod]
  2. Declare
  3.      @adtStart      date = '2017-07-01'
  4.     ,@adtEnd        date = '2017-07-05'
  5.  
  6.     Declare @now  datetime = getdate()
  7.  
  8.     Set @adtStart = isnull(@adtStart,DateAdd(month,-1,getdate()))
  9.     Set @adtEnd   = isnull(@adtEnd,getdate())
  10.  
  11. ---------------------------------------------------------------
  12.     If Object_id('tempdb..#tbl_AccountDepoByPeriod_pre_Operation') is not Null
  13.         Drop table #tbl_AccountDepoByPeriod_pre_Operation
  14.     Create Table #tbl_AccountDepoByPeriod_pre_Operation (ID bigint not Null Primary key)
  15.  
  16.     Insert into #tbl_AccountDepoByPeriod_pre_Operation (ID)
  17.         Select ID
  18.             From Depo.Operation
  19.             Where DepoDate >= @adtStart
  20.               and DepoDate <= @adtEnd
  21.  
  22.  
  23. -- Operations
  24.     If Object_id('tempdb..#tbl_AccountDepoByPeriod_Operation') is not Null
  25.         Drop table #tbl_AccountDepoByPeriod_Operation
  26.  
  27.     Create Table #tbl_AccountDepoByPeriod_Operation
  28.         (
  29.              [ID]              bigint
  30.             ,[Account_ID]      int
  31.             ,[SubAccount_ID]   int not null
  32.             ,[Instrument_ID]   int not null
  33.             ,[Date]            date
  34.             ,[Time]            datetime
  35.             ,[Name]            varchar(255)
  36.             ,[OperationNo]     bigint
  37.             ,[DocName]         varchar(512)
  38.             ,[DocNumber]       varchar(512)
  39.             ,[DocDate]         datetime
  40.             ,[QuantityBefore]  decimal(35,10)
  41.             ,[Quantity]        decimal(35,10)
  42.             ,[QuantityAfter]   decimal(35,10)
  43.             ,[isDebet]         bit
  44.             ,[Account]         varchar(13)
  45.             ,[Corr_Account_ID] int
  46.             ,[Corr_Account]    varchar(13)
  47.             ,[C_Person_ID]     int
  48.             ,[RemindQuantity]  decimal(35,10)
  49.             ,Primary Key ([ID],[Account_ID])
  50.         )
  51.  
  52.     -- Credit side
  53.     Insert into #tbl_AccountDepoByPeriod_Operation
  54.         (
  55.              [ID]
  56.             ,[Date]
  57.             ,[Time]
  58.             ,[Name]
  59.             ,[OperationNo]
  60.             ,[DocName]
  61.             ,[DocNumber]
  62.             ,[DocDate]
  63.             ,[Instrument_ID]
  64.             ,[Quantity]
  65.             ,[isDebet]
  66.             ,[SubAccount_ID]
  67.             ,[Account_ID]
  68.             ,[Account]
  69.             ,[Corr_Account_ID]
  70.             ,[Corr_Account]
  71.             ,[C_Person_ID]
  72.         )
  73.         Select   d_o.ID                   [ID]
  74.                 ,d_o.DepoDate             [Date]
  75.                 ,d_o.OperationDateTime    [Time]
  76.                 ,d_ot.TypeName            [Name]
  77.                 ,Cast(Stuff( d_o.OperationNo
  78.                             ,1,2,''
  79.                            ) as bigint
  80.                      )                    [OperationNo]
  81.                 ,d_o.DocName              [DocName]
  82.                 ,d_o.DocNumber            [DocNumber]
  83.                 ,d_o.DocDate              [DocDate]
  84.                 ,d_o.Instrument_ID        [Instrument_ID]
  85.                 ,d_o.Quantity             [Quantity]
  86.                 ,0                        [isDebet]
  87.                 ,d_sa_c.ID                [SubAccount_ID]
  88.                 ,d_sa_c.DepoAccount_ID    [Account_ID]
  89.                 ,NULL                     [Account]
  90.                 ,isNull( d_a_d.ID
  91.                         ,c_st_d.ID
  92.                        )                  [Corr_Account_ID]
  93.                 ,isNull( d_a_d.Account
  94.                         ,c_st_d.Account
  95.                        )                  [Corr_Account]
  96.                 ,isNull( c_c_d.Person_ID
  97.                         ,c_st_d.Person_ID
  98.                        )                  [C_Person_ID]
  99.             From     #tbl_AccountDepoByPeriod_pre_Operation  c
  100.                 Join [Depo].[Operation]              d_o     on c.ID = d_o.ID
  101.             -- CreditAccount
  102.                 Join [Depo].[SubAccount]             d_sa_c  on d_sa_c.ID = d_o.CreditSubAccount_ID
  103.                 Left join [Depo].[OperationType]          d_ot    on d_ot.ID = d_o.OperationType_ID
  104.  
  105.             -- Debet DepoAccount_ID
  106.                 Left join [Depo].[SubAccount]        d_sa_d  on d_sa_d.ID = d_o.DebetSubAccount_ID
  107.                 Left join [Depo].[Account]           d_a_d   on d_a_d.ID  = d_sa_d.DepoAccount_ID
  108.                 Left join [Core].[Client.Agreement]  c_ca_d  on c_ca_d.ID = d_a_d.[Client.Agreement_ID]
  109.                 Left join [Core].[Client]            c_c_d   on c_c_d.ID  = c_ca_d.Client_ID
  110.             -- Debet Storage
  111.                 Left join [Core].[StorageSubAccount] c_ssa_d on c_ssa_d.ID = d_o.DebetStorageSubAccount_ID
  112.                 Left join [Core].[Storage]           c_st_d  on c_st_d.ID  = c_ssa_d.Storage_ID
  113.  
  114.     -- Debet side
  115.     Insert into #tbl_AccountDepoByPeriod_Operation
  116.         (
  117.              [ID]
  118.             ,[Date]
  119.             ,[Time]
  120.             ,[Name]
  121.             ,[OperationNo]
  122.             ,[DocName]
  123.             ,[DocNumber]
  124.             ,[DocDate]
  125.             ,[Instrument_ID]
  126.             ,[Quantity]
  127.             ,[isDebet]
  128.             ,[SubAccount_ID]
  129.             ,[Account_ID]
  130.             ,[Account]
  131.             ,[Corr_Account_ID]
  132.             ,[Corr_Account]
  133.             ,[C_Person_ID]
  134.         )
  135.         Select   d_o.ID
  136.                 ,d_o.DepoDate          [Date]
  137.                 ,d_o.OperationDateTime [Time]
  138.                 ,d_ot.TypeName         [Name]
  139.                 ,Cast(Stuff( d_o.OperationNo
  140.                             ,1,2,''
  141.                            ) as bigint
  142.                      )                 [OperationNo]
  143.                 ,d_o.DocName           [DocName]
  144.                 ,d_o.DocNumber         [DocNumber]
  145.                 ,d_o.DocDate           [DocDate]
  146.                 ,d_o.Instrument_ID     [Instrument_ID]
  147.                 ,d_o.Quantity          [Quantity]
  148.                 ,1                     [isDebet]
  149.                 ,d_sa_d.ID             [SubAccount_ID]
  150.                 ,d_sa_d.DepoAccount_ID [Account_ID]
  151.                 ,NULL                  [Account]
  152.                 ,isNull( d_a_c.ID
  153.                         ,c_st_c.ID
  154.                        )               [Corr_Account_ID]
  155.                 ,isNull( d_a_c.Account
  156.                         ,c_st_c.Account
  157.                        )               [Corr_Account]
  158.                 ,isNull( c_c_c.Person_ID
  159.                         ,c_st_c.Person_ID
  160.                        )               [C_Person_ID]
  161.             From     #tbl_AccountDepoByPeriod_pre_Operation c
  162.                 Join [Depo].[Operation]              d_o     on c.ID = d_o.ID
  163.             -- Debet DepoAccount_ID
  164.                 Join [Depo].[SubAccount]             d_sa_d  on d_sa_d.ID = d_o.DebetSubAccount_ID
  165.                 Left join [Depo].[OperationType]     d_ot    on d_ot.ID = d_o.OperationType_ID
  166.  
  167.             -- CreditAccount
  168.                 Left Join [Depo].[SubAccount]        d_sa_c  on d_sa_c.ID = d_o.CreditSubAccount_ID
  169.                 Left Join [Depo].[Account]           d_a_c   on d_a_c.ID  = d_sa_c.DepoAccount_ID
  170.                 Left join [Core].[Client.Agreement]  c_ca_c  on c_ca_c.ID = d_a_c.[Client.Agreement_ID]
  171.                 Left join [Core].[Client]            c_c_c   on c_c_c.ID  = c_ca_c.Client_ID
  172.                 -- Debet Storage
  173.                 Left join [Core].[StorageSubAccount] c_ssa_c on c_ssa_c.ID = d_o.CreditStorageSubAccount_ID
  174.                 Left join [Core].[Storage]           c_st_c  on c_st_c.ID = c_ssa_c.Storage_ID
  175.             Where Not Exists (select 1
  176.                                 from #tbl_AccountDepoByPeriod_Operation sub
  177.                                 where sub.[ID] = d_o.[ID]
  178.                                   and sub.[Account_ID] = d_sa_d.[DepoAccount_ID])
  179.  
  180.     Create Index IDX_tbl_AccountDepoByPeriod_Operation_Complex
  181.         on #tbl_AccountDepoByPeriod_Operation ([Account_ID], [Instrument_ID], [Time], [OperationNo])
  182.  
  183.     Create Index IDX_tbl_AccountDepoByPeriod_Operation_SubAccount_ID_Instrument_ID
  184.         on #tbl_AccountDepoByPeriod_Operation ([SubAccount_ID], [Instrument_ID])
  185.  
  186. Select  t.[Account_ID]
  187.                    ,t.[Instrument_ID]
  188.                    ,sum(Case when sub_t.[isDebet] = 1
  189.                              then -1
  190.                              else 1
  191.                         End * isNull(sub_t.[Quantity],0)
  192.                        ) [OperastionBefore]
  193.                 FROM #tbl_AccountDepoByPeriod_Operation t
  194.                     LEFT JOIN #tbl_AccountDepoByPeriod_Operation sub_t ON sub_t.[Account_ID] = t.[Account_ID]
  195.                         and sub_t.[ID] != t.[ID]
  196.                         and sub_t.[Instrument_ID] = t.[Instrument_ID]
  197.                         and sub_t.[time] <= t.[time]
  198.                         and sub_t.[OperationNo] < t.[OperationNo]
  199.                 Group by  t.[Account_ID]
  200.                          ,t.[Instrument_ID]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement