我有两个表(家庭)和(孩子)与家庭ID相关...每个家庭都有一个以上的孩子..我使用百分比从家庭表中选择家庭我想知道每个孩子的数量家庭选择...但当我使用百分比时,程序返回的孩子按百分比计算...所以如何从家庭和所有与家庭相关的孩子中选择百分比......我的查询是
select count(*) Children from
(select top('" + int.Parse(percentCb2.Text) + "') percent
f.family_id ,
f.economic_state
from families f inner join Children c on c.family_id = f.family_id
where f.economic_state = 'b')fq
group by fq.family_id
答案 0 :(得分:1)
你需要带顶级家庭,然后进行加入
SELECT COUNT(*) Children
FROM
(SELECT top('" + int.Parse(percentCb2.Text) + "') percent
f.family_id ,
f.economic_state
FROM families
WHERE f.economic_state = 'b') f
INNER JOIN Children c on c.family_id = f.family_id
GROUP BY fq.family_id