如何使用同一列创建数据透视查询

时间:2018-05-11 22:52:22

标签: sql sql-server pivot

我有一个看起来像这样的表:

enter image description here

我希望使用此数据创建一个数据透视图,并使其看起来像enter image description here

注意模型如何是值线1和修订值第2行,值是值第3行,换句话说,model = test_key_id 23,revision = test_key_id 25和value = test_key_id 24 我能够提出这个问题:

BEGIN
        --this is a copy of the query above just for troubleshooting purposes
        with t1 as (
            SELECT test_recipe_id, LTRIM(RTRIM(value)) as model_num , row_number() over (order by id ) rn
            FROM [leakOpportunities].[dbo].[test_recipe_value] WHERE test_key_id in(23)
        ),
        t2 as (
            SELECT  test_recipe_id,LTRIM(RTRIM(value)) as value, row_number() over (order by id ) rn
            FROM [leakOpportunities].[dbo].[test_recipe_value] WHERE test_key_id in(24)
        ),
        t3 as (
            SELECT  test_recipe_id,LTRIM(RTRIM(value)) as revision, row_number() over (order by id ) rn
            FROM [leakOpportunities].[dbo].[test_recipe_value] WHERE test_key_id in(25)
        ),
        t4 as (
            SELECT  id,LTRIM(RTRIM(value)) as autobrazeError1, row_number() over (order by id ) rn
            FROM [leakOpportunities].[dbo].[test_recipe_value] WHERE test_key_id in(1)
        ),
        t5 as (
            SELECT  id,LTRIM(RTRIM(value)) as autobrazeError2, row_number() over (order by id ) rn
            FROM [leakOpportunities].[dbo].[test_recipe_value] WHERE test_key_id in(2)
        )

        SELECT  model_num,revision,value, value-(CAST((value) AS INT) - CAST((autobrazeError1) AS INT)-CAST((autobrazeError2) AS INT)),CAST((value) AS INT) - CAST((autobrazeError1) AS INT)-CAST((autobrazeError2) AS INT)
        FROM t1 full outer join t2 on t1.rn = t2.rn
        full outer join t3 on t3.rn = t1.rn
        full outer join t4 on t4.rn = t1.rn
        full outer join t5 on t5.rn = t1.rn
        PRINT 'No Records inserted as the insert record was set to false' 
    END

请在我的查询中注意我做一些额外的数学并添加一些额外的东西。此查询不起作用,因为输入数据的方式并非所有记录都具有使得“rn”不是比较它们的安全方式的所有值。对于他们中的大多数人来说,这种方法有效,但存在一些差异。我也尝试使用“test_recipe_id”列作为匹配,但我也有一些数据差异。

有人可以帮助我或指出我如何做到正确的方向,或者可能是一个支点查询

1 个答案:

答案 0 :(得分:1)

您不需要使用单个CTE来使用ID上的行号来获取值。

简单的GROUP BY就足够了,以后也不需要JOIN。

请参阅以下查询:

; with t1 as
(
      select 
        model_num=max(case when test_key_id =23 then LTRIM(RTRIM(value)) end),
        revision =max(case when test_key_id =25 then LTRIM(RTRIM(value)) end),
        [value]  =max(case when test_key_id =24 then LTRIM(RTRIM(value)) end),
        autobrazeError1= max(case when test_key_id =1 then LTRIM(RTRIM(value)) end),
        autobrazeError2= max(case when test_key_id =2 then LTRIM(RTRIM(value)) end)
       from [leakOpportunities].[dbo].[test_recipe_value] 
      group by test_recipe_id
)

select 
    model_num,
    revision,
    [value],
    [value]
        - ISNULL(CAST([value] AS INT),0) 
        - ISNULL(CAST(autobrazeError1 AS INT),0)
        - ISNULL(CAST(autobrazeError2 AS INT),0),
    ISNULL(CAST([value] AS INT),0)
        - ISNULL(CAST(autobrazeError1 AS INT),0)
        - ISNULL(CAST(autobrazeError2 AS INT),0)
from t1