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_Aggregate Functions - Fatal编程技术网

Sql 元组的重复频率

Sql 元组的重复频率,sql,sql-server,aggregate-functions,Sql,Sql Server,Aggregate Functions,对于上面的表,我想提取ID与日期之间的差异作为另一列出现的频率。我的意思是输出应该是: ID Date 7019730 16-03-2015 7019721 16-03-2015 7020250 16-03-2015 7219750 08-04-2015 7019730 22-03-2015 7019721 25-03-2015 7019730 28-03-2

对于上面的表,我想提取ID与日期之间的差异作为另一列出现的频率。我的意思是输出应该是:

ID              Date
7019730         16-03-2015
7019721         16-03-2015
7020250         16-03-2015
7219750         08-04-2015
7019730         22-03-2015
7019721         25-03-2015
7019730         28-03-2015
7019721         30-03-2015

这里有一种方法。假设输入表名为
D

declare@d1表(x-bigint,y-bigint,id-int,dt-date);
在d1处插入(x,y,id,dt)
挑选
(订单编号,[日期])x上的行号(),
(按id排序,[日期])-1上的行号()y,
身份证件
[日期]
从…起
D
订购人
id,[日期];
选择*
从(
选择
a、 身份证,
a、 dt,
发生时(按a.id分区,按a.id排序)上的行号(),
合并(datediff(day,b.dt,a.dt),0)作为频率
来自@d1 a
左外连接@d1 b
关于a.y=b.x
a.id=b.id
)结果
按发生顺序排列,id
ID              Date          Occurrence    Frequency
7019730         16-03-2015          1       0
7019721         16-03-2015          1       0
7020250         16-03-2015          1       0  
7219750         08-04-2015          1       0
7019730         22-03-2015          2       6
7019721         25-03-2015          2       9
7019730         29-03-2015          3       7
7019721         30-03-2015          3       5