Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 计算字段以标识共享相同ID的数据点的时间顺序_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql server 计算字段以标识共享相同ID的数据点的时间顺序

Sql server 计算字段以标识共享相同ID的数据点的时间顺序,sql-server,sql-server-2008,tsql,Sql Server,Sql Server 2008,Tsql,我使用Microsoft SQL Server 2008尝试确定数据点的时间顺序,以便创建一个筛选器字段,该字段允许我创建一个查询,该查询仅包括每个ID号的第一条和最后一条记录,其中多行表示来自同一ID的不同数据点 以下是我当前数据和所需数据的示例,以便更好地理解我的意思: 当前数据 ID Indicator Date 1 1 1988-02-11 1 1 1989-03-9 1 1

我使用Microsoft SQL Server 2008尝试确定数据点的时间顺序,以便创建一个筛选器字段,该字段允许我创建一个查询,该查询仅包括每个ID号的第一条和最后一条记录,其中多行表示来自同一ID的不同数据点

以下是我当前数据和所需数据的示例,以便更好地理解我的意思:

当前数据

ID    Indicator        Date
1       1           1988-02-11
1       1           1989-03-9
1       1           1993-04-3
1       1           2001-05-4
2       1           2000-01-01
2       1           2001-02-03
2       1           2002-04-22
3       1           1990-02-01
3       1           1998-02-01  
3       1           1999-03-02
3       1           2000-04-02
4       0               NA
ID    Indicator    Date        Order_Indicator
1       1       1988-02-11        1
1       1       1989-03-9         2
1       1       1993-04-3         3
1       1       2001-05-4         4
2       1       2000-01-01        1
2       1       2001-02-03        2
2       1       2002-04-22        3
3       1       1990-02-01        1
3       1       1998-02-01        2
3       1       1999-03-02        3
3       1       2000-04-02        4
4       0       NULL             NULL
所需数据

ID    Indicator        Date
1       1           1988-02-11
1       1           1989-03-9
1       1           1993-04-3
1       1           2001-05-4
2       1           2000-01-01
2       1           2001-02-03
2       1           2002-04-22
3       1           1990-02-01
3       1           1998-02-01  
3       1           1999-03-02
3       1           2000-04-02
4       0               NA
ID    Indicator    Date        Order_Indicator
1       1       1988-02-11        1
1       1       1989-03-9         2
1       1       1993-04-3         3
1       1       2001-05-4         4
2       1       2000-01-01        1
2       1       2001-02-03        2
2       1       2002-04-22        3
3       1       1990-02-01        1
3       1       1998-02-01        2
3       1       1999-03-02        3
3       1       2000-04-02        4
4       0       NULL             NULL
我要创建的字段是“所需数据”表中的“Order_Indicator”字段,并且只有相关记录是Indicator=1的记录。有了这些信息,我将创建一个查询,其中我只为共享同一ID的每个“行组”选择Order_Indicator=1和Order_Indicator=MAX(Order_Indicator)的行。有人知道我该怎么做吗?我知道我可以在Excel中很容易地做到这一点,但我需要在SQL server上做到这一点,以便与我的同事一起重复


提前非常感谢

您可以通过排名功能来实现这一点:

select c.*,
       (case when indicator = 1
             then row_number() over (partition by id, indicator order by [date])
        end) as OrderIndicator
from current c
这将根据日期和指示器分配一个序列号。case语句处理指示符=0的情况

顺便说一下,这假设“日期”被存储为日期。

使用以下查询:

select YourTable.ID,
       YourTable.indicator,
       case when date<>'NA' then date end as date,
       case when indicator = 1 then row_number() over (partition by id, indicator order by ID) end as Order_Indicator
from YourTable
选择YourTable.ID,
你的桌子。指示器,
如果日期为“NA”,则日期结束为日期,
如果指示符=1,则(按id划分,按id排序的指示符)上的行号()以顺序指示符结尾
从你的桌子上