• There are a couple of things about the original script that bring up questions. The primary one is that the values that will end up creating the column name cnr552.50M, appear to be one zero shy of the appropriate value in the PARAMETER column of the source data. Also, PARAMETE_VALUE could just be PARAMETER_VALUE, so I coded it that way, and supplied the missing zeros so that a quick transformation of the original data would allow for the use of PIVOT. As your sample data has no issues with the use of the aggregate because there are no records that would get missed by virtue of same, I'm confident that as long as your sample data is representative of your actual data in that respect, that the aggregate won't be a problem. The only other thing you have to worry about is having the same number of values for each group, and that you'll know the resulting column names in advance. If that latter constraint cannot be met, you'll need dynamic SQL, and this will be a fair chunk more complicated. Here's the simple PIVOT:

    WITH SOURCE_DATA (OMNI_NUMBER, [TIMESTAMP], DATA_TYPE, DATA_TYPE2, PARAMETER, PARAMETER_VALUE) AS (

    SELECT 506026,1413240436,'test_data','cnr',211250000,54.8 UNION ALL

    SELECT 506026,1413244259,'test_data','cnr',211250000,53.2 UNION ALL

    SELECT 506026,1413244679,'test_data','cnr',211250000,53.1 UNION ALL

    SELECT 506026,1413309646,'test_data','cnr',211250000,53.4 UNION ALL

    SELECT 506026,1413315987,'test_data','cnr',211250000,53 UNION ALL

    SELECT 506026,1414519090,'test_data','cnr',211250000,53.2 UNION ALL

    SELECT 506026,1414520072,'test_data','cnr',211250000,53.3 UNION ALL

    SELECT 506026,1413240436,'test_data','cnr',325250000,55 UNION ALL

    SELECT 506026,1413244259,'test_data','cnr',325250000,53.2 UNION ALL

    SELECT 506026,1413244679,'test_data','cnr',325250000,53.3 UNION ALL

    SELECT 506026,1413309646,'test_data','cnr',325250000,53.2 UNION ALL

    SELECT 506026,1413315987,'test_data','cnr',325250000,52.7 UNION ALL

    SELECT 506026,1414519090,'test_data','cnr',325250000,53.4 UNION ALL

    SELECT 506026,1414520072,'test_data','cnr',325250000,53.3 UNION ALL

    SELECT 506026,1413240436,'test_data','cnr',547250000,55.2 UNION ALL

    SELECT 506026,1413244259,'test_data','cnr',547250000,53.5 UNION ALL

    SELECT 506026,1413244679,'test_data','cnr',547250000,53.4 UNION ALL

    SELECT 506026,1413309646,'test_data','cnr',547250000,53.3 UNION ALL

    SELECT 506026,1413315987,'test_data','cnr',547250000,53.2 UNION ALL

    SELECT 506026,1414519090,'test_data','cnr',547250000,53.5 UNION ALL

    SELECT 506026,1414520072,'test_data','cnr',547250000,53.7 UNION ALL

    SELECT 506026,1413240436,'test_data','cnr',552500000,55 UNION ALL

    SELECT 506026,1413244259,'test_data','cnr',552500000,53.6 UNION ALL

    SELECT 506026,1413244679,'test_data','cnr',552500000,53.6 UNION ALL

    SELECT 506026,1413309646,'test_data','cnr',552500000,53.8 UNION ALL

    SELECT 506026,1413315987,'test_data','cnr',552500000,53.6 UNION ALL

    SELECT 506026,1414519090,'test_data','cnr',552500000,53.4 UNION ALL

    SELECT 506026,1414520072,'test_data','cnr',552500000,53.6

    ),

    TRANSFORMED_DATA AS (

    SELECT OMNI_NUMBER, [TIMESTAMP], DATA_TYPE, DATA_TYPE2 + LEFT(CAST(PARAMETER/1000000. AS varchar(12)),6) + 'M' AS COLUMN_NAME,

    PARAMETER, PARAMETER_VALUE

    FROM SOURCE_DATA

    )

    SELECT OMNI_NUMBER, [TIMESTAMP], DATA_TYPE,

    MAX([cnr211.25M]) AS [cnr211.25M], MAX([cnr325.25M]) AS [cnr325.25M],

    MAX([cnr547.25M]) AS [cnr547.25M], MAX([cnr552.50M]) AS [cnr552.50M]

    FROM TRANSFORMED_DATA

    PIVOT (MAX(PARAMETER_VALUE) FOR COLUMN_NAME IN ([cnr211.25M], [cnr325.25M], [cnr547.25M], [cnr552.50M])) AS P

    GROUP BY OMNI_NUMBER, [TIMESTAMP], DATA_TYPE

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)