I’ve tables and data as following,
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
*/
If the programID being applied (@tApplyProgram) to meet requirements (@tRulesD), set stTR=1. If not meet requirements , set stTR=0. Else leave it to NULL
Expected results are shown as below,
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*/
Really need help to built the T-SQL. I’m stuck
Here is subquery version of update. It works by connecting subqueries from result and rules given myid and programid respectively with a full outer join by subjectid column. This resultset is then tested for nulls (misses). If there is a miss max will return 1; if all values are matched returned value will be 0. This is valid for both sides. If there are no misses sum will give 0, and we return 1.
If we found misses we still need to determine if all the values per a column are null. We do it by testing for not null value using count; if found, count will give 1, and if not it will give zero. If both sides have at least one not null value, we know that both rules and results have matching rows so we return 0. If not, return value is null.