Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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/1/angular/31.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行数类函数_Sql Server - Fatal编程技术网

Sql server SQL行数类函数

Sql server SQL行数类函数,sql-server,Sql Server,我想知道是否有一种方法可以在SQL Server脚本中执行以下操作 假设我有下表 **为了简单起见,示例中的ID是INT。在我的实际场景中,它们是UNIQUEIDENTIFIER ParentId ChildId ----------------------- 101 201 101 202 101 203 102 204 102 2

我想知道是否有一种方法可以在SQL Server脚本中执行以下操作

假设我有下表

**为了简单起见,示例中的ID是INT。在我的实际场景中,它们是UNIQUEIDENTIFIER

ParentId     ChildId   
-----------------------
101          201        
101          202       
101          203       
102          204        
102          205     
103          206       
103          207      
103          208        
103          209       
我想查询该表以获得以下结果

到目前为止,我能够使用ROW_NUMBER()函数获得ChildIndex列。我现在正在与ParentIndex专栏作斗争

ParentId     ChildId    ChildIndex    ParentIndex
---------------------------------------------------
101          201        1             1 
101          202        2             1
101          203        3             1
102          204        1             2
102          205        2             2
103          206        1             3
103          207        2             3
103          208        3             3
103          209        4             3 
这是我目前的疑问

SELECT ParentId,
       ChildId,
       ROW_NUMBER() OVER ( PARTITION BY ParentId ORDER BY ParentId DESC ) AS ChildIndex
FROM   MyTable
这就是你所需要的

DENSE_RANK() OVER (ORDER BY ParentId DESC) AS ParentIndex

谢谢我之前尝试过,但我在查询中保留了partitionby子句。。。这就是它不起作用的原因。