2012-04-13 45 views
1

我有表和數據如下,使用需要幫助的更新語句,如果

declare @tApplyProgram table (myID varchar(50), programID varchar(10), stTR char(1) null) 
insert into @tApplyProgram(myID,programID) values('1925','184'); 
insert into @tApplyProgram(myID,programID) values('4474','172'); 
insert into @tApplyProgram(myID,programID) values('8890','172'); 
insert into @tApplyProgram(myID,programID) values('5578','172'); 
insert into @tApplyProgram(myID,programID) values('2980','172'); 
insert into @tApplyProgram(myID,programID) values('2500','172'); 
insert into @tApplyProgram(myID,programID) values('1925','180'); 
insert into @tApplyProgram(myID,programID) values('5578','180'); 
/* 
@tApplyProgram keep applicant and their programID 
myID and programID is unique 
*/ 

declare @tResult table (myID varchar(50), subjectCd varchar(50)) 
insert into @tResult values('1925','01') 
insert into @tResult values('1925','02') 
insert into @tResult values('1925','03') 
insert into @tResult values('4474','03') 
insert into @tResult values('4474','04') 
insert into @tResult values('4474','05') 
insert into @tResult values('5578','01') 
insert into @tResult values('5578','02') 
insert into @tResult values('5578','03') 
insert into @tResult values('2980','01') 
insert into @tResult values('2980','02') 
/* 
@tResult keep their applicant's result 
myID and subjectCd is unique 
*/ 


declare @tRulesD table (programID varchar(50), subjectCd varchar(50)) 
insert into @tRulesD values('172','05') 
insert into @tRulesD values('172','02') 
insert into @tRulesD values('172','15') 
insert into @tRulesD values('184','01') 
insert into @tRulesD values('184','02') 
insert into @tRulesD values('184','03') 
/* 
@tRulesD keep programID rules and regulation 
programID and subjectCd is unique 
*/ 

如果要應用programID(@tApplyProgram),以滿足要求(@tRulesD),設置STTR = 1。如果不符合要求,請設置stTR = 0。否則,離開它

預期結果如下所示爲NULL,

myID | programID | stTR 
------------------------------------ 
1925 184   1   /*1925 have rows in @tResult, and 184 have rows in @tRulesD. And, it's meet the requirements */ 
4474 172   0   /*4474 have rows in @tResult, and 172 have rows in @tRulesD. But, it's not meet the requirements */ 
8890 172   NULL  /*8890 don't have rows in @tResult*/ 
5578 172   0   /*5578 have rows in @tResult, and 172 have rows in @tRulesD. But, it's not meet the requirement*/ 
2980 184   0   /*2980 have rows in @tResult, and 184 have rows in @tRulesD. But. it's not meet the requirement*/ 
2500 172   NULL  /*2500 don't have rows in @tResult*/ 
1925 180   NULL  /*180 don't have rows in @tRulesD*/ 
5578 180   NULL  /*180 don't have rows in @tRulesD*/ 

真正需要幫助建立了T-SQL。我堅持

+0

爲什麼4474,172不符合要求?結果中有('172','05')和('4474','05')。如果要求主體之間的平等,他們應該有資格。 – 2012-04-13 09:17:27

+0

哦,你需要所有的subjectCd匹配。 – 2012-04-13 09:46:01

回答

0

這裏是更新的子查詢的版本。它的工作方式是將結果中的子查詢和給定myid和programid的規則分別與主觀列中的完整外連接相連接。然後測試此結果集的空值(未命中)。如果有錯誤,max將返回1;如果所有值匹配,則返回值將爲0.這對雙方都有效。如果沒有錯過總和將給0,並且我們返回1. 如果我們發現錯過,我們仍然需要確定每列的所有值是否爲空。我們通過使用count測試非空值來完成它;如果找到,count會給1,如果沒有,它會給零。如果雙方至少有一個非空值,我們知道規則和結果都有匹配的行,所以我們返回0.如果不是,則返回值爲空。

update @tApplyProgram 
set stTR = 
(
    select 
    case when max (case when u.subjectcd is null then 1 else 0 end) 
      + max (case when r.subjectcd is null then 1 else 0 end) 
      = 0 
     then 1 
     else case when count (u.subjectcd) > 0 
        and count (r.subjectcd) > 0 
        then 0 
        else null 
       end 

    end 
    from (select * from @tResult r where r.myid = [@tApplyProgram].myid) r 
    full outer join 
     (select * from @tRulesD u where u.ProgramID = [@tApplyProgram].programid) u 
    on r.subjectCd = u.subjectCd 
) 
0

這種查詢可以幫助你,我只是不明白什麼叫「不符合要求」是指:

select myID, programID, 
     case 
      when (
        (select case WHEN COUNT(*)>1 then 1 else null end from @tResult where myid=p.myid) + 
        (select case WHEN COUNT(*)>1 then 1 else null end from @tRulesD where programid=p.programid) 
       )>1 then 1 else null end 
from @tApplyProgram P