2011-12-13 52 views
1

我有一个表Plan与下面的示例数据
enter image description here
我想要聚合通过PlanMonth和PlanStatus我想要的结果,如果任何的(在一个组)值是Drafted我得到起草的结果,否则Under Approval。我曾尝试使用下面的查询动态列值由(SQL Server和LINQ)

select PlanMonth, case when Flag=1 then 'Drafted' else 'Under Approval' end as PlanStatus 
from 
(select p.PlanMonth, Max(CASE WHEN p.PlanStatus = 'Drafted' THEN 1 ELSE 0 END) Flag 
from Plans p 
group by p.PlanMonth 
) inquery 

我已经咨询this blog post做到了。它有什么问题吗?此外,如果有人能帮助我把它翻译到LINQ我会很感激

回答

1

你必须将工作的查询。

随着你提供它可以简化一个位的采样数据。如果您有Drafted之前按字母顺序排序在PlanStatus

select p.PlanMonth, 
     min(p.PlanStatus) as PlanStatus 
from Plans as p 
group by p.PlanMonth 

这是行不通的。

+0

@Mikiael我需要一个像'Drafted'文字表述和'approval' UR查询下会给我1或0 –

+0

@MuhammadAdeelZahid若您的样本数据是你说是的。 –

0

继Linq查询为我工作

return (from p in db.mktDrVisitPlans       

        group p by p.PlanMonth into grop 
        select 
        new VMVisitPlan 
        { 
         PlanMonth = grop.Key 
         PlanStatus = grop.Any(x=>x.p.PlanStatus == "Drafted")?"Hold":"Under Approval", 

        }).ToList();