如何计算2楼的Jane人数(不包括1楼的人)?
+------+---------+----------+
|Index | Name | Floor |
+------+---------+----------+
| 1 | Sally | 1 |
| 2 | Sue | 1 |
| 3 | Fred | 1 |
| 4 | Wally | 2 |
| 5 | Tommy | 2 |
| 6 | Jane | 2 |
| 7 | Bart | 2 |
| 8 | Sam | 3 |
+------+---------+----------+
预期结果为2,因为在2楼的Jane之前有2个人(Wally和Tommy)。
我尝试使用CHARINDEX从我生成的临时表中查找行号,但这似乎不起作用:
SELECT CHARINDEX('Jane', Name) as position
INTO #test
FROM tblExample
WHERE Floor = 2
select ROW_NUMBER() over (order by position) from #test
WHERE position = 1
答案 0 :(得分:2)
我认为一个简单的row_number()
就可以解决问题
Select Value = RN-1
From (
Select *
,RN = row_number() over (partition by [floor] order by [index])
From YourTable
Where [Floor]=2
) A
Where [Name]='Jane'
答案 1 :(得分:0)
您可以这样做:
const result = [];
result.length = 2;
function combine(input, len, start) {
if (len === 0) {
console.log(result.join(" x "));
return;
}
for (var i = start; i <= input.length - len; i++) {
result[result.length - len] = input[i];
combine(input, len - 1, i + 1);
}
}
const Names = ['Name1', 'Name2', 'Name3', 'Name4', 'Name5'];
combine(Names, result.length, 0);
使用<div class="Generator">
<div id="button"></div>
<button id="button1">Generate</button>
</div>
上的索引,我希望它比select count(*)
from t
where t.floor = 2 and
t.id < (select t2.id from t t2 where t2.name = 'Jane' and t2.floor = 2);
快。