As given in below image I am having two table I want to insert record in Table 2 and map LicenseID from Table 1. but I want to select LicenseID depending on ComapanyCount and Count of records in Table 2 for that LicenseID. Means I can Map LicenseID 9 With 5 records in Table 2 after 5 record it should give me next LicenseID that is 12 which I can map with 3 record in table 2.As given in below image I am having two table I