对不同记录的PIVOT查询

时间:2018-11-19 10:49:21

标签: sql-server pivot

我有下表:

------------------------------------------------------
| Id    Code  percentage  name  name1   activity     |
-----------------------------------------------------
| 1   Prashant  43.43    James  James_  Running      |
| 1   Prashant  70.43    Sam    Sam_    Cooking      |
| 1   Prashant  90.34    Lisa   Lisa_   Walking      |
| 1   Prashant  0.00     James  James_  Stealing     |
| 1   Prashant  0.00     James  James_  Lacking      |
| 1   Prashant  73       Sam     Sam_   Cooking 1    |
------------------------------------------------------

1)问题是,由于具有MAX函数,它正在忽略列名Name1为0.00的{​​{1}}值

2)预期结果:

Lacking

PIVOT查询我尝试了什么:

-------------------------------------------------------------------
Id  Code        James    James_  Sam        Sam_    Lisa      Lisa_
-------------------------------------------------------------------
1   Prashant    Running  43.43  Cooking     3.43    Walking   90.34
1   Prashant    Stealing 0.0    Cooking 1   73      NULL      NULL
1   Prashant    Lacking  0.0    NULL        NULL    NULL      NULL
-------------------------------------------------------------------

示例查询以生成数据:

DECLARE @DynamicPivotQuery NVARCHAR(MAX)

SET @DynamicPivotQuery  = N'SELECT Id,Code,James,James_,Sam,Sam_,Lisa,Lisa_
    INTO ##TempPivot 
    FROM A
    PIVOT(MAX(activity)
          FOR name IN (James,Sam,Lisa)) AS PVTTable PIVOT
          (
          MAX(percentage)
          FOR name1 IN (James_,Sam_,Lisa_)) AS PVTTable1'


EXECUTE(@DynamicPivotQuery) 

SELECT * 
INTO #RESULT 
FROM ##TempPivot


SELECT * 
FROM #RESULT

1 个答案:

答案 0 :(得分:2)

如果将ROW_NUMBER()添加到组合中,则数据透视表将能够保留活动和百分比之间的关联。

attributes.projects

返回:

;with cte as 
(
    select *, ROW_NUMBER() over (partition by name order by percentage desc) ROWNUM
    from A
),
cte2 as
(
    SELECT Id,Code,ROWNUM,James,James_,Sam,Sam_,Lisa,Lisa_
    FROM cte
    PIVOT(MAX(activity)
          FOR name IN (James,Sam,Lisa)) AS PVTTable PIVOT
          (
          MAX(percentage)
          FOR name1 IN (James_,Sam_,Lisa_)) AS PVTTable1
)
select Id, Code, MAX(James) James, MAX(James_) James_, MAX(Sam) Sam, MAX(Sam_) Sam_, MAX(Lisa) Lisa, MAX(Lisa_) Lisa_
from cte2
group by Id, Code, ROWNUM

这个想法是,在第一个公用表表达式中,将A表转换为此:

Id  Code        James       James_  Sam         Sam_    Lisa    Lisa_
1   Prashant    Running     43.43   Cooking 1   73      Walking 90.34
1   Prashant    Stealing    0.00    Cooking     3.43    NULL    NULL
1   Prashant    Lacking     0.00    NULL        NULL    NULL    NULL

在其余的查询中,ROWNUM列仅用于将百分比值绑定到活动。

在您有一个有效的查询后,使其变得动态起来很容易。只需用变量替换所有动态部分(在这种情况下为逗号分隔的名称列表,对吧?)。像这样:

Id  Code        percentage  name    name1   activity    ROWNUM
1   Prashant    43.43       James   James_  Running     1
1   Prashant    0.00        James   James_  Stealing    2
1   Prashant    0.00        James   James_  Lacking     3
1   Prashant    90.34       Lisa    Lisa_   Walking     1
1   Prashant    73          Sam     Sam_    Cooking 1   1
1   Prashant    3.43        Sam     Sam_    Cooking     2