透视会产生比所需更多的行

时间:2013-04-02 22:13:23

标签: sql sql-server-2008 tsql pivot

我有这样的查询:

`SELECT [/BIC/IORSVPTX] as Region,
COUNTRY_ID,
[/BIC/IOWCNTRY] as Country, 
[/BIC/IOC_TRLNO] as Trial, 
[/BIC/IOWQUAL] as ResourceType,
case 

    when [/BIC/IOWQUAL] like '%Supporter%' 
      then 1 
  when [/BIC/IOWQUAL] like '%Monitor%'
      then 3
  when [/BIC/IOWQUAL] like '%PM%'
      then 2

  end as ResourceGroup, 
[1], [2], [3], [4], [5]
FROM 
    (
SELECT  [/BIC/IORSVPTX], 
COUNTRY_ID,
[/BIC/IOWCNTRY], 
[/BIC/IOC_TRLNO], 
[/BIC/IOWQUAL],    case 

    when [/BIC/IOWQUAL] like '%Supporter%' 
      then 1 
  when [/BIC/IOWQUAL] like '%Monitor%'
      then 3
  when [/BIC/IOWQUAL] like '%PM%'
      then 2

  end as ResourceGroup, 
left(CALMONTH,4) as StartYear,
right(CALMONTH,2) as StartMonth,
((left(CALMONTH,4) - 2013) * 12) + right(CALMONTH,2) AS YearMonth,
QUANTITY as Hours
         FROM dbo.Actuals
        where [/BIC/IOC_TRLNO]<>'0000' and left(CALMONTH,4)>2012 and   COUNTRY_ID='10'
and ([/BIC/IOWQUAL] like '%PM%' or [/BIC/IOWQUAL] like'%Monitor%' or [/BIC/IOWQUAL]   like '%Supporter%')
) up
PIVOT (sum(Hours) FOR YearMonth IN ([1],[2],[3],[4],[5])) AS pvt;
IN ([1],[2],[3],[4],[5])) AS pvt;`

它为每个ResourceType和ResourceGroup提供了2行输出,其中1(1)月为1小时,第2行为2(2月)而不是1行

Region  COUNTRY_ID  Country Trial   ResourceType    ResourceGroup   1   2   3   4   5
North America   10  USA 3619     Monitor    3   158.5   NULL    NULL    NULL    NULL
North America   10  USA 3619     Monitor    3   NULL    42  NULL    NULL    NULL
North America   10  USA 3619     PM / LTM / RTM     2   20  NULL    NULL    NULL    NULL
North America   10  USA 3619     PM / LTM / RTM     2   NULL    22  NULL    NULL    NULL
North America   10  USA 3619     Supporter  1   18.5    NULL    NULL    NULL    NULL
North America   10  USA 3619     Supporter  1   NULL    15.75   NULL    NULL    NULL

期望的输出应该是这样的:

Region  COUNTRY_ID  Country Trial   ResourceType    ResourceGroup   1   2   3   4   5
North America   10  USA 3619     Monitor    3   158.5   42  NULL    NULL    NULL
North America   10  USA 3619     PM / LTM / RTM     2   20  22  NULL    NULL    NULL
North America   10  USA 3619     Supporter  1   18.5    15.75   NULL    NULL    NULL

我将非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我发现您当前的查询存在一些问题。

首先,您在外部和子查询中使用CASE表达式。在创建名为ResourceGroup的列时,我并不完全理解您为什么这样做。

其次,如果子查询包含具有不同值的列并且您应用了数据透视,则将获得多行。

例如,在您使用的子查询中:

left(CALMONTH,4) as StartYear,
right(CALMONTH,2) as StartMonth,

但是你在最终选择列表中没有这些,如果你将它们添加到最终选择中,它们可能会显示在枢轴分组期间导致问题的不同值。

根据您现有的查询,我猜您要使用:

SELECT 
  [/BIC/IORSVPTX] as Region,
  COUNTRY_ID,
  [/BIC/IOWCNTRY] as Country, 
  [/BIC/IOC_TRLNO] as Trial, 
  [/BIC/IOWQUAL] as ResourceType,
  ResourceGroup, 
  [1], [2], [3], [4], [5]
FROM 
(
  SELECT [/BIC/IORSVPTX], 
    COUNTRY_ID,
    [/BIC/IOWCNTRY], 
    [/BIC/IOC_TRLNO], 
    [/BIC/IOWQUAL],    
    case 
      when [/BIC/IOWQUAL] like '%Supporter%' then 1 
      when [/BIC/IOWQUAL] like '%Monitor%'   then 3
      when [/BIC/IOWQUAL] like '%PM%'  then 2
    end as ResourceGroup, 
    ((left(CALMONTH,4) - 2013) * 12) + right(CALMONTH,2) AS YearMonth,
    QUANTITY as Hours
  FROM dbo.Actuals
  where [/BIC/IOC_TRLNO]<>'0000' 
    and left(CALMONTH,4)>2012 
    and   COUNTRY_ID='10'
    and ([/BIC/IOWQUAL] like '%PM%' 
         or [/BIC/IOWQUAL] like'%Monitor%' 
         or [/BIC/IOWQUAL] like '%Supporter%')
) up
PIVOT 
(
  sum(Hours) 
  FOR YearMonth IN ([1],[2],[3],[4],[5])
) AS pvt;