开发者

Complicated AVG within date range

I've got a table with a tracking of a plant's equipment installation. Here is a sample:

ID  Name    Date    Percentage
1   GT-001  2011-01-08  30
2   GT-002  2011-01-11  40
3   GT-003  2011-02-02  30
4   GT-001  2011-02-03  50
5   GT-003  2011-02-15  50
6   GT-004  2011-02-15  30
7   GT-002  2011-02-15  60
8   GT-001  2011-02-20  60
9   GT-003  2011-03-01  60
10  GT-004  2011-03-05  50
11  GT-001  2011-03-10  70
12  GT-004  2011-03-15  60

And the corresponding script:

CREATE TABLE [dbo].[SampleTable](
[ID] [int] NOT NULL,
[Name] [nvarchar](50) NULL,
[Date] [date] NULL,
[Percentage] [int] NULL) ON [PRIMARY]
GO
--Populate the table with values
INSERT INTO [dbo].[SampleTable] VALUES
('1', 'GT-001', '2011-01-08', '30'), 
('2', 'GT-002', '2011-01-11', '40'), 
('3', 'GT-003', '2011-02-02', '30'), 
('4', 'GT-001', '2011-02-03', '50'), 
('5', 'GT-003', '2011-02-15', '50'), 
('6', 'GT-004', '2011-02-15', '30'), 
('7', 'GT-002', '2011-02-15', '60'), 
('8', 'GT-001', '2011-02-20', '60'), 
('9', 'GT-003', '2011-03-01', '60'), 
('10', 'GT-004', '2011-03-05', '50'), 
('11', 'GT-001', '2011-03-10', '70'), 
('12', 'GT-004', '2011-03-15', '60');
GO

What i need is to create a chart with Date on the X and Average Percentage on the Y. Average Percentage is an average percentage of all equipment by that particular date starting from the beggining of the installation process (MIN(Fields!Date.Value, "EquipmentDataset"))

Having no luck in implementing this using SSRS only, i decided to create a more complicated dataset for it using T-SQL.

I guess that it is nessesary to add a calculated column named 'Aver开发者_如何学运维agePercentage' that should store an average percentage on that date, calculating only the most latest equipment percentage values in a range between the beggining of the installation process (MIN(Date)) and the current row's date. Smells like a recursion, but i'm newbie to T-SQL....))

Here is the desired output

ID  Name    Date    Percentage Average
1   GT-001  2011-01-08  30  30
2   GT-002  2011-01-11  40  35
3   GT-003  2011-02-02  30  33
4   GT-001  2011-02-03  50  40
5   GT-003  2011-02-15  50  48
6   GT-004  2011-02-15  30  48
7   GT-002  2011-02-15  60  48
8   GT-001  2011-02-20  60  50
9   GT-003  2011-03-01  60  53
10  GT-004  2011-03-05  50  58
11  GT-001  2011-03-10  70  60
12  GT-004  2011-03-15  60  63

What do you think?

I'll be very appreciated for any help.


You could use cross apply with row_number to find the latest value for each machine. An additional subquery is required because you cannot use row_number in the where clause directly. Here's the query:

select  t1.id
,       t1.Name
,       t1.Date
,       t1.Percentage
,       avg(1.0*last_per_machine.percentage)
from    SampleTable t1
outer apply
        (
        select  *
        from    (
                select  row_number() over (partition by Name order by id desc)
                          as rn
                ,       *
                from    SampleTable t2
                where   t2.date <= t1.date
                ) as numbered
        where   rn = 1
        ) as last_per_machine
group by
        t1.id
,       t1.Name
,       t1.Date
,       t1.Percentage

Working example on SE Data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜