SQL QUERY MERGE两行数据

时间:2013-11-01 12:03:13

标签: sql sql-server-2008 merge

假设我的数据库是这样的:

MemberName  MemberID   ResultsEligibilityID
Thuso         2              1
Thuso         2              1
Maubane       3              2
Maubane       3              1
CDeveloper    5              2
CDeveloper    5              2

现在可以根据以下内容编写一个显示(The Below输出)的查询:

如果单个成员的ResultsEligibilityID都是1,那么Eligibile,  否则不合格。

输出

MemberName  MemberID  ResultsEligibilityID  Results
Thuso        2              1             Eligible
Maubane      3              2             Non-Eligible
CDeveloper   5              2             Non-Eligible

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

请尝试:

select 
    MemberName,
    MemberID, 
    MAX(ResultsEligibilityID) ResultsEligibilityID ,
    (case when sum(case when ResultsEligibilityID=1 then 1 else 0 end)= COUNT(*) 
    then 'Eligible' else 'Non-Eligible' end) Results
From 
    YourTable
group by MemberName,MemberID