Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL server-列中的累积值_Sql Server - Fatal编程技术网

Sql server SQL server-列中的累积值

Sql server SQL server-列中的累积值,sql-server,Sql Server,这是我的原始查询: declare @symbol1 varchar(50) = 'D', @symbol2 varchar(50) = 'IN', @barDurationSeconds int = 60, @daysOfData int = 1 DECLARE @bars int = datediff(second, '2000-01-01 9:30', '2000-01-01 16:00') / @barDurationSeconds * @day

这是我的原始查询:

    declare @symbol1 varchar(50) = 'D',
    @symbol2 varchar(50) = 'IN',
    @barDurationSeconds int = 60,
    @daysOfData int = 1

    DECLARE @bars int = datediff(second, '2000-01-01 9:30', '2000-01-01 16:00') / @barDurationSeconds * @daysOfData;

    with t as (
        select
            datediff(second, '2000-01-01', time)/ @barDurationSeconds startOfBar,
            last,
            time,
            symbol
        from ticks (nolock)
        where symbol in (@symbol1, @symbol2)
        and time < dateadd(second, 5, getdate())
        and '9:30' <= cast(time as time) and cast(time as time) <= '16:00'
    ), m as (
        select
            (a.last + b.last) / 2 Last,
            a.startOfBar,
            a.time
        from (select * from t where t.Symbol = @symbol1) a
        join (select * from t where t.Symbol = @symbol2) b on a.time = b.time
    ), r as (
        select
            *,
            row_number() over (partition by startofbar order by time desc) r
        from m
    )


    select top (@bars)
        convert(varchar(max), dateadd(second, startofbar * @barDurationSeconds, '2000-01-01'), 126) + 'Z' Time,
        last [Close],
        avg([last]) over (order by startofbar desc rows between current row and 20 following) SMA
    from r where r = 1
    order by startofbar desc
做-

将[Close]值显示为先前值的累积和,因此我修改了上述查询,如下所示:

Select IDENTITY(int, 1,1) AS Id, t1.Time, t1.[Close], t1.SMA
into new
from
(
select top (@bars)
    convert(varchar(max), dateadd(second, startofbar * @barDurationSeconds, '2000-01-01'), 126) + 'Z' Time,
    last [Close],
    avg([last]) over (order by startofbar desc rows between current row and 20 following) SMA
from r where r = 1
order by startofbar desc
) t1


select new.Id, new.Time, new.[Close], SUM(t2.[Close]) as sum from new
 INNER JOIN
        new t2
        ON new.Id =  t2.Id
group by new.Id, new.Time, new.[Close]
order by new.id
它在两个Close列中显示相同的值,并且不在SUMt2中继续添加前面的值。[Close]

预期产出:

Time                        Close           sum
2018-06-12T16:00:00Z    26429505.5      26429505.5
2018-06-12T15:59:00Z    26519811.5      52949317
2018-06-12T15:58:00Z    25759386        78708703
2018-06-12T15:57:00Z    25278214.5      103986917.5
你能告诉我我做错了什么吗?

在结尾处使用带窗口的总和

declare @symbol1 varchar(50) = 'D',
@symbol2 varchar(50) = 'IN',
@barDurationSeconds int = 60,
@daysOfData int = 1

DECLARE @bars int = datediff(second, '2000-01-01 9:30', '2000-01-01 16:00') / @barDurationSeconds * @daysOfData;

with t as (
    select
        datediff(second, '2000-01-01', time)/ @barDurationSeconds startOfBar,
        last,
        time,
        symbol
    from ticks (nolock)
    where symbol in (@symbol1, @symbol2)
    and time < dateadd(second, 5, getdate())
    and '9:30' <= cast(time as time) and cast(time as time) <= '16:00'
), m as (
    select
        (a.last + b.last) / 2 Last,
        a.startOfBar,
        a.time
    from (select * from t where t.Symbol = @symbol1) a
    join (select * from t where t.Symbol = @symbol2) b on a.time = b.time
), r as (
    select
        *,
        row_number() over (partition by startofbar order by time desc) r
    from m
), topbars as (
        select top (@bars)
            convert(varchar(max), dateadd(second, startofbar * @barDurationSeconds, '2000-01-01'), 126) + 'Z' Time,
            last [Close],
            avg([last]) over (order by startofbar desc rows between current row and 20 following) SMA
        from r where r = 1
        order by startofbar desc
)
SELECT
    T.Time,
    T.[Close],
    [sum] = SUM(T.[Close]) OVER (ORDER BY CONVERT(DATETIME, T.Time) ASC)
FROM
    topbars AS T

只需在第一次查询结果中添加一个带有SUMClose OVER ORDER BY Time ASC的新列。获取错误-按范围窗口框架列表排序不能包含LOB类型的表达式。@user1254053您的时间是varcharmax,我已通过转换为datetime将其编辑为ORDER。虽然它正确地累积了值,但结果出现在,顶栏为。。。与此新查询不同,即我在原始查询中获得的输出与[Close]的此输出不同values@user1254053我刚刚在初始查询中添加了另一层来进行滚动求和。也许您可以先验证CTE的数据。仍然是相同的结果:之前的输出仍然与此更改不同。
declare @symbol1 varchar(50) = 'D',
@symbol2 varchar(50) = 'IN',
@barDurationSeconds int = 60,
@daysOfData int = 1

DECLARE @bars int = datediff(second, '2000-01-01 9:30', '2000-01-01 16:00') / @barDurationSeconds * @daysOfData;

with t as (
    select
        datediff(second, '2000-01-01', time)/ @barDurationSeconds startOfBar,
        last,
        time,
        symbol
    from ticks (nolock)
    where symbol in (@symbol1, @symbol2)
    and time < dateadd(second, 5, getdate())
    and '9:30' <= cast(time as time) and cast(time as time) <= '16:00'
), m as (
    select
        (a.last + b.last) / 2 Last,
        a.startOfBar,
        a.time
    from (select * from t where t.Symbol = @symbol1) a
    join (select * from t where t.Symbol = @symbol2) b on a.time = b.time
), r as (
    select
        *,
        row_number() over (partition by startofbar order by time desc) r
    from m
), topbars as (
        select top (@bars)
            convert(varchar(max), dateadd(second, startofbar * @barDurationSeconds, '2000-01-01'), 126) + 'Z' Time,
            last [Close],
            avg([last]) over (order by startofbar desc rows between current row and 20 following) SMA
        from r where r = 1
        order by startofbar desc
)
SELECT
    T.Time,
    T.[Close],
    [sum] = SUM(T.[Close]) OVER (ORDER BY CONVERT(DATETIME, T.Time) ASC)
FROM
    topbars AS T