[英]sql server select percent from table and all data from another table
i have two tables (families) and (children ) that related by family id ... every family has more than one child .. i use percentage to select families from families table and i want to know count of children to each family selected ... but when i use percentage the program return children count by percentage ... so how can i select percentage from families and all children that related to families ... my query is 我有两个表(家庭)和(孩子),这两个表按家庭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
You are going to need to take the top families, and THEN do the join 您将需要接受顶级家庭,然后加入
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.