我正在尝试计算表联接中表的列标记为“ A”,“ B”或“ C”的记录数。我还需要它返回不存在任何行的计数0(因此我需要A,B和C每次都返回其相对计数)。
我的查询
Select t1.Num, t2.Flag, count(*)
from t1, t2
where t1.id = t2.id
and t2.flag in ('A','B','C')
group by t1.Num, t2.flag
这当然只会返回以下内容,因为不存在“ C”的记录
Num Flag Count
1 A 3
1 B 2
有什么办法让它像下面的C一样返回0吗?
Num Flag Count
1 A 3
1 B 2
0 C 0
答案 0 :(得分:1)
外部加入之类的声音应该会出现在游戏中:
var propertyNames = typeof(INotPersistingProperties).GetProperties()
.Select(p => p.Name)
.ToList();
var entityTypes = modelBuilder.Model.GetEntityTypes()
.Where(t => typeof(INotPersistingProperties).IsAssignableFrom(t.ClrType));
foreach (var entityType in entityTypes)
{
var entityTypeBuilder = modelBuilder.Entity(entityType.ClrType);
foreach (var propertyName in propertyNames)
entityTypeBuilder.Ignore(propertyName);
}
答案 1 :(得分:0)
代替(in
),您应该使用left join
。您似乎已经有一个join
,因此查询如下:
Select t1.Num, f.Flag, count(t1.id)
from (select 'A' as flag from dual union all
select 'B' as flag from dual union all
select 'C' as flag from dual
) f left join t2
on t2.flag = f.flag left join
t1
on t1.id = t2.id
group by t1.Num, f.flag;