每年从不同的表中计算最新的小数位数

时间:2018-12-03 17:42:34

标签: tsql

如何从两个表中获取小数和。 表A仅保存当前年份值(按A.ID),表B保存表A对象的更改,包括本年度以前的更改。

Table A:
"ID"     >> int (PrimaryKey), 
"Date"   >> datetime,
"AValue" >> decimal 

Objects of A:
1 | 12/02/2018 | 10.000
2 | 11/25/2018 | 20.000

Table B:
"ID"       >> int (PrimaryKey), 
"AObjID"   >> int (ForeignKey to Table A.ID), 
"Date"     >> datetime, 
"BValue"   >> decimal

Objects of B:
 1 | 1 | 08/06/2018 | 9.000 
 2 | 1 | 12/15/2017 | 10.000  *
 3 | 1 | 10/18/2017 | 8.000
 4 | 1 | 12/09/2016 | 10.000 *
 5 | 1 | 11/11/2016 | 5.000
 6 | 2 | 05/21/2018 | 13.000 
 7 | 2 | 12/19/2017 | 20.000 **
 8 | 2 | 08/04/2017 | 15.000 
 9 | 2 | 05/13/2017 | 15.000
10 | 2 | 12/25/2016 | 20.000 **
11 | 2 | 02/21/2016 | 15.000
12 | 2 | 11/09/2015 | 20.000 **

结果应将当前年份的AValue和所有相关的BValue汇总在一起,应将每年的最新条目(当前的Year除外)进行汇总。

案例A.ID = 1 >>(结果= 30.000):

 A:      1 | 12/02/2018 | 10.000
 B:  2 | 1 | 12/15/2017 | 10.000 *
 B:  4 | 1 | 12/09/2016 | 10.000 *

案例A.ID = 2 >>(结果= 80.000):

 A:       2 | 11/25/2018 | 20.000
 B:   7 | 2 | 12/19/2017 | 20.000 **
 B:  10 | 2 | 12/25/2016 | 20.000 **
 B:  12 | 2 | 11/09/2015 | 20.000 **

仍在为此苦苦挣扎...任何帮助表示赞赏!谢谢你们。

2 个答案:

答案 0 :(得分:1)

declare @table_a table
(
    ID int,
    [Date] date,
    AValue decimal
);

declare @table_b table
(
    ID int,
    AObjID int,
    [Date] datetime,
    BValue decimal
);

insert into @table_a values
(1, '2018-12-02', 10.0),
(2, '2018-11-25', 20.0);

insert into @table_b values
(1, 1, '2018-08-06', 9.0),
(2, 1, '2017-12-15', 10.0),
(3, 1, '2017-10-18', 8.0),
(4, 1, '2016-12-09', 10.0),
(5, 1, '2016-11-11', 5.0),
(6, 2, '2018-05-21', 13.0),
(7, 2, '2017-12-19', 20.0),
(8, 2, '2017-08-04', 15.0),
(9, 2, '2017-05-13', 15.0),
(10, 2, '2016-12-25', 20.0),
(11, 2, '2016-02-21', 15.0),
(12, 2, '2015-11-09', 9.0);

with a_vals as (select null AID, ID, [Date], AValue, null rnum from @table_a where ID = 1)
select * from a_vals
union all
select * from
(
    select tb.ID, tb.AObjID, tb.[Date], tb.BValue,
           row_num = row_number() over (partition by tb.AObjID, year(tb.[Date])
                                        order by tb.[Date] desc)
    from @table_b tb inner join a_vals av on tb.AObjID = av.ID
        and year(tb.[Date]) < year(av.[Date])
) x
where x.row_num = 1
order by AID;

答案 1 :(得分:0)

我完成了另一笔CTE的工作,以获取总金额... @JohnyL:谢谢您让我理解! :)

declare @table_a table
(
    ID int,
    [Date] date,
    AValue decimal
);

declare @table_b table
(
    ID int,
    AObjID int,
    [Date] datetime,
    BValue decimal
);

insert into @table_a values
(1, '2018-12-02', 10.0),
(2, '2018-11-25', 20.0);

insert into @table_b values
(1, 1, '2018-08-06', 9.0),
(2, 1, '2017-12-15', 10.0),
(3, 1, '2017-10-18', 8.0),
(4, 1, '2016-12-09', 10.0),
(5, 1, '2016-11-11', 5.0),
(6, 2, '2018-05-21', 13.0),
(7, 2, '2017-12-19', 20.0),
(8, 2, '2017-08-04', 15.0),
(9, 2, '2017-05-13', 15.0),
(10, 2, '2016-12-25', 20.0),
(11, 2, '2016-02-21', 15.0),
(12, 2, '2015-11-09', 20.0);

with a_vals as (select null as AID, ID, [Date],AValue, null as rnum from @table_a where ID =1), 
sum_vals as (
    select * from a_vals
    union all
    select * from
    (
        select 
        tb.ID, 
        AID = tb.AObjID, 
        tb.[Date], 
        tb.BValue,
        row_num = row_number() over (partition by tb.AObjID, year(tb.[Date]) order by tb.[Date] desc)
        from @table_b tb 
        inner join a_vals av 
        on tb.AObjID = av.ID and year(tb.[Date]) < year(av.[Date])
    ) x
        where x.row_num = 1)
SELECT Sum(AValue) FROM sum_vals