在SQL中获取货币汇率

时间:2018-02-06 04:52:12

标签: sql-server tsql

使用SQL Server,我有......

AccDocumentItems表:

CurId

我正在尝试在sql中编写一个按Cnt分组的查询,然后从0 Cnt列中减去Debit的{​​{1}}列。{{1} }} 0,例如,行13CurId 1,行1的{​​{1}}大于零,然后我们有Debit,第4行到第6行有(2-4+5)=3 CurId然后我们2最后我想在(4-2+2)=4表中找到3*Last ExchangeRate的总和,以及ExchangeRates表中的4*Last ExchangeRate,然后是ExchangeRates的总和。

ExchangeRates表:

(3*Last ExchangeRate+4*Last ExchangeRate)
  

结果=(3 *最后CurrencyId EffectiveDate ExchangeRate ------------------------------------------------ 1 2016-1-1 1000 2 2016-1-2 1200 2 2016-1-3 2000 1 2016-1-4 1500 + 4 *最后ExchangeRate) - ((ExchangeRate之和) - (总和   Debit))

在此示例中,Credit 1的上一个ExchangeRate为1500,CurrencyId 2的上一个ExchangeRateCurrencyId,最后我想要此结果

结果=(3 * 1500 + 4 * 2000) - (15000-8000)

3 个答案:

答案 0 :(得分:3)

此解决方案基于您的计算,仅适用于2个CurId。

declare @cur_1 int = 1
declare @cur_2 int = 2

;with AccDocumentItems as (
    select
        *
    from
        (values 
            ('S1','D1','D4',2000,0,1,1000,2)
            ,('S1','D1','D4',0,6000,1,1500,4)
            ,('S1','D1','D4',6000,0,1,1200,5)
            ,('S2','D2','D4',4000,0,2,1000,4)
            ,('S2','D2','D4',0,2000,2,1000,2)
            ,('S2','D2','D4',3000,0,2,1500,2)
        ) t (SLId, DL1Id, DL2Id, Debit, Credit, CurId, ExRate, Cnt)
)
, ExchangeRates as (
    select
        *
    from
        (values
            (1,'2016-1-1',1000)
            ,(2,'2016-1-2',1200)
            ,(2,'2016-1-3',2000)
            ,(1,'2016-1-4',1500)
        ) t (CurrencyId, EffectiveDate, ParityRate)
)

select
    sum(t.cnt * q.ParityRate) - sum(diff)
from 
    (
        select
            CurId, diff = sum(Debit) - sum(Credit),  cnt = sum(cnt * case when Debit = 0 then -1 else 1 end)
        from
            AccDocumentItems
        where
            CurId in (@cur_1, @cur_2)
        group by CurId
    ) t
    join (
        select
            top 1 with ties *
        from
            ExchangeRates
        order by row_number() over (partition by CurrencyId order by EffectiveDate desc)
    ) q on t.CurId = q.CurrencyId

答案 1 :(得分:1)

此解决方案适用于All CurId。

Declare @Result int
Declare @DeCr   int
Declare @Cur    int

Set     @DeCr = (
                    Select  SUM(Debit)-SUM(Credit)
                    From    dbo.AccDocumentItems
                )
Set     @Cur = 
                (
                    Select   Sum(ParityRate * Zarib)
                    From
                    (
                        Select  ExchangeRates.CurrencyId as cuur,ParityRate
                        From
                            (
                                Select  Max(EffectiveDate) As MaxDate,CurrencyId
                                From    dbo.ExchangeRates
                                Group by CurrencyId
                            )As Table1
                        left join
                            dbo.ExchangeRates
                        ON  table1.currencyid = ExchangeRates.CurrencyId
                        And Table1.MaxDate  = EffectiveDate
                    ) As Table3
                    inner join
                    (
                        Select  Curid
                        ,       sum(cnt*signs) As Zarib
                        From
                        (
                            Select  CurId
                            ,       Cnt
                            ,       Case
                                        When Debit = 0 Then -1
                                        Else                1
                                    End As Signs
                            From    dbo.AccDocumentItems
                        ) As Table2
                        Group by Curid
                    )  As Table4
                    On  Table3.cuur = Table4.Curid
                )

Select  @Cur - @DeCr

答案 2 :(得分:0)

Declare @Result int
Declare @DeCr   int
Declare @Cur    int

Set     @DeCr = (
                    Select  SUM(Debit)-SUM(Credit)
                    From    dbo.AccDocumentItems
                )
Set     @Cur = 
                (
                    Select   Sum(ParityRate * Zarib)
                    From
                    (
                        Select  ExchangeRates.CurrencyId as cuur,ParityRate
                        From
                            (
                                Select  Max(EffectiveDate) As MaxDate,CurrencyId
                                From    dbo.ExchangeRates
                                Group by CurrencyId
                            )As Table1
                        left join
                            dbo.ExchangeRates
                        ON  table1.currencyid = ExchangeRates.CurrencyId
                        And Table1.MaxDate  = EffectiveDate
                    ) As Table3
                    inner join
                    (
                        Select  Curid
                        ,       sum(cnt*signs) As Zarib
                        From
                        (
                            Select  CurId
                            ,       Cnt
                            ,       Case
                                        When Debit = 0 Then -1
                                        Else                1
                                    End As Signs
                            From    dbo.AccDocumentItems
                        ) As Table2
                        Group by Curid
                    )  As Table4
                    On  Table3.cuur = Table4.Curid
                )

set @result = @Cur - @DeCr
Select @result