Кажется, что ваши исключения и включения смешаны. Группа 3 включена в столбец «Клиенты» и строку 18, но группа 4 не включена в таблицу «Клиенты». Включите в свою таблицу m_Customers только те, у кого есть доступ, или только те, для которых есть ограничения - но не оба.
Обычно проще и проще управлять изменением структуры данных, чтобы m_Customers использовал запись для каждой группы.
MetadataID Field Type Access
1 Customers table Group1
2 Customers table Group2
3 Customers table Group3
4 MiddleInitial column Group1
5 MiddleInitial column Group3
6 18 row Group1
7 18 row Group3
Вот два варианта; Вы можете проверить производительность.
SELECT MAX(col.customerID) AS customerID, MAX(col.FirstName) AS FirstName, MAX(col.MiddleInitial) AS MiddleIntitial, MAX(col.LastName) AS LastName
FROM Customers AS c
INNER JOIN m_Customers AS mRow ON mRow.[Type] = 'row' AND mRow.CustomerID = c.Field AND mRow.Access = @group
LEFT JOIN (
SELECT
IIF(mCol.Field = 'customerID', c.customerID, NULL) AS customerID,
IIF(mCol.Field = 'Firstname', c.FirstName, NULL) AS FirstName,
IIF(mCol.Field = 'MiddleInitial', c.FirMiddleInitialstName, NULL) AS MiddleInitial,
IIF(mCol.Field = 'LastName', c.LastName, NULL) AS LastName
FROM Customers AS c
INNER JOIN m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'MiddleInitial' AND mCol.Access = @group
) AS col ON col.customerID = c.CustomerID
GROUP BY col.customerID, col.FirstName, col.MiddleInitial, col.LastName
или
SELECT customerID.customerID, FirstName.FirstName, MiddleInitial.MiddleInitial, LastName.LastName
FROM Customers AS c
INNER JOIN m_Customers AS mRow ON mRow.[Type] = 'row' AND mRow.CustomerID = c.Field AND mRow.Access = @group
LEFT JOIN (
SELECT c.customerID
FROM Customers AS c
INNER JOIN m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'customerID' AND mCol.Access = @group
) AS customerID ON customerID.customerID = c.customerID
LEFT JOIN (
SELECT c.customerID, c.FirstName
FROM Customers AS c
INNER JOIN m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'FirstName' AND mCol.Access = @group
) AS FirstName ON FirstName.customerID = c.customerID
LEFT JOIN (
SELECT c.customerID, c.MiddleInitial
FROM Customers AS c
INNER JOIN m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'MiddleInitial' AND mCol.Access = @group
) AS MiddleInitial ON MiddleInitial.customerID = c.customerID
LEFT JOIN (
SELECT c.customerID, c.LastName
FROM Customers AS c
INNER JOIN m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'LastName' AND mCol.Access = @group
) AS LastName ON LastName.customerID = c.customerID
Рассмотрите возможность изменения названия вашего столбца с Типа на незарезервированное слово.