Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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存储过程_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

获取所有以前年份记录的连接字符串的SQL存储过程

获取所有以前年份记录的连接字符串的SQL存储过程,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一张有时间栏的桌子。我正在尝试创建一个存储过程SQL Server 2008,以便为每一行获取一个字符串,其中包含所有前几年的最后一个值和当前行值。例如,如果我有下表 _________________ time value _________________ mar-2009 1 _________________ may-2009 2 _________________ jan-2010 3 _________________ apr-2011

我有一张有时间栏的桌子。我正在尝试创建一个存储过程SQL Server 2008,以便为每一行获取一个字符串,其中包含所有前几年的最后一个值和当前行值。例如,如果我有下表

_________________ time value _________________ mar-2009 1 _________________ may-2009 2 _________________ jan-2010 3 _________________ apr-2011 4 _________________ feb-2011 5 _________________ jan-2012 6 _________________ 我试图得到以下结果

____________________________________________________ time value Result ____________________________________________________ mar-2009 1 "2009,1" ____________________________________________________ may-2009 2 "2009,2" ____________________________________________________ jan-2010 3 "2009,2,2010,3" ____________________________________________________ apr-2011 4 "2009,2,2010,3,2011,4" ____________________________________________________ feb-2011 5 "2009,2,2010,3,2011,5" ____________________________________________________ jan-2012 6 "2009,2,2010,3,2011,5,2012,6" ____________________________________________________
您需要执行一个简单的选择来获取所有相关记录,然后循环遍历所有记录,并将所需的输出连接成一个字符串。您可以在每个循环中输出累积的数据。
以下是示例,但出于性能目的,最好对原始表进行一些更改

-- first we need to extract months/years to get comparable and sortable values
-- otherwise we can't select "all previous years" and "last value".
;with converted as (
    select
        time,
        right(time, 4) as year,
        datepart(m, '2000-' + left(time, 3) + '-01') as month,
        right(time, 4) + ',' + convert(varchar(16), value) as concatenated,
        value
    from
        YourTableName --TODO: Replace this with your table name
)
select
    time,
    value,
    -- using xml for string concatenation
    isnull((
        select
            prev.concatenated + ',' as 'text()'
        from
            converted as prev
        where
            prev.year < main.year
            and prev.month = (select max(month) from converted lookup where lookup.year = prev.year)
        for
            xml path('')
    ),'')
    + main.concatenated
from
    converted as main