-1

我有这样的查询:

`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 月)的小时数和第 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

我会感谢你的帮助!

4

1 回答 1

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;
于 2013-04-02T23:00:11.670 回答