I've tables and data as following,declare @tStudent table (applicantIdx int, gender char(1), category char(1))insert into @tStudent values(100, 'm', 'A')insert into @tStudent values(102, 'm', 'B')insert into @tStudent values(101, 'f', 'C')insert into @tStudent values(104, 'm', 'B')insert into @tStudent values(103, 'f', 'B')insert into @tStudent values(109, 'm', 'D')/*applicantIdx is a primary keygender only contain values m or fcategory only contain values A, B, C, or D*/declare @tQuota table (idx int, iptsIdx int, kursusIdx int, mQuota_A int, fQuota_A int, mQuota_B int, fQuota_B int, mQuota_C int, fQuota_C int, mQuota_D int, fQuota_D int)insert into @tQuota values(1999, 120, 300, 30, 44, 29, 77, 55, 39, 34, 89)insert into @tQuota values(1923, 200, 300, 90, 23, 34, 45, 55, 90, 20, 50)insert into @tQuota values(4488, 87, 38, 390, 229, 298, 23, 57, 58, 12, 8)/*idx is a primary keyiptsIdx, and kursusIdx is a uniquemQuota_A is a male quota for A categoryfQuota_A is a female quota for A categorymQuota_B is a male quota for B categoryfQuota_B is a female quota for B categoryand so on ...*/declare @tAccepted table (applicantIdx int, tQuotaIdx int)insert into @tAccepted values(104, 1923);insert into @tAccepted values(102, 1999);insert into @tAccepted values(101, 4488);/*@tAccepted save the quota was taken@tAccepted(applicantIdx) is a foreign key to @tStudent(applicantIdx)applicantIdx is a unique*/
I want to built SQL to display the number of remaining quotaMy expected result for the number of remaining quota as following,the number of remaining quotaidx | mQuota_A | fQuota_A | mQuota_B | fQuota_B | mQuota_C | fQuota_C | mQuota_D | fQuota_D-------------------------------------------------------------------------------------------------------1999 30 44 28 77 55 39 34 891923 90 23 33 45 55 90 20 504488 390 229 298 23 57 57 12 8
Need help