简体   繁体   English

sql server从表中选择百分比,并从另一个表中选择所有数据

[英]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.

相关问题 从SQL Server中的表中选择数据时出错 - Error in in select data from table in SQL Server 从另一个表中选择数据SQL Select查询 - Selecting Data from another table SQL Select Query 需要将整个表从SQL Server复制到SQL Server CE,包括表结构和所有数据 - Need to copy an entire table from SQL Server to SQL Server CE including the table structure and all data 从SQL Server表中选择NULL值 - Select NULL values from SQL Server table 将表从一个SQL Server复制到另一个 - Copying table from one SQL Server to another 如何从SQL Server中的表中循环数据以及如何使用返回的数据查询另一个表。 请参见 - How to loop data from a table in sql server and using the returned data to query another table. Please see C#SQL从表中选择一个数据 - C# SQL Select a data from table 如何将数据从特定表中的一列复制到SQL Server中另一表中的相同命名列 - How to copy data from one column in certain table to same named column in another table in SQL Server 尝试从文本框中将数据放入SQL Server数据库中,并从C#中的另一张表中放入一些数据 - Trying to put data into a SQL Server database from textbox and some data from another table in C# 从一个表中选择所有列,从另一表中选择1列 - Select all columns from one table and 1 column from another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM