每组一只一条
人员分组表
组编号 组员姓名 组员信息1 组员信息2 。。。001 张三 汉族 团员
CREATE TABLE test ([组编号] varchar(10),[组员姓名] varchar(10),[组员信息1] varchar(10),[组员信息2]varchar(10) )INSERT INTO testSELECT '001', '张三','汉族', '团员'UNION ALL SELECT '001', '张三1','汉族', '团员'UNION ALL SELECT '002', '张三','汉族', '团员'UNION ALL SELECT '002', '张三1','汉族', '团员'SELECT * FROM (SELECT NTILE(10) OVER(PARTITION BY [组编号],[组员信息1],[组员信息2] ORDER BY [组编号])id,* FROM test WHERE [组员信息1]='汉族' AND [组员信息2]='团员')bWHERE id=1id 组编号 组员姓名 组员信息1 组员信息2-------------------- ---------- ---------- ---------- ----------1 001 张三 汉族 团员1 002 张三 汉族 团员(2 行受影响)
[解决办法]
select ID from 人员分组表 a where ID =
(
select top 1 ID from 人员分组表 b where b.姓名='张三' and b.ID = a.ID
)
[解决办法]
用NTILE有风险吧?万一有某组满足条件的有11人的话.用NTILE岂不查多了?
SELECT * FROM (
SELECT row_number() OVER(PARTITION BY [组编号] ORDER BY [组编号]) id,* FROM test WHERE [组员信息1]='汉族' AND [组员信息2]='团员' ......
)b
WHERE id=1