Sql server 具有分组依据的动态列值(sql server和Linq)

Sql server 具有分组依据的动态列值(sql server和Linq),sql-server,linq-to-sql,group-by,aggregate-functions,Sql Server,Linq To Sql,Group By,Aggregate Functions,我有一个带有以下样本数据的表计划 我希望按PlanMonth汇总结果,对于PlanStatus,我希望如果其任何(在组中)值为Drafted我将在结果中起草,否则将在批准中起草。我已经使用下面的查询完成了 select PlanMonth, case when Flag=1 then 'Drafted' else 'Under Approval' end as PlanStatus from (select p.PlanMonth, Max(CASE WHEN p.PlanStatus =

我有一个带有以下样本数据的表
计划


我希望按PlanMonth汇总结果,对于PlanStatus,我希望如果其任何(在组中)值为
Drafted
我将在结果中起草,否则将在批准中起草
。我已经使用下面的查询完成了

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

我咨询过。有什么问题吗?此外,如果有人能帮我把它翻译成linq,我将不胜感激

您的查询将有效

使用您提供的示例数据,可以将其简化一点

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

如果在
PlanStatus
中有值,并且在
起草之前按字母顺序排序,则此操作将不起作用

您的查询将起作用

使用您提供的示例数据,可以将其简化一点

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

如果在
PlanStatus
中有值,并且在
起草之前按字母顺序排序,则此操作将不起作用

下面的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();

下面的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();

@Mikiael我需要文本表示,如
已起草
正在审批中
您的查询将给我1或0@MuhammadAdeelZahid如果您的示例数据是您所说的,则不会。@Mikiael我需要文本表示,如
已起草
正在审批中
您的查询将给我1或0@MuhammadAdeelZahid如果你的示例数据就是您所说的。