SQL数据透视表生成重复项

时间:2011-10-12 17:18:40

标签: database sql-server-2005 duplicates pivot-table

开发,

我是透视表的新手,并且在重复时遇到了一些问题。我的表,在旋转之前看起来像这样:

位置 |的食品
田纳西州|梨
田纳西州|橙色
佛罗里达|橙色
佛罗里达|苹果
弗吉尼亚|梨

以下是pivot的代码,工作正常:

SELECT PivotTable.location, [apple], [orange], [pear]
FROM
(SELECT location, food FROM someTable) as inventory
PIVOT
(COUNT(inventory.food) FOR inventory.location IN ([apple],[orange],[pear])) AS PivotTable

这会产生如下输出:

位置 | Apple | 橙色 |的
田纳西州| 0 | 1 | 1
佛罗里达| 1 | 1 | 0
弗吉尼亚| 0 | 0 | 1

正如我所说,工作正常。但是,我在原始表中添加了新的注释列,如下所示:

位置 | 食物 | apple_comments | orange_comments |的 pear_comments

田纳西州|梨| NULL | NULL | NULL
田纳西州|橙色| NULL |很多汁NULL
佛罗里达|橙色| NULL | NULL | NULL
佛罗里达|苹果|脆| NULL | NULL
弗吉尼亚|梨| NULL | NULL |好吃的

以下是我更改的数据透视表以说明评论:

SELECT PivotTable.location, [apple], [apple_comments], [orange], [orange_comments], [pear], [pear_comments]
FROM
(SELECT location, food, apple_comments, orange_comments, pear_comments FROM someTable) as inventory
PIVOT
(COUNT(inventory.food) FOR inventory.location IN ([apple],[orange],[pear])) AS PivotTable

这会产生如下输出:

位置 | Apple | apple_comments | 橙色 | Orange_comments | |的 Pear_comments
田纳西州| 0 | NULL | 0 | NULL | 1 | NULL
田纳西州| 0 | NULL | 1 |很多汁0 | NULL
佛罗里达| 0 | NULL | 1 | NULL | 0 | NULL
佛罗里达| 1 |脆| 1 | NULL | 0 | NULL
弗吉尼亚| 0 | NULL | 1 | NULL | 1 |可口

因此,基本上,当为每个有多个位置的条目添加注释时,它会创建一个重复的行。在弗吉尼亚州的情况下,只有一个条目,所以行结果很好。

似乎我需要做另一个支点或其他什么。任何人都可以就我出错的地方提供建议吗?

对不起。所需的输出应如下所示:

位置 | Apple | apple_comments | 橙色 | Orange_comments | |的 Pear_comments
田纳西州| 0 | NULL | 1 |很多汁1 | NULL
佛罗里达| 1 |脆| 1 | NULL | 0 | NULL
弗吉尼亚| 0 | NULL | 1 | NULL | 1 |可口

基本上,将重复项合并为一行。

感谢。

2 个答案:

答案 0 :(得分:0)

根本问题在于,除了comment列之外,您已经有效地告诉编译器按food列进行分组。有一些解决方案,例如将注释汇总到分隔列表中,如下所示:

Select location
    , Sum( Case When S.food = 'Apple' Then 1 Else 0 End ) As Apple
    , Stuff(
        (
        Select ', ' + S1.Apple_Comments
        From SomeTable As S1
        Where S1.location = S.location
            And S1.Apple_Comments Is Not Null
        Group By S1.Apple_Comments
        For Xml Path(''), type
        ).value('.','nvarchar(max)')
        , 1, 2, '') As Apple_Comments
    , Sum( Case When S.food = 'Orange' Then 1 Else 0 End ) As Orange
    , Stuff(
        (
        Select ', ' + S1.Orange_Comments
        From SomeTable As S1
        Where S1.location = S.location
            And S1.Orange_Comments Is Not Null
        Group By S1.Orange_Comments
        For Xml Path(''), type
        ).value('.','nvarchar(max)')
        , 1, 2, '') As Orange_Comments
    , Sum( Case When S.food = 'Pear' Then 1 Else 0 End ) As Pear
    , Stuff(
        (
        Select ', ' + S1.Pear_Comments
        From SomeTable As S1
        Where S1.location = S.location
            And S1.Pear_Comments Is Not Null
        Group By S1.Pear_Comments
        For Xml Path(''), type
        ).value('.','nvarchar(max)')
        , 1, 2, '') As Pear_Comments
From SomeTable As S
Group By S.location

答案 1 :(得分:0)

找到答案(利用'with CTE'和MAX函数):

;With CTE as (
SELECT PivotTable.location, [apple], [apple_comments], [orange], [orange_comments], [pear], [pear_comments]
FROM
(SELECT location, food, apple_comments, orange_comments, pear_comments FROM someTable) as inventory
PIVOT
(COUNT(inventory.food) FOR inventory.location IN ([apple],[orange],[pear])) AS PivotTable)
select location, MAX([apple]) as [apple], MAX([apple_comments]) as [apple_comments],MAX([orange]) as [orange], 
MAX([orange_comments]) as [orange_comments], MAX([pear]) as [pear], MAX([pear_comments]) as [pear_comments]
from CTE group by location