请教一个较复杂的查询问题
有一个医疗的数据库,里面是所有人的医疗记录
我想查询一段时间以内,一个人在同一个医院就诊多次的情况,并按医院来计数
这个查询我能做出来,但有一个问题,就是有的医院没有这种情况的,我在Linq里就显示不出来,而SSMS能(rightjoin很方便)
具体请看代码,SQL的:
select yymc,grbh,count(grbh)as'num' into temp6 --存到临时表
from nb_bxjl join NB_YYZD on NB_BXJL.BXD = NB_YYZD.YYBH
where czrq <='2012-03-31 23:59:59.999'
and czrq >='2012-01-01 00:00:00.000'
and glzf is null
and bxlx=2
and bxd in(select yybh from nb_yyzd where jgdm=2 )
group by yymc,grbh having count(grbh)>1
select NB_YYZD.yymc,sum(num) from temp6 right join NB_YYZD
on temp6.yymc=NB_YYZD.YYMC
where NB_YYZD.JGDM=2
group by NB_YYZD.yymc
--------------------------------------------------------------------
Linq的:
var con =
from b in db.NB_BXJL
join y in db.NB_YYZD on b.BXD equals y.YYBH
where y.YYDJ == "2"
&& b.GLZF.HasValue == false
&& b.BXRQ >= dateTimePickerBegin.Value
&& b.BXRQ <= dateTimePickerEnd.Value
&& b.BXLX == 2
group b by new { name = b.BXD, grbh = b.GRBH } into c
where c.Count() > 1--查出符合的人
from a in c
join h in db.NB_YYZD on a.BXD equals h.YYBH into z
from x in z.DefaultIfEmpty()
group x by x.YYMC;
foreach (var item in con)
{
DataRow dr = dt.NewRow();
dr[0] = item.Key;