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
*/

要件 (@tRulesD) を満たすために programID が適用されている (@tApplyProgram) 場合は、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 を構築するには本当に助けが必要です。私は立ち往生しています

4

2 に答える 2

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
于 2012-04-13T09:49:36.820 に答える
0

これは、更新のサブクエリ バージョンです。結果からのサブクエリと、myid と programid がそれぞれ与えられたルールを、subjectid 列による完全な外部結合で接続することによって機能します。次に、この結果セットの null (ミス) がテストされます。ミスがある場合、max は 1 を返します。すべての値が一致する場合、戻り値は 0 になります。これは両側で有効です。ミスがない場合、sum は 0 になり、1 を返します。ミスが見つかった場合でも、列ごとのすべての値が null かどうかを判断する必要があります。count を使用して null 値ではないかどうかをテストすることでそれを行います。見つかった場合、count は 1 を返し、そうでない場合は 0 を返します。両側に null 以外の値が少なくとも 1 つある場合、ルールと結果の両方に一致する行があることがわかっているため、0 を返します。そうでない場合、戻り値は null です。

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
)
于 2012-04-13T14:31:04.247 に答える