Dynamic SQL

Team -

I have a table with the below split.

First Table: RawData
First Column: ReportID
Second Column to TenthColumn: Customer Levels (Ex: Customer_Level1, Customer_Level2..., Customer_Level9)
Same goes for Organization for the next 9 columns and the next 9 columns for Products Level (9 levels)
Then I will few columns for values / calculations. i.e., ShippedOrders, BackOrders etc.,

Ex: of Raw Data attached.

My 2nd Table is a Criteria Table
First Column: Unique identifier
Second Column: ReportID
Third Column: KeyName
FourthColumn: KeyType
FifthColumn: KeyLevel
(Ex: Check the attachment for sample)  Test-EE-v1.xlsx

What i need to do is..,

Create an output table with the below columns

How I can get this...,

Criteria table will have, lets say 2 customer numbers each with their levels and 2 org numbers with their levels and 2 products with their levels.,

From the raw data, I need to check for every organization from the criteria table & create a table like below
OrgNumber1 | Product1 | Customer1 | Sum(Shipments) | Sum(Backorder)
OrgNumber1 | Product1 | Customer2 | Sum(Shipments) | Sum(Backorder)
OrgNumber1 | Product2 | Customer1 | Sum(Shipments) | Sum(Backorder)
OrgNumber1 | Product2 | Customer2 | Sum(Shipments) | Sum(Backorder)
OrgNumber2 | Product1 | Customer1 | Sum(Shipments) | Sum(Backorder)
OrgNumber2 | Product1 | Customer2 | Sum(Shipments) | Sum(Backorder)
OrgNumber2 | Product2 | Customer1 | Sum(Shipments) | Sum(Backorder)
OrgNumber2 | Product2 | Customer2 | Sum(Shipments) | Sum(Backorder)

Here, the orgnumber1/2 or Product / Customer needs to be searched against the 9 levels in the raw data & then populate the output.

I am totally at a loss here as i couldnt even understand of how to get the data. Please help / guide me with a sample query.
ManjuIT - Project ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I would go back and redesign your tables to be relational.  This is not a properly designed structure.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ManjuIT - Project ManagerAuthor Commented:
Jerry - I agree however data format is out of my control.this is how I get the raw data from the client. I need to have some work around.
This is probably not possible in a SQL statement.

You might have write program to iterate through the data.  That's a mess.
ManjuIT - Project ManagerAuthor Commented:
I've requested that this question be deleted for the following reason:

Deleting this question as the solution doesnt seem to be possible in SQL. points to be refunded.
I provided an expert opinion that what the poster asked cannot be done as an SQL statement.  I provided an alternative method to get what he needs.  That IS an answer!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.