I have XML files will engineering test data in them. There are n possible parameter definitions for any given XML file, but the parameter definitions are of a fixed construct.
<ParameterDef>
<ParameterDefId>1</ParameterDefId>
<Name>name</Name>
<Description>desc</Description>
<LowLimit>0.00E+00</LowLimit>
<HighLimit>1.00E+03</HighLimit>
<SpecLowLimit>-1.00E+32</SpecLowLimit>
<SpecHighLimit>1.00E+32</SpecHighLimit>
</ParameterDef>
I created a table for the parameter definitions and insert my n parameter definitions into a table.
ParameterDefId | Name | Description | LowLimit | HighLimit | SpecLowLimit | SpecHighLimit |
---|---|---|---|---|---|---|
1 | Test_One | Test_One_Desc | 0.00E+00 | 1.00E+03 | -1.00E+32 | 1.00E+32 |
2 | Test_Two | Test_Two_Desc | 2.00E-15 | 2.00E-10 | -1.00E+32 | 1.00E+32 |
For reasons, I need to pull these n parameter definitions into a single row. I understand I can use a PIVOT for this in theory, but I’m getting the selected values returned mostly NULL:
SELECT [1], [2], [3], [4], [5], [6], [7], [8]
FROM (
SELECT ParameterDefId, [Name], LowLimit, HighLimit, SpecLowLimit, SpecHighLimit
FROM dbo.ParameterDefinitions def
) dat
PIVOT(
MAX(LowLimit) -- Max(HighLimit), etc
FOR ParameterDefId IN ([1], [2], [3], [4], [5], [6], [7], [8])
)pvt
If possible, I would like to alias my columns when I pivot so that each column name is unique for the row from which it came: ie, Test_One_LowLimit, Test_One_HighLimit, etc. I can’t seem to find a way to programmatically make the alias.
I can’t be the first person to need something like this, but I’m new to PIVOTs and I’m not entirely sure I know what I’m doing (which is probably obvious).
I would prefer to avoid d-sql, but I’m aware that might not be possible; but I am still not sure how to formulate the query I need in d-sql.
To be clear, this is what I'm after:
ParentTestData | Test_One_LowLimit | Test_One_HighLimit | etc | Test_Two_Lowlimit | Test_Two_HighLimit |
---|---|---|---|---|---|
something | 0.00E+00 | 1.00E+03 | etc | 2.00E-15 | 2.00E-10 |
I may have the wrong idea of what PIVOT does, but in that case I still need a way to get to this.
Edit 1: It's SQL Server 11 if that's relevant.
Edit 2: It's clear from some comments in my attempt to be concise, I have not communicated clearly what I'm looking for.
I have two tables currently. One has test data in it. One has the parameter definitions of those tests. The parameter definitions for a given test is a variable number of definitions. The parameter definitions table I'm pulling from has a defined structure.
Now, when I pull the data from these two tables to feed to an external piece of software I don't control, I need to provide it all of the data for a given test as one row, regardless how many entries a given test has in the parameter definitions table.
I don't need to keep this. I don't need to store it anywhere; the storage happens before this part of the process is even relevant. I just need to know how to pull the data from the tables I have and put it in the format I need it in.
If there are alternatives to PIVOT or this can be accomplished using a handful of CTEs (or even a cursor as a last resort), I'm fine with that and open to suggestions.
As long as the final product can be easily understood and easy to maintain, I don't particularly care how it's accomplished. I'm asking about a PIVOT because that's my understanding of how to turn rows into columns in SQL server.
What I'm trying to avoid is having to create a table for every product and test the clients have or invent along with all the associated plumbing and maintenance. This will be written into a SPROC, so a series of queries to get the end result is fine.
If there's still confusion about what I'm trying to accomplish, please let me know how to communicate with you more clearly what my end goal is. Thanks.