2011-12-13 51 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();