Currently we are working with Northwind DB.
In the Master Query, we have the query as below
SELECT
[Customers].[CustomerID],
[Customers].[CompanyName],
[Customers].[City],
[Customers].[Region],
[Customers].[Country],
[Orders].[OrderDate],
[Orders].[Freight],
[OrderDetails].[UnitPrice],
[OrderDetails].[Quantity],
[OrderDetails].[Discount],
[Shippers].[CompanyName],
[Products].[ProductName],
[Categories].[CategoryName]
FROM
[Customers]
INNER JOIN [Orders]
ON
[Customers].[CustomerID]=[Orders].[CustomerID]
INNER JOIN [OrderDetails]
ON
[Orders].[OrderID]=[OrderDetails].[OrderID]
INNER JOIN [Shippers]
ON
[Orders].[ShipVia]=[Shippers].[ShipperID]
INNER JOIN [Products]
ON
[OrderDetails].[ProductID]=[Products].[ProductID]
INNER JOIN [Categories]
ON
[Products].[CategoryID]=[Categories].[CategoryID]
Now, in the child query the user is selecting only 2 fields
Categories.CategoryName, Orders.Freight
Then, we have to remove the unnecessary tables like Customers, Shippers and generate the query as below
SELECT
[Categories].[CategoryName],
[Orders].[Freight]
FROM
[Orders]
INNER JOIN [OrderDetails]
ON
[Orders].[OrderID]=[OrderDetails].[OrderID]
INNER JOIN [Products]
ON
[OrderDetails].[ProductID]=[Products].[ProductID]
INNER JOIN [Categories]
ON
[Products].[CategoryID]=[Categories].[CategoryID]
How can we make this in a automatic way using any code or approach