日期:2014-05-17  浏览次数:20473 次

求助 sql语句 求大师帮助!


有如上表
想得到结果
医疗证号    户主姓名 户人口数
02140001   金百忠    3
02140002   金嘉懿    1
02140003   高明      1

------解决方案--------------------

select a.*,b.[户主姓名] from
(
select [医疗证号],count(*) as 户人口数
from [表名]
group by [医疗证号]
) a inner join
(
  select *
  from [表名] where [户主姓名] is not null
) b on a.[医疗证号]= b.[医疗证号]

------解决方案--------------------
select a.医疗证号,a.户主姓名,b.户口人数
from
(select 医疗证号,户主姓名 from table where 户主姓名 is not null) a
join (select 医疗证号 count(姓名) 户口人数 from table group by 医疗证号) b
on a.医疗证号=b.医疗证号