让我说我有以下sql表:
id item_count texture_count
30 82 205
30 133 406
35 253 658
30 552 1004
35 234 475
35 119 288
30 104 115
36 594 1422
如何在保留原始列收集的同时,修改选择查询以返回带有附加列的数据,该列告诉我每个ID的出现次数?这将导致以下结果:
id item_count texture_count id_count
30 82 205 4
30 133 406 4
35 253 658 3
30 552 1004 4
35 234 475 3
35 119 288 3
30 104 115 4
36 594 1422 1
查询是这样
SELECT
CAST ((2 * FLOOR(idvalue / 2)) AS INT) as id, -- im creating an id based on this operation, currently id value is a float
item_count,
texture_count
FROM
db
答案 0 :(得分:3)
如果数据库支持窗口功能,则可以使用它们。您所需要的就像窗口count
一样简单:
select
t.*,
count(*) over(partition by id) id_count
from mytable