Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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 - Fatal编程技术网

如何在SQL中计算字段中具有较低值的记录?

如何在SQL中计算字段中具有较低值的记录?,sql,sql-server,Sql,Sql Server,我想这样说: select AnswerText, count(select * from @temp b where b.Ordinal < a.Ordinal) as Idx from @temp a 选择 答复文本, 计数(从@temp b中选择*其中b.序数

我想这样说:

select 
    AnswerText, 
    count(select * from @temp b where b.Ordinal < a.Ordinal) as Idx 
from @temp a
选择
答复文本,
计数(从@temp b中选择*其中b.序数

当然,我不能将
select
语句放入
count
函数中。那么,我如何才能获得所有具有较低
序数值的记录的计数呢?还是有更好的方法来确定列表中记录的索引?

将计数移动到子查询:

select AnswerText,
       (select count(*) from @temp b where b.Ordinal < a.Ordinal) as Idx
from @temp a;
select t.*,
       row_number() over (order by ordinal) as idx
from @temp t;